5 * $Id: FrenzyView.java,v 1.2 2008/04/30 04:13:13 rmh3093 Exp $
8 * $Log: FrenzyView.java,v $
9 * Revision 1.2 2008/04/30 04:13:13 rmh3093
10 * first attemp at moving enemies
12 * Revision 1.1 2008/04/26 15:46:15 rmh3093
18 Copyright (c) 2008, Ryan M. Hope
21 Redistribution and use in source and binary forms, with or without modification,
22 are permitted provided that the following conditions are met:
24 * Redistributions of source code must retain the above copyright notice,
25 this list of conditions and the following disclaimer.
26 * Redistributions in binary form must reproduce the above copyright notice,
27 this list of conditions and the following disclaimer in the documentation
28 and/or other materials provided with the distribution.
29 * Neither the name of the project nor the names of its contributors may be
30 used to endorse or promote products derived from this software without
31 specific prior written permission.
33 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
34 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
37 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
40 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 import java
.awt
.event
.*;
48 import javax
.swing
.border
.Border
;
49 import javax
.swing
.border
.TitledBorder
;
52 * The visual aspect of Frenzy
56 @SuppressWarnings("serial")
57 public class FrenzyView
extends JFrame
{
59 ActionListener enemySpawner
;
60 ActionListener enemyMover
;
62 private FrenzyModel f_model
;
63 private BetterRandom betterRandom
;
65 Thread enemySpawnerThread
= new Thread(new EnemySpawner());
68 * Customized button to represent a square on the game board
72 class BoardSqaure
extends JButton
{
74 // The default button colors
79 * Create a new board square
81 public BoardSqaure() {
83 // Do not want board squares stealing focus from player controls
86 // Get the default button color for unoccupied squares
87 defaultcolor
= getBackground();
89 // Set margins to 0 so that symbol shows up with out a giant button
90 setMargin(new Insets(0,0,0,0));
92 // Set button size to something reasonable
93 setPreferredSize(new Dimension(20,20));
95 // Force font size so that symbol fits within button bounds
96 setFont(new Font(getFont().getName(), getFont().getStyle(), 12));
100 * Returns the occupant of the board square
102 * @return the board square occupant
104 FrenzyModel
.OCCUPANT
isOccupied() {
105 FrenzyModel
.OCCUPANT ret
= FrenzyModel
.OCCUPANT
.NONE
;
106 if (getText().length() > 0) {
107 if (getText() == FrenzyPlayer
.symbol
) {
108 ret
= FrenzyModel
.OCCUPANT
.PLAYER
;
110 ret
= FrenzyModel
.OCCUPANT
.ENEMY
;
118 // The label of the game control buttons
119 private static final String
[] buttonLables
= {
120 "Pause","Spawn","++","--","Restart","Quit"
124 Container mainPane
= this.getContentPane();
125 Container infoContainer
= new Container();
126 Container infoSubcontainerLeft
= new Container();
127 Container infoSubcontainerRight
= new Container();
128 Container controlsContainer
= new Container();
129 JCheckBox pauseControl
= new JCheckBox();
130 JButton
[] controlButtons
= new JButton
[5];
131 JLabel consumptionLabel
= new JLabel("Consumption");
132 JLabel pieceLabel
= new JLabel("Piece is ");
133 JLabel consumedLabel
= new JLabel("0");
134 JLabel statusLabel
= new JLabel("Game Running!");
135 JLabel lastActionLabel
= new JLabel("S ate K");
136 JPanel gameboard
= new JPanel();
137 BoardSqaure
[][] boardSquares
;
138 TitledBorder boardContainerBoarder
;
139 Border standardBoarder
;
144 * @param f_model Frenzy f_model of important game data
146 FrenzyView(FrenzyModel f_model
, BetterRandom betterRandom
) {
147 this.f_model
= f_model
;
148 this.betterRandom
= betterRandom
;
150 // Set some basics settings
152 setDefaultCloseOperation(JFrame
.EXIT_ON_CLOSE
);
153 standardBoarder
= BorderFactory
.createTitledBorder("");
154 pauseControl
.setSelected(false);
158 // Setup view ====================================================
159 mainPane
.setLayout(new BorderLayout());
162 infoContainer
.setLayout(new GridLayout(1,4));
163 infoSubcontainerLeft
.setLayout(new GridLayout(2,1));
164 infoSubcontainerLeft
.add(consumptionLabel
);
165 infoSubcontainerLeft
.add(pieceLabel
);
166 infoContainer
.add(infoSubcontainerLeft
);
167 infoSubcontainerRight
.setLayout(new GridLayout(2,1));
168 consumedLabel
.setBorder(standardBoarder
);
169 statusLabel
.setBorder(standardBoarder
);
170 infoSubcontainerRight
.add(consumedLabel
);
171 infoSubcontainerRight
.add(statusLabel
);
172 infoContainer
.add(infoSubcontainerLeft
);
173 infoContainer
.add(infoSubcontainerRight
);
174 lastActionLabel
.setBorder(standardBoarder
);
175 infoContainer
.add(lastActionLabel
);
178 controlsContainer
.setLayout(new GridLayout(3,2));
179 for (int i
=0; i
<6; i
++) {
181 pauseControl
.setText(buttonLables
[i
]);
182 pauseControl
.setFocusable(false);
183 controlsContainer
.add(pauseControl
);
185 controlButtons
[i
-1] = new JButton(buttonLables
[i
]);
186 controlButtons
[i
-1].setFocusable(false);
187 controlsContainer
.add(controlButtons
[i
-1]);
190 infoContainer
.add(controlsContainer
);
192 mainPane
.add(infoContainer
, BorderLayout
.NORTH
);
195 int boardsize
= f_model
.getBoardSize();
196 boardContainerBoarder
= BorderFactory
.createTitledBorder("Game Board");
197 gameboard
.setBorder(boardContainerBoarder
);
198 gameboard
.setLayout(new GridLayout(boardsize
, boardsize
));
200 boardSquares
= new BoardSqaure
[boardsize
][boardsize
];
201 for (int i
=0; i
<boardsize
; i
++) {
202 for (int j
=0; j
<boardsize
; j
++) {
203 boardSquares
[i
][j
] = new BoardSqaure();
204 gameboard
.add(boardSquares
[i
][j
]);
207 mainPane
.add(gameboard
, BorderLayout
.CENTER
);
218 protected void start() {
219 pieceLabel
.setText("Piece is " + f_model
.player
.getSymbol());
221 f_model
.gamestate
= true;
222 enemySpawnerThread
.start();
225 class EnemySpawner
implements Runnable
{
230 Thread
.sleep(f_model
.enemySpawnInterval
);
231 } catch (InterruptedException e
) {
234 enemySpawner
.actionPerformed(new ActionEvent(this,
235 ActionEvent
.ACTION_PERFORMED
, "spawn"));
236 Thread
.sleep(f_model
.enemySpawnInterval
);
238 } catch (InterruptedException e
) {
245 * Add a player to a board square
247 * @param player the player
248 * @param location the board square to move to
250 public void addOccupant(FrenzyPlayer player
, BoardSqaure location
) {
251 location
.setText(player
.getSymbol());
252 location
.setBackground(new Color(255,255,0));
256 * Add an enemy to a board square
258 * @param enemy the enemy
259 * @param location the board square to move to
261 public void addOccupant(FrenzyEnemy enemy
, BoardSqaure location
) {
262 location
.setText(enemy
.getSymbol());
263 location
.setBackground(new Color(0,255,0));
267 * Remove occupant from board square
269 * @param location board square to remove occupant from
271 public void removeOccupant(BoardSqaure location
) {
272 location
.setText("");
273 location
.setBackground(location
.defaultcolor
);
281 int px
= f_model
.player
.getXposition();
282 int py
= f_model
.player
.getYposition();
283 addOccupant(f_model
.player
, boardSquares
[py
][px
]);
287 * Register a player controls handler
289 * @param pc player controls handler
291 void addPlayerControlsListener(KeyListener pc
) {
292 this.addKeyListener(pc
);
296 * Register a quit handler
298 * @param quit quit handler
300 void addQuitListener(ActionListener quit
) {
301 controlButtons
[4].addActionListener(quit
);
305 * Register a reset handler
309 void addResetListener(ActionListener reset
) {
310 controlButtons
[3].addActionListener(reset
);
314 * Register a spawn handler
318 void addSpawnListener(ActionListener spawn
) {
319 controlButtons
[0].addActionListener(spawn
);
323 * Register a pause handler
327 void addPauseListener(ActionListener pause
) {
328 pauseControl
.addActionListener(pause
);
332 * Register an enemy spawn handler
336 void addEnemySpawnListener(ActionListener spawn
) {
337 enemySpawner
= spawn
;