3
Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
5
Use and copying of this software and preparation of derivative works based
6
upon this software are permitted. Any distribution of this software or
7
derivative works must comply with all applicable United States export control
10
This software is made available AS IS, and Xerox Corporation makes no warranty
11
about the software, its performance or its conformity to any specification.
13
|<--- this code is formatted to fit into 80 columns --->|
14
|<--- this code is formatted to fit into 80 columns --->|
15
|<--- this code is formatted to fit into 80 columns --->|
19
Part of the Spacewar system.
24
import java.util.Vector;
25
import java.util.Enumeration;
26
import java.awt.Graphics;
27
import java.awt.Canvas;
28
import java.awt.Image;
31
* The display aspects capture the look and feel of the Game in modular
34
* The model is that constructing a concrete subclass of Display attaches that
35
* kind of display to the game. It will Display the game as it goes along.
36
* A game can have any number of displays. Any of the displays will accept
41
class Display extends Canvas {
43
private static Vector DISPLAYS = new Vector(2);
44
private static Vector PLAYERS = new Vector(2);
45
private static Pilot pilot1, pilot2;
52
Game getGame() { return game; }
53
static Pilot getPilot1() { return pilot1; }
54
static Pilot getPilot2() { return pilot2; }
60
frame = new SWFrame(game, this);
61
DISPLAYS.addElement(this);
65
void noticeSizeChange() {
69
private void initializeOffImage () {
70
int w = getSize().width;
71
int h = getSize().height;
73
offImage = createImage(w, h);
74
offGraphics = offImage.getGraphics();
79
* In our double buffering scheme, painting just means copying the buffer
80
* to the screen. The Display aspect draws into the buffer.
82
public void paint(Graphics g) {
84
g.drawImage(offImage, 0, 0, null);
87
public void update(Graphics g) {
89
* There are 4 steps to this:
90
* - clear the double buffer
91
* - paint the objects into the double buffer
92
* - paint the status into the double buffer
93
* - paint the doublebuffer into the buffer
95
offGraphics.setColor(getBackground());
96
offGraphics.fillRect(0, 0, getBounds().width, getBounds().height);
97
paintObjects(offGraphics);
98
paintStatus(offGraphics);
99
g.drawImage(offImage, 0, 0, null);
102
void paintObjects(Graphics g) { }
103
void paintStatus(Graphics g) {}
105
static aspect DisplayAspect {
107
after (String mode) returning (Game game): call(Game+.new(String)) && args(mode) {
111
if ( mode.equals("1") ) {
112
pilot1 = game.newPlayer(1);
114
else if ( mode.equals("2") ) {
115
pilot1 = game.newPlayer(1);
116
pilot2 = game.newPlayer(2);
118
else if (mode. equals("demo")) {
119
pilot1 = game.newRobot(1);
120
pilot2 = game.newRobot(2);
122
game.error("Invalid mode: " + mode);
129
* I'm not really sure this belongs here.
131
* Being here what it does is makes the Display aspect
132
* responsible for having the Players couple up to it. That's
133
* kind of nice, but its a bit incomplete, since Player is
134
* really part of the GUI, not part of the core Game.
136
* In a future re-factoring this will get worked out better.
137
* What will happen is that GUI will be an aspect that has the
138
* core GUI. Each of the different kinds of displays will be
139
* aspects that tie themselves in.
141
after () returning (Player player): call(Player+.new(..)) {
142
Enumeration elements = DISPLAYS.elements();
143
while ( elements.hasMoreElements() ) {
144
Display display = (Display)elements.nextElement();
145
display.addKeyListener(player);
149
after() returning (Display display): call(Display+.new(..)) {
150
display.noticeSizeChange();
153
after(Display display) returning (): call(void setSize(..)) && target(display) {
154
display.noticeSizeChange();
157
after() returning : call(void Game.clockTick()) {
158
Enumeration elements = DISPLAYS.elements();
159
while ( elements.hasMoreElements() ) {
160
Display display = (Display)elements.nextElement();