1 package se
.umu
.cs
.dit06ajnajs
.util
;
3 import java
.awt
.BorderLayout
;
4 import java
.awt
.Graphics
;
6 import java
.awt
.event
.ActionEvent
;
7 import java
.awt
.event
.ActionListener
;
8 import java
.awt
.event
.MouseAdapter
;
9 import java
.awt
.event
.MouseEvent
;
10 import java
.io
.BufferedWriter
;
12 import java
.io
.FileWriter
;
13 import java
.io
.IOException
;
14 import java
.io
.Writer
;
16 import java
.util
.logging
.Logger
;
17 import javax
.swing
.DefaultListModel
;
18 import javax
.swing
.JButton
;
19 import javax
.swing
.JComponent
;
20 import javax
.swing
.JFrame
;
21 import javax
.swing
.JList
;
22 import javax
.swing
.JPanel
;
23 import javax
.swing
.ListSelectionModel
;
24 import javax
.swing
.event
.ListSelectionEvent
;
25 import javax
.swing
.event
.ListSelectionListener
;
26 import se
.umu
.cs
.dit06ajnajs
.AntiTD
;
27 import se
.umu
.cs
.dit06ajnajs
.level
.Level
;
28 import se
.umu
.cs
.dit06ajnajs
.level
.MapSquare
;
29 import se
.umu
.cs
.dit06ajnajs
.level
.MapSquarePrototypeFactory
;
30 import se
.umu
.cs
.dit06ajnajs
.util
.LevelsXMLOutputter
;
33 * A level editor to make template XML-levels. All information needed to
34 * make a complete level is not implemented.
36 * TODO: unitsToWin, towers, save XML to user specified location.
38 * @author Anton Johansson, dit06ajn@cs.umu.se
41 public class LevelEditor
extends JFrame
{
42 private static Logger logger
= Logger
.getLogger("AntiTD.levelEditor");
44 private MapSquarePrototypeFactory factory
=
45 MapSquarePrototypeFactory
.getInstance();
47 private JComponent mapComponent
;
48 private JList mapSquareList
;
49 private String selectedMapSquareType
;
53 * Initializes the editor. creates and shows a level containing only
56 public LevelEditor() {
61 // Create a matrix containing TowerSquares
62 MapSquare
[][] mapMatrix
= new MapSquare
[cols
][rows
];
63 for (int c
= 0; c
< cols
; c
++) {
64 for (int r
= 0; r
< rows
; r
++) {
65 mapMatrix
[c
][r
] = factory
.createMapSquare("TowerSquare",
66 c
* AntiTD
.SQUARE_SIZE
,
67 r
* AntiTD
.SQUARE_SIZE
);
71 this.level
= new Level("new level", mapMatrix
);
76 * Creates and show the GUI of this frame.
78 public void createAndShowGUI() {
79 JPanel mainPanel
= new JPanel(new BorderLayout());
80 this.mapComponent
= new MapComponent();
81 mapComponent
.addMouseListener(new MapListener());
82 mainPanel
.add(mapComponent
, BorderLayout
.CENTER
);
84 JPanel controlsPanel
= new JPanel(new BorderLayout());
87 DefaultListModel mapSquaresListModel
= new DefaultListModel();
88 for (String type
: factory
.getMapSquareTypes()) {
89 mapSquaresListModel
.addElement(type
);
92 this.mapSquareList
= new JList(mapSquaresListModel
);
93 mapSquareList
.setSelectionMode(ListSelectionModel
.SINGLE_SELECTION
);
94 mapSquareList
.addListSelectionListener(new MapSquareListListener());
96 controlsPanel
.add(mapSquareList
, BorderLayout
.NORTH
);
99 JButton saveButton
= new JButton("Save Level");
100 saveButton
.setActionCommand("saveMap");
101 saveButton
.addActionListener(new ActionListener() {
102 public void actionPerformed(ActionEvent ae
) {
103 System
.out
.println("Dude");
105 URL dirURL
= this.getClass().getResource("/resources/");
106 // TODO: fix outFile path and name
107 File outFile
= new File(dirURL
.getPath(), "anton-test.xml");
109 Writer writer
= new BufferedWriter(new FileWriter(outFile
));
111 LevelsXMLOutputter
.outputMap(level
, writer
);
112 System
.out
.println("Wrote file");
113 } catch (IOException e
) {
114 // TODO - fix error message
119 controlsPanel
.add(saveButton
, BorderLayout
.SOUTH
);
122 mainPanel
.add(controlsPanel
, BorderLayout
.EAST
);
126 this.setTitle("MapEditor");
127 this.setVisible(true);
131 * Inner class that represent and displays the current Map.
133 private class MapComponent
extends JComponent
{
134 public MapComponent() {
136 this.setPreferredSize(level
.getDimension());
140 public void paintComponent(Graphics g
) {
141 logger
.info("MapComponent paintComponent()");
142 Image img
= level
.getMapImage();
143 g
.drawImage(img
, 0, 0, null);
148 * Listens on the Map and creates new MapSquares on clicked tiles.
150 private class MapListener
extends MouseAdapter
{
152 public void mouseClicked(MouseEvent me
) {
155 MapSquare square
= level
.getMapSquareAtPoint(x
, y
);
156 logger
.info("Mouse clicked @ (" + x
+ ", " + y
+ ")");
157 logger
.info("MapSquare @ " + square
);
158 logger
.info("Selected MapSquareType: " + selectedMapSquareType
);
159 MapSquare newSquare
= factory
.createMapSquare(selectedMapSquareType
,
162 level
.setMapSquareAtPoint(square
.getX(), square
.getY(), newSquare
);
163 mapComponent
.repaint();
168 * An inner class that handles changes in MapSquare list.
170 private class MapSquareListListener
implements ListSelectionListener
{
171 public void valueChanged(ListSelectionEvent e
) {
172 if (e
.getValueIsAdjusting() == false) {
173 selectedMapSquareType
= mapSquareList
.getSelectedValue().toString();
181 * @param args Is not used.
183 public static void main(String
[] args
) {
185 Logger
.getLogger("AntiTD.levelEditor").setLevel(java
.util
.logging
.Level
.INFO
);