Renamed Map.java to Level.java
[AntiTD.git] / src / se / umu / cs / dit06ajnajs / ATDView.java
blob95f864f435ea7390faee873f1a2ba18489b1a822
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.FlowLayout;
7 import java.awt.Graphics2D;
8 import java.awt.Graphics;
9 import java.awt.Image;
10 import java.awt.event.ActionListener;
11 import java.awt.event.MouseListener;
12 import java.awt.event.WindowListener;
13 import java.awt.geom.Rectangle2D;
14 import java.awt.image.BufferedImage;
15 import java.util.logging.Logger;
17 import javax.swing.BoxLayout;
18 import javax.swing.JComponent;
19 import javax.swing.JFrame;
20 import javax.swing.JMenu;
21 import javax.swing.JMenuBar;
22 import javax.swing.JMenuItem;
23 import javax.swing.JPanel;
24 import javax.swing.JTextArea;
26 import java.util.EventListener;
27 import javax.swing.JButton;
28 import javax.swing.DefaultListModel;
29 import se.umu.cs.dit06ajnajs.agent.AgentPrototypeFactory;
30 import javax.swing.JList;
31 import javax.swing.ListSelectionModel;
32 import se.umu.cs.dit06ajnajs.agent.Unit;
33 import javax.swing.SwingUtilities;
35 public class ATDView {
36 private static Logger logger = Logger.getLogger("AntiTD");
38 private ATDModel model;
40 // Components
41 private JFrame frame;
42 private JTextArea scoreboard;
44 private GameComponent gameComponent;
45 private Graphics2D gameGraphics;
46 private Graphics2D gameBackgroundGraphics;
48 // Menu
49 private JMenuItem newGameMenuItem;
50 private JMenuItem pausMenuItem;
51 private JMenuItem quitMenuItem;
52 private JMenuItem helpMenuItem;
53 private JMenuItem aboutMenuItem;
55 // GameController
56 private JButton releaseUnitsButton;
58 private JList unitList;
60 public ATDView(ATDModel model) {
61 this.model = model;
62 createAndShowGUI();
65 /**
66 * Create and show GUI used by this application.
68 private void createAndShowGUI() {
69 this.gameComponent = new GameComponent();
71 JPanel mainPanel = new JPanel(new BorderLayout());
73 mainPanel.add(createControlPanel(), BorderLayout.EAST);
74 mainPanel.add(createMenu(), BorderLayout.NORTH);
75 mainPanel.add(gameComponent, BorderLayout.CENTER);
77 this.frame = new JFrame();
78 frame.setTitle("AntiTD");
79 frame.add(mainPanel);
80 frame.pack();
81 frame.setVisible(true);
84 /**
85 * Creates the JPanel containg Components to control the game.
87 * @return The JPanel containg Components to control the game.
89 private JPanel createControlPanel() {
90 JPanel resultPanel = new JPanel(new BorderLayout());
92 // JList unitTypes
93 DefaultListModel unitTypesListModel = new DefaultListModel();
94 AgentPrototypeFactory factory = AgentPrototypeFactory.getInstance();
95 for (String type : factory.getUnitTypes()) {
96 Unit unit = factory.createUnit(type);
97 unitTypesListModel.addElement(unit);
99 this.unitList= new JList(unitTypesListModel);
100 unitList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
101 resultPanel.add(unitList, BorderLayout.NORTH);
103 JPanel shopPanel = new JPanel();
104 shopPanel.setLayout(new BoxLayout(shopPanel, BoxLayout.Y_AXIS));
105 // Scoreboard
106 scoreboard = new JTextArea("Credit: \nScore: ", 1, 10);
107 // Release Button
108 this.releaseUnitsButton = new JButton("Release Units");
110 shopPanel.add(scoreboard);
112 shopPanel.add(releaseUnitsButton);
115 resultPanel.add(shopPanel, BorderLayout.SOUTH);
116 return resultPanel;
120 * Creates the MenuBar used by this application.
122 * @return The MenuBar used by this application.
124 private JMenuBar createMenu() {
125 JMenuBar menubar = new JMenuBar();
127 // Menu AntiTD
128 JMenu antiTDMenu = new JMenu("AntiTD");
130 // TODO change name depending on if a game is running or not.
131 this.newGameMenuItem = new JMenuItem("New Game/Restart");
132 antiTDMenu.add(newGameMenuItem);
134 this.pausMenuItem = new JMenuItem("Pause/Reusme");
135 antiTDMenu.add(pausMenuItem);
137 this.quitMenuItem = new JMenuItem("Quit");
138 antiTDMenu.add(quitMenuItem);
140 // Menu Help
141 JMenu helpMenu = new JMenu("Help");
143 this.helpMenuItem = new JMenuItem("Help");
144 helpMenu.add(helpMenuItem);
146 this.aboutMenuItem = new JMenuItem("About");
147 helpMenu.add(aboutMenuItem);
149 // Add menus to menubar
150 menubar.add(antiTDMenu);
151 menubar.add(helpMenu);
152 return menubar;
156 * Returns the Graphics used to update visual information about Agents in
157 * the game.
159 * @return The Graphics used by Agents on the Level.
161 public Graphics getGameGraphics() {
162 return gameGraphics;
165 public String getSelectedUnitType() {
166 return this.unitList.getSelectedValue().toString();
170 * Returns the Graphics used to update visual information about the Background in
171 * the game.
173 * @return The Graphics used by Controller to create the Level background.
175 public Graphics getBackgroundGraphics() {
176 return this.gameBackgroundGraphics;
180 * Updates the backgroundimage used, for example when MapSquare are changed
181 * during gameplay.
183 public void updateBackgroundImage() {
184 this.gameComponent.updateBackgroundImage();
188 * Calls repaint on the GameComponent.
190 public void repaintGame() {
191 gameComponent.repaint();
195 * Update and repaint Pause menu information.
197 public void updatePauseMenu(final String TEXT) {
198 SwingUtilities.invokeLater(new Runnable() {
199 public void run() {
200 //this.newGameMenuItem = new JMenuItem("New Game/Restart");
201 ATDView.this.pausMenuItem.setText(TEXT);
207 * Updates the numbers on the scoreboard
209 public void updateScoreboard() {
210 SwingUtilities.invokeLater(new Runnable() {
211 public void run() {
212 scoreboard.setText("Credit:\t" + model.getCredit() + "\n" +
213 "Score:\t" + model.getScore());
219 * GameComponent is the componenent on which the Level and all Units and
220 * Towers is shown.
222 private class GameComponent extends JComponent {
223 private Image backgroundImage;
224 // TODO: Se if its possible to change to Image
225 private BufferedImage gameImage;
227 private int width;
228 private int height;
230 public GameComponent() {
231 super();
232 this.width = (int) model.getMapDimension().getWidth();
233 this.height = (int) model.getMapDimension().getHeight();
234 this.setPreferredSize(model.getMapDimension());
236 // Background Image
237 this.backgroundImage = model.getMapImage();
239 // Game Image
240 this.gameImage = new BufferedImage(width, height,
241 BufferedImage.TYPE_INT_ARGB);
242 ATDView.this.gameGraphics = gameImage.createGraphics();
245 public void updateBackgroundImage() {
246 this.backgroundImage = model.getMapImage();
250 * Ovverrides standard paintComponent, used to only repaint information
251 * about Unit and Tower information.
253 * @param g The Graphics used to update visual information.
255 @Override
256 public void paintComponent(Graphics g) {
257 logger.fine("paintComponent(...)");
259 g.drawImage(backgroundImage, 0, 0, null);
261 // Should contain updated information from Controller
262 g.drawImage(gameImage, 0, 0, null);
264 // Clear gameImage image with a big transparent rectangle.
265 Color transparent = new Color(0, 0, 0, 0);
266 gameGraphics.setColor(transparent);
267 gameGraphics.setComposite(AlphaComposite.Src);
268 gameGraphics.fill(new Rectangle2D.Double(0, 0, width, height));
272 /* Set Listener methods ******************************/
274 // TODO: Good way to not cast and not be to specific?
275 public void addClosingListener(EventListener el) {
276 this.quitMenuItem.addActionListener((ActionListener) el);
277 this.frame.addWindowListener((WindowListener) el);
280 // Set menu item listeners
281 public void addNewGameMenuItemListener(ActionListener al) {
282 this.newGameMenuItem.addActionListener(al);
285 public void addPauseMenuItemListener(ActionListener al) {
286 this.pausMenuItem.addActionListener(al);
289 public void addHelpMenuItemListener(ActionListener al) {
290 this.helpMenuItem.addActionListener(al);
293 public void addAboutMenuItemListener(ActionListener al) {
294 this.aboutMenuItem.addActionListener(al);
297 // Controls
298 public void addReleaseUnitListener(ActionListener al) {
299 this.releaseUnitsButton.addActionListener(al);
302 public void addMapListener(MouseListener ml) {
303 gameComponent.addMouseListener(ml);