aboutsummaryrefslogtreecommitdiffstats
path: root/docs/dist/examples/spacewar/SWFrame.java
blob: 6dfb9f6f681d34f492a45b0a9d613aa9b4bc6ceb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*

Copyright (c) Xerox Corporation 1998-2002.  All rights reserved.

Use and copying of this software and preparation of derivative works based
upon this software are permitted.  Any distribution of this software or
derivative works must comply with all applicable United States export control
laws.

This software is made available AS IS, and Xerox Corporation makes no warranty
about the software, its performance or its conformity to any specification.

|<---            this code is formatted to fit into 80 columns             --->|
|<---            this code is formatted to fit into 80 columns             --->|
|<---            this code is formatted to fit into 80 columns             --->|


SWFrame.java
Part of the Spacewar system.

*/

package spacewar;

import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.Dimension;
import java.awt.Insets;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

class SWFrame extends Frame implements ActionListener {
    private Game     game;
    private Display  display;
    private Menu     menu;

    Game     getGame()      { return game; }
    Display  getDisplay() { return display; }
    Menu     getMenu()      { return menu; }

    SWFrame(Game theGame, Display d) {
        super("Space War!");

        game = theGame;

        display = d;
        add(display);

        // create menu
        menu = new Menu("Game");
        MenuItem item1 = new MenuItem("Add Robot",   new MenuShortcut('a'));
        MenuItem item2 = new MenuItem("Reset Ships", new MenuShortcut('r'));
        MenuItem item3 = new MenuItem("Quit",        new MenuShortcut('q'));
        item1.setActionCommand("Add Robot");
        item2.setActionCommand("Reset Ships");
        item3.setActionCommand("Quit");
        menu.add(item1);
        menu.add(item2);
        menu.add(item3);
        menu.addActionListener(this);

        setMenuBar(new MenuBar());
        getMenuBar().add(menu);

        Dimension screenSize = new Dimension(500, 500);
        setSize(screenSize);
        setVisible(true);
        toFront();

        Insets inset = getInsets();
        int displayWidth  = screenSize.width - inset.left - inset.right;
        int displayHeight = screenSize.height - inset.top - inset.bottom;
        display.setSize(displayWidth, displayHeight);
    }

    public void actionPerformed(ActionEvent e) {
        String s = e.getActionCommand();
        if (s.equals("Add Robot")) {
            getGame().addRobot();
        }
        else if (s.equals("Reset Ships")) {
            getGame().resetShips();
        }
        else if (s.equals("Quit")) {
            getGame().quit();
        }
    }
}