2
* This program is free software; you can redistribute it and/or modify
3
* it under the terms of the GNU General Public License as published by
4
* the Free Software Foundation; either version 2 of the License, or
5
* (at your option) any later version.
6
* This program is distributed in the hope that it will be useful,
7
* but WITHOUT ANY WARRANTY; without even the implied warranty of
8
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9
* GNU General Public License for more details.
10
* You should have received a copy of the GNU General Public License
11
* along with this program; if not, write to the Free Software
12
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
import games.strategy.ui.Util;
19
import games.strategy.util.PointFileReaderWriter;
22
import java.awt.event.*;
25
import java.util.List;
30
public class PlacementPicker extends JFrame
32
private Point m_currentSquare;
33
private Image m_image;
34
private JLabel m_location = new JLabel();
35
private Map m_polygons = new HashMap();
37
private Map<String, List<Point>> m_placements;
38
private List<Point> m_currentPlacements;
39
private String m_currentCountry;
41
private static final int PLACE_SIZE = 48;
45
main(java.lang.String[])
47
Main program begins here.
48
Asks the user to select the map then runs the
49
the actual placement picker program.
51
@param java.lang.String[] args the command line arguments
53
@see Picker(java.lang.String) picker
55
public static void main(String[] args)
57
System.out.println("Select the map");
58
String mapName = new FileOpen("Select The Map").getPathString();
62
PlacementPicker picker = new PlacementPicker(mapName);
63
picker.setSize(600,550);
64
picker.setVisible(true);
67
System.out.println("No Image Map Selected. Shutting down.");
76
Constructor PlacementPicker(java.lang.String)
78
Setus up all GUI components, initializes variables with
79
default or needed values, and prepares the map for user
82
@param java.lang.String mapName name of map file
84
public PlacementPicker(String mapName)
86
super("Placement Picker");
87
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
88
File file = new File(new File(mapName).getParent() + File.pathSeparator + "polygons.txt");
89
if(file.exists() && JOptionPane.showConfirmDialog(new JPanel(), "A polygons.txt file was found in the map's folder, do you want to use the file to supply the territories?", "File Suggestion", 1) == 0)
93
System.out.println("Polygons : " + file.getPath());
94
m_polygons = PointFileReaderWriter.readOneToManyPolygons(new FileInputStream(file.getPath()));
96
catch (IOException ex1)
98
ex1.printStackTrace();
105
System.out.println("Select the Polygons file");
106
String polyPath = new FileOpen("Select A Polygon File").getPathString();
110
System.out.println("Polygons : "+polyPath);
111
m_polygons = PointFileReaderWriter.readOneToManyPolygons(new FileInputStream(polyPath));
115
System.out.println("Polygons file not given. Will run regardless");
118
catch (IOException ex1)
120
ex1.printStackTrace();
124
createImage(mapName);
126
JPanel imagePanel = createMainPanel();
130
Add a mouse listener to show
131
X : Y coordinates on the lower
132
left corner of the screen.
134
imagePanel.addMouseMotionListener(
135
new MouseMotionAdapter()
137
public void mouseMoved(MouseEvent e)
139
m_location.setText("x:"+ e.getX() + " y:" + e.getY());
140
m_currentSquare = new Point(e.getPoint());
148
Add a mouse listener to monitor
149
for right mouse button being
152
imagePanel.addMouseListener(
155
public void mouseClicked(MouseEvent e)
157
mouseEvent(e.getPoint(), e.isControlDown(), SwingUtilities.isRightMouseButton(e));
163
//set up the image panel size dimensions ...etc
165
imagePanel.setMinimumSize(new Dimension( m_image.getWidth(this), m_image.getHeight(this)));
166
imagePanel.setPreferredSize(new Dimension( m_image.getWidth(this), m_image.getHeight(this)));
167
imagePanel.setMaximumSize(new Dimension( m_image.getWidth(this), m_image.getHeight(this)));
169
//set up the layout manager
171
this.getContentPane().setLayout(new BorderLayout());
172
this.getContentPane().add(new JScrollPane(imagePanel), BorderLayout.CENTER);
173
this.getContentPane().add(m_location, BorderLayout.SOUTH);
178
Action openAction = new AbstractAction("Load Placements") {
179
public void actionPerformed(ActionEvent event) {
183
openAction.putValue(Action.SHORT_DESCRIPTION, "Load An Existing Placement File");
186
Action saveAction = new AbstractAction("Save Placements") {
187
public void actionPerformed(ActionEvent event) {
191
saveAction.putValue(Action.SHORT_DESCRIPTION, "Save The Placements To File");
194
Action exitAction = new AbstractAction("Exit") {
195
public void actionPerformed(ActionEvent event) {
199
exitAction.putValue(Action.SHORT_DESCRIPTION, "Exit The Program");
201
//set up the menu items
203
JMenuItem openItem = new JMenuItem(openAction);
204
openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
206
JMenuItem saveItem = new JMenuItem(saveAction);
207
saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
209
JMenuItem exitItem = new JMenuItem(exitAction);
212
//set up the menu bar
214
JMenuBar menuBar = new JMenuBar();
215
setJMenuBar(menuBar);
217
JMenu fileMenu = new JMenu("File");
218
fileMenu.setMnemonic('F');
219
fileMenu.add(openItem);
220
fileMenu.add(saveItem);
221
fileMenu.addSeparator();
222
fileMenu.add(exitItem);
224
menuBar.add(fileMenu);
231
createImage(java.lang.String)
233
creates the image map and makes sure
234
it is properly loaded.
236
@param java.lang.String mapName the path of image map
238
private void createImage(String mapName)
240
m_image = Toolkit.getDefaultToolkit().createImage(mapName);
244
Util.ensureImageLoaded(m_image);
246
catch (InterruptedException ex)
248
ex.printStackTrace();
255
javax.swing.JPanel createMainPanel()
257
Creates the main panel and returns
260
@return javax.swing.JPanel the panel to return
262
private JPanel createMainPanel()
264
JPanel imagePanel = new JPanel()
266
public void paint(Graphics g)
269
g.drawImage(m_image, 0, 0, this);
270
g.setColor(Color.red);
272
if(m_currentSquare != null)
274
g.drawRect(m_currentSquare.x, m_currentSquare.y, PLACE_SIZE, PLACE_SIZE);
277
if(m_currentPlacements == null)
282
Iterator<Point> pointIter = m_currentPlacements.iterator();
284
while (pointIter.hasNext())
286
Point item = pointIter.next();
287
g.fillRect(item.x, item.y, PLACE_SIZE ,PLACE_SIZE);
300
Saves the placements to disk.
302
private void savePlacements()
306
String fileName = new FileSave("Where To Save place.txt ?","place.txt").getPathString();
308
if (fileName == null)
313
FileOutputStream out = new FileOutputStream(fileName);
314
PointFileReaderWriter.writeOneToMany(out, m_placements);
317
System.out.println("Data written to :" + new File(fileName).getCanonicalPath());
319
catch (FileNotFoundException ex)
321
ex.printStackTrace();
323
catch (HeadlessException ex)
325
ex.printStackTrace();
329
ex.printStackTrace();
338
Loads a pre-defined file with map placement points.
340
private void loadPlacements()
344
System.out.println("Load a placement file");
345
String placeName = new FileOpen("Load A Placement File").getPathString();
347
if(placeName == null)
352
FileInputStream in = new FileInputStream(placeName);
353
m_placements = PointFileReaderWriter.readOneToMany(in);
356
catch (FileNotFoundException ex)
358
ex.printStackTrace();
360
catch (IOException ex)
362
ex.printStackTrace();
364
catch (HeadlessException ex)
366
ex.printStackTrace();
373
java.lang.String findTerritoryName(java.awt.Point)
375
Finds a land territory name or
378
@param java.awt.point p a point on the map
380
private String findTerritoryName(Point p)
382
String seaName = "there be dragons";
384
//try to find a land territory.
385
//sea zones often surround a land territory
387
Iterator keyIter = m_polygons.keySet().iterator();
389
while (keyIter.hasNext())
391
String name = (String)keyIter.next();
392
Collection polygons = (Collection) m_polygons.get(name);
393
Iterator polyIter = polygons.iterator();
395
while (polyIter.hasNext())
397
Polygon poly = (Polygon)polyIter.next();
401
if(name.endsWith("Sea Zone") || name.startsWith("Sea Zone"))
422
mouseEvent(java.awt.Point, java.lang.boolean, java.lang.boolean)
425
left button start in territory
426
left button + control, add point
427
right button and ctrl write
428
right button remove last
430
@param java.awt.Point point a point clicked by mouse
431
@param java.lang.boolean ctrlDown true if ctrl key was hit
432
@param java.lang.boolean rightMouse true if the right mouse button was hit
434
private void mouseEvent(Point point, boolean ctrlDown, boolean rightMouse)
436
if(!rightMouse && !ctrlDown)
438
m_currentCountry = findTerritoryName(point);
439
//If there isn't an existing array, create one
440
if(m_placements == null || m_placements.get(m_currentCountry) == null)
441
m_currentPlacements = new ArrayList<Point>();
443
m_currentPlacements = new ArrayList<Point>( m_placements.get(m_currentCountry));
445
JOptionPane.showMessageDialog(this, m_currentCountry);
447
else if(!rightMouse && ctrlDown)
449
m_currentPlacements.add(point);
451
else if (rightMouse && ctrlDown)
453
//If there isn't an existing hashmap, create one
454
if(m_placements == null)
456
m_placements = new HashMap<String, List<Point>>();
460
m_placements.put(m_currentCountry, m_currentPlacements);
462
m_currentPlacements = new ArrayList<Point>();
463
System.out.println("done:" + m_currentCountry);
467
if(!m_currentPlacements.isEmpty())
469
m_currentPlacements.remove(m_currentPlacements.size() -1);
476
}//end class PlacementPicker