~ubuntu-branches/ubuntu/natty/aspectj/natty

« back to all changes in this revision

Viewing changes to org.aspectj/modules/docs/dist/doc/examples/spacewar/Display.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2009-10-04 16:37:23 UTC
  • mfrom: (1.1.3 upstream) (3.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20091004163723-ck4y7j7fhjxskkie
Tags: 1.6.6+dfsg-1
* New upstream release.
  - Update 02_use_gjdoc.diff patch
* Update my email address

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 
3
 
Copyright (c) Xerox Corporation 1998-2002.  All rights reserved.
4
 
 
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
8
 
laws.
9
 
 
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.
12
 
 
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             --->|
16
 
 
17
 
 
18
 
Display.java
19
 
Part of the Spacewar system.
20
 
*/
21
 
 
22
 
package spacewar;
23
 
 
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;
29
 
 
30
 
/**
31
 
 * The display aspects capture the look and feel of the Game in modular
32
 
 * pluggable units.
33
 
 *
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
37
 
 * keyboard input.
38
 
 *
39
 
 */
40
 
 
41
 
class Display  extends Canvas {
42
 
 
43
 
    private static Vector DISPLAYS = new Vector(2);
44
 
    private static Vector PLAYERS  = new Vector(2);
45
 
    private static Pilot  pilot1, pilot2;
46
 
 
47
 
    Game     game;
48
 
    SWFrame  frame;
49
 
    Image    offImage;
50
 
    Graphics offGraphics;
51
 
 
52
 
    Game  getGame()   { return game; }
53
 
    static Pilot getPilot1() { return pilot1; }
54
 
    static Pilot getPilot2() { return pilot2; }
55
 
 
56
 
    Display(Game g) {
57
 
        super();
58
 
        game = g;
59
 
 
60
 
        frame = new SWFrame(game, this);
61
 
        DISPLAYS.addElement(this);
62
 
    }
63
 
 
64
 
 
65
 
    void noticeSizeChange() {
66
 
        initializeOffImage();
67
 
    }
68
 
 
69
 
    private void initializeOffImage () {
70
 
        int w = getSize().width;
71
 
        int h = getSize().height;
72
 
        if ( w > 0 & h > 0) {
73
 
            offImage = createImage(w, h);
74
 
            offGraphics = offImage.getGraphics();
75
 
        }
76
 
    }
77
 
 
78
 
    /*
79
 
     * In our double buffering scheme, painting just means copying the buffer
80
 
     * to the screen.  The Display aspect draws into the buffer.
81
 
     */
82
 
    public void paint(Graphics g) {
83
 
        if (offImage != null)
84
 
            g.drawImage(offImage, 0, 0, null);
85
 
    }
86
 
 
87
 
    public void update(Graphics g) {
88
 
        /*
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
94
 
         */
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);
100
 
    }
101
 
 
102
 
    void paintObjects(Graphics g) { }
103
 
    void paintStatus(Graphics g) {}
104
 
 
105
 
    static aspect DisplayAspect {
106
 
 
107
 
        after (String mode) returning (Game game): call(Game+.new(String)) && args(mode) {
108
 
            new Display1(game);
109
 
            new Display2(game);
110
 
 
111
 
            if ( mode.equals("1") ) {
112
 
                pilot1 = game.newPlayer(1);
113
 
            }
114
 
            else if ( mode.equals("2") ) {
115
 
                pilot1 = game.newPlayer(1);
116
 
                pilot2 = game.newPlayer(2);
117
 
            }
118
 
            else if (mode. equals("demo")) {
119
 
                pilot1 = game.newRobot(1);
120
 
                pilot2 = game.newRobot(2);
121
 
            } else {
122
 
                game.error("Invalid mode: " + mode);
123
 
                game.quit();
124
 
            }
125
 
        }
126
 
 
127
 
 
128
 
        /*
129
 
         * I'm not really sure this belongs here.
130
 
         *
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.
135
 
         *
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.
140
 
         */
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);
146
 
            }
147
 
        }
148
 
 
149
 
        after() returning (Display display): call(Display+.new(..)) {
150
 
            display.noticeSizeChange();
151
 
        }
152
 
 
153
 
        after(Display display) returning (): call(void setSize(..)) && target(display) {
154
 
            display.noticeSizeChange();
155
 
        }
156
 
 
157
 
        after() returning : call(void Game.clockTick()) {
158
 
            Enumeration elements = DISPLAYS.elements();
159
 
            while ( elements.hasMoreElements() ) {
160
 
                Display display = (Display)elements.nextElement();
161
 
                display.repaint();
162
 
            }
163
 
        }
164
 
    }
165
 
}