StartSquare selection, distanceCalc changed in TowerSquare, new level with two StartS...
[AntiTD.git] / src / se / umu / cs / dit06ajnajs / ATDView.java
blobeb31e7b38dc6823e1082ef4d33931fad90253809
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;
15 import javax.swing.JComponent;
16 import javax.swing.JFrame;
17 import javax.swing.JMenu;
18 import javax.swing.JMenuBar;
19 import javax.swing.JMenuItem;
20 import javax.swing.JPanel;
22 import java.util.EventListener;
23 import javax.swing.JButton;
24 import javax.swing.DefaultListModel;
25 import se.umu.cs.dit06ajnajs.agent.AgentPrototypeFactory;
26 import javax.swing.JList;
27 import javax.swing.ListSelectionModel;
29 public class ATDView {
30 private static Logger logger = Logger.getLogger("AntiTD");
32 private ATDModel model;
34 // Components
35 private JFrame frame;
37 private GameComponent gameComponent;
38 private Graphics2D gameGraphics;
39 private Graphics2D gameBackgroundGraphics;
41 // Menu
42 private JMenuItem newGameMenuItem;
43 private JMenuItem quitMenuItem;
44 private JMenuItem helpMenuItem;
45 private JMenuItem aboutMenuItem;
47 // GameController
48 JButton releaseUnitsButton;
50 public ATDView(ATDModel model) {
51 this.model = model;
52 createAndShowGUI();
55 /**
56 * Create and show GUI used by this application.
58 private void createAndShowGUI() {
59 this.gameComponent = new GameComponent();
61 JPanel mainPanel = new JPanel(new BorderLayout());
63 mainPanel.add(createControlPanel(), BorderLayout.EAST);
64 mainPanel.add(createMenu(), BorderLayout.NORTH);
65 mainPanel.add(gameComponent, BorderLayout.CENTER);
67 this.frame = new JFrame();
68 frame.setTitle("AntiTD");
69 frame.add(mainPanel);
70 frame.pack();
71 frame.setVisible(true);
74 /**
75 * Creates the JPanel containg Components to control the game.
77 * @return The JPanel containg Components to control the game.
79 private JPanel createControlPanel() {
80 JPanel resultPanel = new JPanel(new BorderLayout());
82 // JList unitTypes
83 DefaultListModel unitTypesListModel = new DefaultListModel();
84 for (String type : AgentPrototypeFactory.getInstance().getUnitTypes()) {
85 unitTypesListModel.addElement(type);
88 JList unitList= new JList(unitTypesListModel);
89 unitList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
90 resultPanel.add(unitList, BorderLayout.NORTH);
92 // Release Button
93 this.releaseUnitsButton = new JButton("Release Units");
94 resultPanel.add(releaseUnitsButton, BorderLayout.SOUTH);
95 return resultPanel;
98 /**
99 * Creates the MenuBar used by this application.
101 * @return The MenuBar used by this application.
103 private JMenuBar createMenu() {
104 JMenuBar menubar = new JMenuBar();
106 // Menu AntiTD
107 JMenu antiTDMenu = new JMenu("AntiTD");
109 // TODO change name depending on if a game is running or not.
110 this.newGameMenuItem = new JMenuItem("New Game/Restart");
111 antiTDMenu.add(newGameMenuItem);
113 this.quitMenuItem = new JMenuItem("Quit");
114 antiTDMenu.add(quitMenuItem);
116 // Menu Help
117 JMenu helpMenu = new JMenu("Help");
119 this.helpMenuItem = new JMenuItem("Help");
120 helpMenu.add(helpMenuItem);
122 this.aboutMenuItem = new JMenuItem("About");
123 helpMenu.add(aboutMenuItem);
125 // Add menus to menubar
126 menubar.add(antiTDMenu);
127 menubar.add(helpMenu);
128 return menubar;
132 * Returns the Graphics used to update visual information about Agents in
133 * the game.
135 * @return The Graphics used by Agents on the Map.
137 public Graphics getGameGraphics() {
138 return gameGraphics;
142 * Returns the Graphics used to update visual information about the Background in
143 * the game.
145 * @return The Graphics used by Controller to create the Map background.
147 public Graphics getBackgroundGraphics() {
148 return this.gameBackgroundGraphics;
152 * Updates the backgroundimage used, for example when MapSquare are changed
153 * during gameplay.
155 public void updateBackgroundImage() {
156 this.gameComponent.updateBackgroundImage();
160 * Calls repaint on the GameComponent.
162 public void repaintGame() {
163 gameComponent.repaint();
167 * GameComponent is the componenent on which the Map and all Units and
168 * Towers is shown.
170 private class GameComponent extends JComponent {
171 private Image backgroundImage;
172 // TODO: Se if its possible to change to Image
173 private BufferedImage gameImage;
175 private int width;
176 private int height;
178 public GameComponent() {
179 super();
180 this.width = (int) model.getMapDimension().getWidth();
181 this.height = (int) model.getMapDimension().getHeight();
182 this.setPreferredSize(model.getMapDimension());
184 // Background Image
185 this.backgroundImage = model.getMapImage();
187 // Game Image
188 this.gameImage = new BufferedImage(width, height,
189 BufferedImage.TYPE_INT_ARGB);
190 ATDView.this.gameGraphics = gameImage.createGraphics();
193 public void updateBackgroundImage() {
194 this.backgroundImage = this.backgroundImage = model.getMapImage();
198 * Ovverrides standard paintComponent, used to only repaint information
199 * about Unit and Tower information.
201 * @param g The Graphics used to update visual information.
203 @Override
204 public void paintComponent(Graphics g) {
205 logger.fine("paintComponent(...)");
207 g.drawImage(backgroundImage, 0, 0, null);
209 // Should contain updated information from Controller
210 g.drawImage(gameImage, 0, 0, null);
212 // Clear gameImage image with a big transparent rectangle.
213 Color transparent = new Color(0, 0, 0, 0);
214 gameGraphics.setColor(transparent);
215 gameGraphics.setComposite(AlphaComposite.Src);
216 gameGraphics.fill(new Rectangle2D.Double(0, 0, width, height));
220 /* Set Listener methods ******************************/
222 public void addMapListener(MouseListener ml) {
223 gameComponent.addMouseListener(ml);
226 // Set menu item listeners
227 public void addNewGameMenuItemListener(ActionListener al) {
228 this.newGameMenuItem.addActionListener(al);
231 // TODO: Good way to not cast and not be to specific?
232 public void addClosingListener(EventListener el) {
233 this.quitMenuItem.addActionListener((ActionListener) el);
234 this.frame.addWindowListener((WindowListener) el);
237 public void addHelpMenuItemListener(ActionListener al) {
238 this.helpMenuItem.addActionListener(al);
241 public void addAboutMenuItemListener(ActionListener al) {
242 this.aboutMenuItem.addActionListener(al);