Added function restart level. Also, player has now 10000 credits per level
[AntiTD.git] / src / se / umu / cs / dit06ajnajs / ATDView.java
blob125876340ef3c9e4e16afed27e68d1e83310a3b5
1 package se.umu.cs.dit06ajnajs;
3 import java.awt.AlphaComposite;
4 import java.awt.BorderLayout;
5 import java.awt.Color;
6 import java.awt.Graphics2D;
7 import java.awt.Graphics;
8 import java.awt.Image;
9 import java.awt.event.ActionListener;
10 import java.awt.event.MouseListener;
11 import java.awt.event.WindowListener;
12 import java.awt.geom.Rectangle2D;
13 import java.awt.image.BufferedImage;
14 import java.util.logging.Logger;
16 import javax.swing.BoxLayout;
17 import javax.swing.JComponent;
18 import javax.swing.JFrame;
19 import javax.swing.JLabel;
20 import javax.swing.JMenu;
21 import javax.swing.JMenuBar;
22 import javax.swing.JMenuItem;
23 import javax.swing.JOptionPane;
24 import javax.swing.JPanel;
25 import javax.swing.JTextArea;
27 import java.util.EventListener;
28 import javax.swing.JButton;
29 import javax.swing.DefaultListModel;
31 import se.umu.cs.dit06ajnajs.agent.AgentPrototypeFactory;
32 import javax.swing.JList;
33 import javax.swing.ListSelectionModel;
34 import se.umu.cs.dit06ajnajs.agent.Unit;
35 import javax.swing.SwingUtilities;
36 import javax.swing.JLabel;
38 public class ATDView {
39 private static Logger logger = Logger.getLogger("AntiTD");
41 private ATDModel model;
43 // Components
44 private JFrame frame;
45 private JTextArea scoreboard;
46 private JLabel messageLabel;
47 //private JLabel scoreboard;
49 private GameComponent gameComponent;
50 private Graphics2D gameGraphics;
51 private Graphics2D gameBackgroundGraphics;
53 // Menu
54 private JMenuItem newGameMenuItem;
55 private JMenuItem restartLevelMenuItem;
56 private JMenuItem pausMenuItem;
57 private JMenuItem quitMenuItem;
58 private JMenuItem helpMenuItem;
59 private JMenuItem aboutMenuItem;
61 // GameController
62 private JButton releaseUnitsButton;
64 private JList unitList;
66 public ATDView(ATDModel model) {
67 this.model = model;
68 createAndShowGUI();
71 /**
72 * Create and show GUI used by this application.
74 private void createAndShowGUI() {
75 this.gameComponent = new GameComponent();
77 JPanel mainPanel = new JPanel(new BorderLayout());
79 mainPanel.add(createControlPanel(), BorderLayout.EAST);
80 mainPanel.add(createMenu(), BorderLayout.NORTH);
81 mainPanel.add(gameComponent, BorderLayout.CENTER);
83 this.frame = new JFrame();
84 frame.setTitle("AntiTD");
85 frame.add(mainPanel);
86 frame.pack();
87 frame.setVisible(true);
90 /**
91 * Creates the JPanel containg Components to control the game.
93 * @return The JPanel containg Components to control the game.
95 private JPanel createControlPanel() {
96 JPanel resultPanel = new JPanel(new BorderLayout());
98 // JList unitTypes
99 DefaultListModel unitTypesListModel = new DefaultListModel();
100 AgentPrototypeFactory factory = AgentPrototypeFactory.getInstance();
101 for (String type : factory.getUnitTypes()) {
102 Unit unit = factory.createUnit(type);
103 unitTypesListModel.addElement(unit);
105 this.unitList= new JList(unitTypesListModel);
106 unitList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
107 resultPanel.add(unitList, BorderLayout.NORTH);
109 JPanel shopPanel = new JPanel();
111 shopPanel.setLayout(new BoxLayout(shopPanel, BoxLayout.Y_AXIS));
112 // Scoreboard
113 scoreboard = new JTextArea("Credit:\t0\nScore:\t0\nCleared Units:\t0/0",1,10);
114 scoreboard.setOpaque(false);
115 scoreboard.setEditable(false);
117 // MessageArea
118 this.messageLabel = new JLabel();
119 resultPanel.add(messageLabel, BorderLayout.CENTER);
121 // Release Button
122 this.releaseUnitsButton = new JButton("Release Units");
124 shopPanel.add(scoreboard);
125 shopPanel.add(releaseUnitsButton);
127 resultPanel.add(shopPanel, BorderLayout.SOUTH);
128 return resultPanel;
132 * Creates the MenuBar used by this application.
134 * @return The MenuBar used by this application.
136 private JMenuBar createMenu() {
137 JMenuBar menubar = new JMenuBar();
139 // Menu AntiTD
140 JMenu antiTDMenu = new JMenu("AntiTD");
142 // TODO change name depending on if a game is running or not.
143 this.newGameMenuItem = new JMenuItem("New Game");
144 antiTDMenu.add(newGameMenuItem);
146 this.restartLevelMenuItem = new JMenuItem("Restart level");
147 antiTDMenu.add(restartLevelMenuItem);
149 this.pausMenuItem = new JMenuItem("Pause");
150 antiTDMenu.add(pausMenuItem);
152 this.quitMenuItem = new JMenuItem("Quit");
153 antiTDMenu.add(quitMenuItem);
155 // Menu Help
156 JMenu helpMenu = new JMenu("Help");
158 this.helpMenuItem = new JMenuItem("Help");
159 helpMenu.add(helpMenuItem);
161 this.aboutMenuItem = new JMenuItem("About");
162 helpMenu.add(aboutMenuItem);
164 // Add menus to menubar
165 menubar.add(antiTDMenu);
166 menubar.add(helpMenu);
167 return menubar;
171 * Returns the Graphics used to update visual information about Agents in
172 * the game.
174 * @return The Graphics used by Agents on the Level.
176 public Graphics getGameGraphics() {
177 return gameGraphics;
180 public String getSelectedUnitType() {
181 return this.unitList.getSelectedValue().toString();
185 * Returns the Graphics used to update visual information about the Background in
186 * the game.
188 * @return The Graphics used by Controller to create the Level background.
190 public Graphics getBackgroundGraphics() {
191 return this.gameBackgroundGraphics;
195 * Updates the backgroundimage used, for example when MapSquare are changed
196 * during gameplay.
198 public void updateBackgroundImage() {
199 this.gameComponent.updateBackgroundImage();
203 * Calls repaint on the GameComponent.
205 public void repaintGame() {
206 gameComponent.repaint();
210 * Update and repaint Pause menu information.
212 public void updatePauseMenu(final String TEXT) {
213 SwingUtilities.invokeLater(new Runnable() {
214 public void run() {
215 //this.newGameMenuItem = new JMenuItem("New Game/Restart");
216 ATDView.this.pausMenuItem.setText(TEXT);
222 * Prints a message to messageLabel, previous message is erased.
224 public void printMessage(final String TEXT) {
225 SwingUtilities.invokeLater(new Runnable() {
226 public void run() {
227 ATDView.this.messageLabel.setText(TEXT);
233 * Updates the numbers on the scoreboard
235 public void updateScoreboard() {
236 SwingUtilities.invokeLater(new Runnable() {
237 public void run() {
238 scoreboard.setText("Credit:\t" + model.getCredit() + "\n" +
239 "Score:\t" + model.getScore() + "\n" +
240 "Cleared units:\t" + model.getNumOfClearedUnits()
241 + "/" + model.getUnitsToWin());
246 public String promtForHighScoreEntry() {
247 String message = "Want to send in a highscore, what's your username?";
248 String result = JOptionPane.showInputDialog(frame, message);
249 return result;
252 public void showLevelCompleteDialog() {
253 String title = "Level completed";
254 String message = "Congratulations! You have completed this level." +
255 "\nClick OK-button to go to the next level";
256 JOptionPane.showMessageDialog(frame,
257 message,
258 title,
259 JOptionPane.INFORMATION_MESSAGE,
260 null);
264 public void showAboutDialog() {
265 // TODO Auto-generated method stub
266 String title = "About";
267 String message = "*** Anti-tower Defence (alfa) ***" +
268 "\n\nCreated by:\n" +
269 "Andreas Jakobsson (dit06ajs@cs.umu.se)\n" +
270 "Anton Johansson (dit06ajn@cs.umu.se)";
271 JOptionPane.showMessageDialog(frame,
272 message,
273 title,
274 JOptionPane.INFORMATION_MESSAGE,
275 null);
278 public void showHelpDialog() {
279 // TODO Auto-generated method stub
280 String title = "Help";
281 JTextArea message = new JTextArea("Instructions\n" +
282 "The goal for Anti-tower Defence is to release units " +
283 "from start squares in such a way that they reach goal " +
284 "squares without being killed by towers. When enough " +
285 "units has made it to goal squares you have completed " +
286 "the level\n",1,40);
287 message.setOpaque(false);
288 message.setEditable(false);
289 message.setLineWrap(true);
290 JOptionPane.showMessageDialog(frame,
291 message,
292 title,
293 JOptionPane.INFORMATION_MESSAGE,
294 null);
298 * GameComponent is the componenent on which the Level and all Units and
299 * Towers is shown.
301 private class GameComponent extends JComponent {
302 private Image backgroundImage;
303 // TODO: Se if its possible to change to Image
304 private BufferedImage gameImage;
306 private int width;
307 private int height;
309 public GameComponent() {
310 super();
311 this.width = (int) model.getMapDimension().getWidth();
312 this.height = (int) model.getMapDimension().getHeight();
313 this.setPreferredSize(model.getMapDimension());
315 // Background Image
316 this.backgroundImage = model.getMapImage();
318 // Game Image
319 this.gameImage = new BufferedImage(width, height,
320 BufferedImage.TYPE_INT_ARGB);
321 ATDView.this.gameGraphics = gameImage.createGraphics();
324 public void updateBackgroundImage() {
325 this.backgroundImage = model.getMapImage();
329 * Ovverrides standard paintComponent, used to only repaint information
330 * about Unit and Tower information.
332 * @param g The Graphics used to update visual information.
334 @Override
335 public void paintComponent(Graphics g) {
336 logger.fine("paintComponent(...)");
338 g.drawImage(backgroundImage, 0, 0, null);
340 // Should contain updated information from Controller
341 g.drawImage(gameImage, 0, 0, null);
343 // Clear gameImage image with a big transparent rectangle.
344 Color transparent = new Color(0, 0, 0, 0);
345 gameGraphics.setColor(transparent);
346 gameGraphics.setComposite(AlphaComposite.Src);
347 gameGraphics.fill(new Rectangle2D.Double(0, 0, width, height));
351 /* Set Listener methods ******************************/
353 // TODO: Good way to not cast and not be to specific?
354 public void addClosingListener(EventListener el) {
355 this.quitMenuItem.addActionListener((ActionListener) el);
356 this.frame.addWindowListener((WindowListener) el);
359 // Set menu item listeners
360 public void addNewGameMenuItemListener(ActionListener al) {
361 this.newGameMenuItem.addActionListener(al);
364 public void addRestartLevelMenuItemListener(ActionListener al) {
365 this.restartLevelMenuItem.addActionListener(al);
368 public void addPauseMenuItemListener(ActionListener al) {
369 this.pausMenuItem.addActionListener(al);
372 public void addHelpMenuItemListener(ActionListener al) {
373 this.helpMenuItem.addActionListener(al);
376 public void addAboutMenuItemListener(ActionListener al) {
377 this.aboutMenuItem.addActionListener(al);
380 // Controls
381 public void addReleaseUnitListener(ActionListener al) {
382 this.releaseUnitsButton.addActionListener(al);
385 public void addMapListener(MouseListener ml) {
386 gameComponent.addMouseListener(ml);