sadjfda
[rmh3093.git] / project2 / FrenzyView.java
blob7f3a22968f60f430639448e5c21d643f4d36f41a
1 /*
2 * FrenzyView.java
4 * Version:
5 * $Id: FrenzyView.java,v 1.2 2008/04/30 04:13:13 rmh3093 Exp $
7 * Revisions:
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
13 * Initial revision
18 Copyright (c) 2008, Ryan M. Hope
19 All rights reserved.
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.
45 import java.awt.*;
46 import java.awt.event.*;
47 import javax.swing.*;
48 import javax.swing.border.Border;
49 import javax.swing.border.TitledBorder;
51 /**
52 * The visual aspect of Frenzy
54 * @author Ryan Hope
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());
67 /**
68 * Customized button to represent a square on the game board
70 * @author Ryan Hope
72 class BoardSqaure extends JButton {
74 // The default button colors
75 Color defaultcolor;
78 /**
79 * Create a new board square
81 public BoardSqaure() {
83 // Do not want board squares stealing focus from player controls
84 setFocusable(false);
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));
99 /**
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;
109 } else {
110 ret = FrenzyModel.OCCUPANT.ENEMY;
113 return ret;
118 // The label of the game control buttons
119 private static final String[] buttonLables = {
120 "Pause","Spawn","++","--","Restart","Quit"
123 // Components
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;
142 * Create a new view
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
151 setTitle("Frenzy");
152 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
153 standardBoarder = BorderFactory.createTitledBorder("");
154 pauseControl.setSelected(false);
155 setFocusable(true);
156 setResizable(false);
158 // Setup view ====================================================
159 mainPane.setLayout(new BorderLayout());
161 // Info area
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);
177 // Game controls
178 controlsContainer.setLayout(new GridLayout(3,2));
179 for (int i=0; i<6; i++) {
180 if (i==0) {
181 pauseControl.setText(buttonLables[i]);
182 pauseControl.setFocusable(false);
183 controlsContainer.add(pauseControl);
184 } else {
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);
194 // Game board
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);
209 this.pack();
211 setVisible(true);
216 * Start game
218 protected void start() {
219 pieceLabel.setText("Piece is " + f_model.player.getSymbol());
220 spawnPlayer();
221 f_model.gamestate = true;
222 enemySpawnerThread.start();
225 class EnemySpawner implements Runnable {
227 public void run() {
228 try {
229 try {
230 Thread.sleep(f_model.enemySpawnInterval);
231 } catch (InterruptedException e) {
233 while (true) {
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);
274 location.repaint();
278 * Spawn the player
280 void spawnPlayer() {
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
307 * @param reset
309 void addResetListener(ActionListener reset) {
310 controlButtons[3].addActionListener(reset);
314 * Register a spawn handler
316 * @param spawn
318 void addSpawnListener(ActionListener spawn) {
319 controlButtons[0].addActionListener(spawn);
323 * Register a pause handler
325 * @param pause
327 void addPauseListener(ActionListener pause) {
328 pauseControl.addActionListener(pause);
332 * Register an enemy spawn handler
334 * @param spawn
336 void addEnemySpawnListener(ActionListener spawn) {
337 enemySpawner = spawn;