Merge branch 'master' of ssh://repo.or.cz/srv/git/AntiTD
[AntiTD.git] / src / se / umu / cs / dit06ajnajs / ATDView.java
blobae97b8a26c4583a4463e72a18897507726404a13
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;
30 import se.umu.cs.dit06ajnajs.agent.AgentPrototypeFactory;
31 import javax.swing.JList;
32 import javax.swing.ListSelectionModel;
33 import se.umu.cs.dit06ajnajs.agent.Unit;
34 import javax.swing.SwingUtilities;
35 import javax.swing.JLabel;
37 public class ATDView {
38 private static Logger logger = Logger.getLogger("AntiTD");
40 private ATDModel model;
42 // Components
43 private JFrame frame;
44 private JTextArea scoreboard;
45 private JLabel messageLabel;
46 //private JLabel scoreboard;
48 private GameComponent gameComponent;
49 private Graphics2D gameGraphics;
50 private Graphics2D gameBackgroundGraphics;
52 // Menu
53 private JMenuItem newGameMenuItem;
54 private JMenuItem pausMenuItem;
55 private JMenuItem quitMenuItem;
56 private JMenuItem helpMenuItem;
57 private JMenuItem aboutMenuItem;
59 // GameController
60 private JButton releaseUnitsButton;
62 private JList unitList;
64 public ATDView(ATDModel model) {
65 this.model = model;
66 createAndShowGUI();
69 /**
70 * Create and show GUI used by this application.
72 private void createAndShowGUI() {
73 this.gameComponent = new GameComponent();
75 JPanel mainPanel = new JPanel(new BorderLayout());
77 mainPanel.add(createControlPanel(), BorderLayout.EAST);
78 mainPanel.add(createMenu(), BorderLayout.NORTH);
79 mainPanel.add(gameComponent, BorderLayout.CENTER);
81 this.frame = new JFrame();
82 frame.setTitle("AntiTD");
83 frame.add(mainPanel);
84 frame.pack();
85 frame.setVisible(true);
88 /**
89 * Creates the JPanel containg Components to control the game.
91 * @return The JPanel containg Components to control the game.
93 private JPanel createControlPanel() {
94 JPanel resultPanel = new JPanel(new BorderLayout());
96 // JList unitTypes
97 DefaultListModel unitTypesListModel = new DefaultListModel();
98 AgentPrototypeFactory factory = AgentPrototypeFactory.getInstance();
99 for (String type : factory.getUnitTypes()) {
100 Unit unit = factory.createUnit(type);
101 unitTypesListModel.addElement(unit);
103 this.unitList= new JList(unitTypesListModel);
104 unitList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
105 resultPanel.add(unitList, BorderLayout.NORTH);
107 JPanel shopPanel = new JPanel();
109 shopPanel.setLayout(new BoxLayout(shopPanel, BoxLayout.Y_AXIS));
110 // Scoreboard
111 scoreboard = new JTextArea("Credit:\t0\nScore:\t0\nCleared Units:\t0/0",1,10);
112 scoreboard.setOpaque(false);
113 scoreboard.setEditable(false);
115 // MessageArea
116 this.messageLabel = new JLabel();
117 resultPanel.add(messageLabel, BorderLayout.CENTER);
119 // Release Button
120 this.releaseUnitsButton = new JButton("Release Units");
122 shopPanel.add(scoreboard);
123 shopPanel.add(releaseUnitsButton);
125 resultPanel.add(shopPanel, BorderLayout.SOUTH);
126 return resultPanel;
130 * Creates the MenuBar used by this application.
132 * @return The MenuBar used by this application.
134 private JMenuBar createMenu() {
135 JMenuBar menubar = new JMenuBar();
137 // Menu AntiTD
138 JMenu antiTDMenu = new JMenu("AntiTD");
140 // TODO change name depending on if a game is running or not.
141 this.newGameMenuItem = new JMenuItem("New Game");
142 antiTDMenu.add(newGameMenuItem);
144 this.pausMenuItem = new JMenuItem("Pause");
145 antiTDMenu.add(pausMenuItem);
147 this.quitMenuItem = new JMenuItem("Quit");
148 antiTDMenu.add(quitMenuItem);
150 // Menu Help
151 JMenu helpMenu = new JMenu("Help");
153 this.helpMenuItem = new JMenuItem("Help");
154 helpMenu.add(helpMenuItem);
156 this.aboutMenuItem = new JMenuItem("About");
157 helpMenu.add(aboutMenuItem);
159 // Add menus to menubar
160 menubar.add(antiTDMenu);
161 menubar.add(helpMenu);
162 return menubar;
166 * Returns the Graphics used to update visual information about Agents in
167 * the game.
169 * @return The Graphics used by Agents on the Level.
171 public Graphics getGameGraphics() {
172 return gameGraphics;
175 public String getSelectedUnitType() {
176 return this.unitList.getSelectedValue().toString();
180 * Returns the Graphics used to update visual information about the Background in
181 * the game.
183 * @return The Graphics used by Controller to create the Level background.
185 public Graphics getBackgroundGraphics() {
186 return this.gameBackgroundGraphics;
190 * Updates the backgroundimage used, for example when MapSquare are changed
191 * during gameplay.
193 public void updateBackgroundImage() {
194 this.gameComponent.updateBackgroundImage();
198 * Calls repaint on the GameComponent.
200 public void repaintGame() {
201 gameComponent.repaint();
205 * Update and repaint Pause menu information.
207 public void updatePauseMenu(final String TEXT) {
208 SwingUtilities.invokeLater(new Runnable() {
209 public void run() {
210 //this.newGameMenuItem = new JMenuItem("New Game/Restart");
211 ATDView.this.pausMenuItem.setText(TEXT);
217 * Prints a message to messageLabel, previous message is erased.
219 public void printMessage(final String TEXT) {
220 SwingUtilities.invokeLater(new Runnable() {
221 public void run() {
222 ATDView.this.messageLabel.setText(TEXT);
228 * Updates the numbers on the scoreboard
230 public void updateScoreboard() {
231 SwingUtilities.invokeLater(new Runnable() {
232 public void run() {
233 scoreboard.setText("Credit:\t" + model.getCredit() + "\n" +
234 "Score:\t" + model.getScore() + "\n" +
235 "Cleared units:\t" + model.getNumOfClearedUnits()
236 + "/" + model.getUnitsToWin());
241 public String promtForHighScoreEntry() {
242 String message = "Want to send in a highscore, what's your username?";
243 String result = JOptionPane.showInputDialog(frame, message);
244 return result;
247 public void showLevelCompleteDialog() {
248 String title = "Level completed";
249 String message = "Congratulations! You have completed this level." +
250 "\nClick OK-button to go to the next level";
251 JOptionPane.showMessageDialog(frame,
252 message,
253 title,
254 JOptionPane.INFORMATION_MESSAGE,
255 null);
259 public void showAboutDialog() {
260 // TODO Auto-generated method stub
261 String title = "About";
262 String message = "*** Anti-tower Defence (alfa) ***" +
263 "\n\nCreated by:\n" +
264 "Andreas Jakobsson (dit06ajs@cs.umu.se)\n" +
265 "Anton Johansson (dit06ajn@cs.umu.se)";
266 JOptionPane.showMessageDialog(frame,
267 message,
268 title,
269 JOptionPane.INFORMATION_MESSAGE,
270 null);
273 public void showHelpDialog() {
274 // TODO Auto-generated method stub
275 String title = "Help";
276 JTextArea message = new JTextArea("Instructions\n" +
277 "The goal for Anti-tower Defence is to release units " +
278 "from start squares in such a way that they reach goal " +
279 "squares without being killed by towers. When enough " +
280 "units has made it to goal squares you have completed " +
281 "the level\n",1,40);
282 message.setOpaque(false);
283 message.setEditable(false);
284 message.setLineWrap(true);
285 JOptionPane.showMessageDialog(frame,
286 message,
287 title,
288 JOptionPane.INFORMATION_MESSAGE,
289 null);
293 * GameComponent is the componenent on which the Level and all Units and
294 * Towers is shown.
296 private class GameComponent extends JComponent {
297 private Image backgroundImage;
298 // TODO: Se if its possible to change to Image
299 private BufferedImage gameImage;
301 private int width;
302 private int height;
304 public GameComponent() {
305 super();
306 this.width = (int) model.getMapDimension().getWidth();
307 this.height = (int) model.getMapDimension().getHeight();
308 this.setPreferredSize(model.getMapDimension());
310 // Background Image
311 this.backgroundImage = model.getMapImage();
313 // Game Image
314 this.gameImage = new BufferedImage(width, height,
315 BufferedImage.TYPE_INT_ARGB);
316 ATDView.this.gameGraphics = gameImage.createGraphics();
319 public void updateBackgroundImage() {
320 this.backgroundImage = model.getMapImage();
324 * Ovverrides standard paintComponent, used to only repaint information
325 * about Unit and Tower information.
327 * @param g The Graphics used to update visual information.
329 @Override
330 public void paintComponent(Graphics g) {
331 logger.fine("paintComponent(...)");
333 g.drawImage(backgroundImage, 0, 0, null);
335 // Should contain updated information from Controller
336 g.drawImage(gameImage, 0, 0, null);
338 // Clear gameImage image with a big transparent rectangle.
339 Color transparent = new Color(0, 0, 0, 0);
340 gameGraphics.setColor(transparent);
341 gameGraphics.setComposite(AlphaComposite.Src);
342 gameGraphics.fill(new Rectangle2D.Double(0, 0, width, height));
346 /* Set Listener methods ******************************/
348 // TODO: Good way to not cast and not be to specific?
349 public void addClosingListener(EventListener el) {
350 this.quitMenuItem.addActionListener((ActionListener) el);
351 this.frame.addWindowListener((WindowListener) el);
354 // Set menu item listeners
355 public void addNewGameMenuItemListener(ActionListener al) {
356 this.newGameMenuItem.addActionListener(al);
359 public void addPauseMenuItemListener(ActionListener al) {
360 this.pausMenuItem.addActionListener(al);
363 public void addHelpMenuItemListener(ActionListener al) {
364 this.helpMenuItem.addActionListener(al);
367 public void addAboutMenuItemListener(ActionListener al) {
368 this.aboutMenuItem.addActionListener(al);
371 // Controls
372 public void addReleaseUnitListener(ActionListener al) {
373 this.releaseUnitsButton.addActionListener(al);
376 public void addMapListener(MouseListener ml) {
377 gameComponent.addMouseListener(ml);