1 package se
.umu
.cs
.dit06ajnajs
;
3 import java
.awt
.AlphaComposite
;
4 import java
.awt
.BorderLayout
;
6 import java
.awt
.Graphics2D
;
7 import java
.awt
.Graphics
;
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
;
37 private GameComponent gameComponent
;
38 private Graphics2D gameGraphics
;
39 private Graphics2D gameBackgroundGraphics
;
42 private JMenuItem newGameMenuItem
;
43 private JMenuItem quitMenuItem
;
44 private JMenuItem helpMenuItem
;
45 private JMenuItem aboutMenuItem
;
48 JButton releaseUnitsButton
;
50 public ATDView(ATDModel model
) {
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");
71 frame
.setVisible(true);
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());
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
);
93 this.releaseUnitsButton
= new JButton("Release Units");
94 resultPanel
.add(releaseUnitsButton
, BorderLayout
.SOUTH
);
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();
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
);
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
);
132 * Returns the Graphics used to update visual information about Agents in
135 * @return The Graphics used by Agents on the Map.
137 public Graphics
getGameGraphics() {
142 * Returns the Graphics used to update visual information about the Background in
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
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
170 private class GameComponent
extends JComponent
{
171 private Image backgroundImage
;
172 // TODO: Se if its possible to change to Image
173 private BufferedImage gameImage
;
178 public GameComponent() {
180 this.width
= (int) model
.getMapDimension().getWidth();
181 this.height
= (int) model
.getMapDimension().getHeight();
182 this.setPreferredSize(model
.getMapDimension());
185 this.backgroundImage
= model
.getMapImage();
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.
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
);