~ubuntu-branches/ubuntu/precise/triplea/precise

« back to all changes in this revision

Viewing changes to src/util/image/PlacementPicker.java

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2011-11-11 21:40:11 UTC
  • Revision ID: package-import@ubuntu.com-20111111214011-sehf2rwat36o2xqf
Tags: upstream-1.3.2.2
ImportĀ upstreamĀ versionĀ 1.3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
13
 */
 
14
 
 
15
 
 
16
package util.image;
 
17
 
 
18
import games.strategy.ui.Util;
 
19
import games.strategy.util.PointFileReaderWriter;
 
20
 
 
21
import java.awt.*;
 
22
import java.awt.event.*;
 
23
import java.io.*;
 
24
import java.util.*;
 
25
import java.util.List;
 
26
 
 
27
import javax.swing.*;
 
28
 
 
29
 
 
30
public class PlacementPicker extends JFrame
 
31
{
 
32
    private Point  m_currentSquare;
 
33
    private Image  m_image;
 
34
    private JLabel m_location = new JLabel();
 
35
    private Map    m_polygons = new HashMap();
 
36
 
 
37
    private Map<String, List<Point>>    m_placements;
 
38
    private List<Point>   m_currentPlacements;
 
39
    private String m_currentCountry;
 
40
 
 
41
    private static final int PLACE_SIZE = 48;
 
42
 
 
43
 
 
44
    /**
 
45
       main(java.lang.String[])
 
46
       
 
47
       Main program begins here.
 
48
       Asks the user to select the map then runs the
 
49
       the actual placement picker program.
 
50
       
 
51
       @param java.lang.String[] args  the command line arguments
 
52
 
 
53
       @see Picker(java.lang.String) picker
 
54
    */
 
55
    public static void main(String[] args)
 
56
    {
 
57
        System.out.println("Select the map");
 
58
        String mapName = new FileOpen("Select The Map").getPathString();
 
59
        
 
60
        if(mapName != null)
 
61
        { 
 
62
            PlacementPicker picker = new PlacementPicker(mapName);
 
63
            picker.setSize(600,550);
 
64
            picker.setVisible(true);
 
65
        }
 
66
        else {
 
67
            System.out.println("No Image Map Selected. Shutting down.");
 
68
            System.exit(0);
 
69
        }
 
70
 
 
71
    }//end main
 
72
 
 
73
 
 
74
 
 
75
    /**
 
76
       Constructor PlacementPicker(java.lang.String)
 
77
       
 
78
       Setus up all GUI components, initializes variables with
 
79
       default or needed values, and prepares the map for user
 
80
       commands.
 
81
       
 
82
       @param java.lang.String  mapName  name of map file
 
83
    */
 
84
    public PlacementPicker(String mapName)
 
85
    {
 
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)
 
90
        {
 
91
            try
 
92
            {
 
93
                System.out.println("Polygons : " + file.getPath());
 
94
                m_polygons = PointFileReaderWriter.readOneToManyPolygons(new FileInputStream(file.getPath()));
 
95
            }
 
96
            catch (IOException ex1)
 
97
            {
 
98
                ex1.printStackTrace();
 
99
            }
 
100
        }
 
101
        else
 
102
        {
 
103
            try
 
104
            {
 
105
                System.out.println("Select the Polygons file");
 
106
                String polyPath = new FileOpen("Select A Polygon File").getPathString();
 
107
 
 
108
                if(polyPath != null)
 
109
                {
 
110
                    System.out.println("Polygons : "+polyPath);
 
111
                    m_polygons = PointFileReaderWriter.readOneToManyPolygons(new FileInputStream(polyPath));
 
112
                }
 
113
                else
 
114
                {
 
115
                    System.out.println("Polygons file not given. Will run regardless");
 
116
                }
 
117
            }
 
118
            catch (IOException ex1)
 
119
            {
 
120
                ex1.printStackTrace();
 
121
            }
 
122
        }
 
123
 
 
124
        createImage(mapName);
 
125
 
 
126
        JPanel imagePanel = createMainPanel();
 
127
 
 
128
 
 
129
        /*
 
130
           Add a mouse listener to show
 
131
           X : Y coordinates on the lower
 
132
           left corner of the screen.
 
133
        */
 
134
        imagePanel.addMouseMotionListener(
 
135
            new MouseMotionAdapter()
 
136
            {
 
137
                public void mouseMoved(MouseEvent e)
 
138
                {
 
139
                     m_location.setText("x:"+ e.getX() + " y:" + e.getY());
 
140
                     m_currentSquare = new Point(e.getPoint());
 
141
                     repaint();
 
142
                }
 
143
            }
 
144
        );
 
145
 
 
146
 
 
147
        /*
 
148
           Add a mouse listener to monitor
 
149
           for right mouse button being
 
150
           clicked.     
 
151
        */
 
152
        imagePanel.addMouseListener(
 
153
            new MouseAdapter()
 
154
            {
 
155
                public void mouseClicked(MouseEvent e)
 
156
                {
 
157
                    mouseEvent(e.getPoint(), e.isControlDown(), SwingUtilities.isRightMouseButton(e));
 
158
                }
 
159
            }
 
160
        );
 
161
 
 
162
 
 
163
        //set up the image panel size dimensions ...etc
 
164
 
 
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)));
 
168
 
 
169
        //set up the layout manager
 
170
 
 
171
        this.getContentPane().setLayout(new BorderLayout());
 
172
        this.getContentPane().add(new JScrollPane(imagePanel),  BorderLayout.CENTER);
 
173
        this.getContentPane().add(m_location, BorderLayout.SOUTH);
 
174
 
 
175
 
 
176
        //set up the actions
 
177
        
 
178
        Action openAction = new AbstractAction("Load Placements") {
 
179
            public void actionPerformed(ActionEvent event) {
 
180
                loadPlacements();
 
181
            }
 
182
        };
 
183
        openAction.putValue(Action.SHORT_DESCRIPTION, "Load An Existing Placement File");
 
184
 
 
185
 
 
186
        Action saveAction = new AbstractAction("Save Placements") {
 
187
            public void actionPerformed(ActionEvent event) {
 
188
                savePlacements();
 
189
            }
 
190
        };
 
191
        saveAction.putValue(Action.SHORT_DESCRIPTION, "Save The Placements To File");
 
192
 
 
193
 
 
194
        Action exitAction = new AbstractAction("Exit") {
 
195
            public void actionPerformed(ActionEvent event) {
 
196
                System.exit(0);
 
197
            }
 
198
        };
 
199
        exitAction.putValue(Action.SHORT_DESCRIPTION, "Exit The Program");
 
200
 
 
201
        //set up the menu items
 
202
 
 
203
        JMenuItem openItem = new JMenuItem(openAction);
 
204
        openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
 
205
 
 
206
        JMenuItem saveItem = new JMenuItem(saveAction);
 
207
        saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
 
208
 
 
209
        JMenuItem exitItem = new JMenuItem(exitAction);
 
210
        
 
211
        
 
212
        //set up the menu bar
 
213
 
 
214
        JMenuBar menuBar = new JMenuBar();
 
215
        setJMenuBar(menuBar);
 
216
        
 
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);
 
223
 
 
224
        menuBar.add(fileMenu);
 
225
 
 
226
    }//end constructor
 
227
 
 
228
 
 
229
 
 
230
    /**
 
231
       createImage(java.lang.String)
 
232
       
 
233
       creates the image map and makes sure
 
234
       it is properly loaded.
 
235
       
 
236
       @param java.lang.String mapName  the path of image map
 
237
    */
 
238
    private void createImage(String mapName)
 
239
    {
 
240
         m_image = Toolkit.getDefaultToolkit().createImage(mapName);
 
241
 
 
242
         try
 
243
         {
 
244
             Util.ensureImageLoaded(m_image);
 
245
         }
 
246
         catch (InterruptedException ex)
 
247
         {
 
248
             ex.printStackTrace();
 
249
         }
 
250
    }
 
251
 
 
252
 
 
253
 
 
254
    /**
 
255
       javax.swing.JPanel createMainPanel()
 
256
       
 
257
       Creates the main panel and returns
 
258
       a JPanel object.
 
259
       
 
260
       @return javax.swing.JPanel  the panel to return
 
261
    */
 
262
    private JPanel createMainPanel()
 
263
    {
 
264
        JPanel imagePanel = new JPanel()
 
265
        {
 
266
            public void paint(Graphics g)
 
267
            {
 
268
                //super.paint(g);
 
269
                g.drawImage(m_image, 0, 0, this);
 
270
                g.setColor(Color.red);
 
271
 
 
272
                if(m_currentSquare != null)
 
273
                {
 
274
                    g.drawRect(m_currentSquare.x, m_currentSquare.y, PLACE_SIZE, PLACE_SIZE);
 
275
                }
 
276
 
 
277
                if(m_currentPlacements == null)
 
278
                {
 
279
                    return;
 
280
                }
 
281
                
 
282
                Iterator<Point> pointIter = m_currentPlacements.iterator();
 
283
                
 
284
                while (pointIter.hasNext())
 
285
                {
 
286
                    Point item = pointIter.next();
 
287
                    g.fillRect(item.x, item.y, PLACE_SIZE ,PLACE_SIZE);
 
288
                }
 
289
            
 
290
            }//paint
 
291
        };
 
292
        return imagePanel;
 
293
    }
 
294
 
 
295
 
 
296
 
 
297
    /**
 
298
       savePlacements()
 
299
       
 
300
       Saves the placements to disk.
 
301
    */
 
302
    private void savePlacements()
 
303
    {
 
304
        try
 
305
        {
 
306
            String fileName = new FileSave("Where To Save place.txt ?","place.txt").getPathString();
 
307
            
 
308
            if (fileName == null)
 
309
            {
 
310
                return;
 
311
            }
 
312
            
 
313
            FileOutputStream out = new FileOutputStream(fileName);
 
314
            PointFileReaderWriter.writeOneToMany(out, m_placements);
 
315
            out.flush();
 
316
            out.close();
 
317
            System.out.println("Data written to :" + new File(fileName).getCanonicalPath());
 
318
        }
 
319
        catch (FileNotFoundException ex)
 
320
        {
 
321
            ex.printStackTrace();
 
322
        }
 
323
        catch (HeadlessException ex)
 
324
        {
 
325
            ex.printStackTrace();
 
326
        }
 
327
        catch (Exception ex)
 
328
        {
 
329
            ex.printStackTrace();
 
330
        }
 
331
    }
 
332
    
 
333
 
 
334
 
 
335
    /**
 
336
       loadPlacements()
 
337
       
 
338
       Loads a pre-defined file with map placement points.
 
339
    */
 
340
    private void loadPlacements()
 
341
    {
 
342
        try
 
343
        {
 
344
             System.out.println("Load a placement file");
 
345
             String placeName = new FileOpen("Load A Placement File").getPathString();
 
346
 
 
347
             if(placeName == null)
 
348
             {
 
349
                 return;
 
350
             }   
 
351
                 
 
352
             FileInputStream in = new FileInputStream(placeName);
 
353
             m_placements = PointFileReaderWriter.readOneToMany(in);
 
354
             repaint();
 
355
         }
 
356
         catch (FileNotFoundException ex)
 
357
         {
 
358
             ex.printStackTrace();
 
359
         }
 
360
         catch (IOException ex)
 
361
         {
 
362
             ex.printStackTrace();
 
363
         }
 
364
         catch (HeadlessException ex)
 
365
         {
 
366
             ex.printStackTrace();
 
367
         }
 
368
    }
 
369
 
 
370
 
 
371
 
 
372
    /**
 
373
        java.lang.String findTerritoryName(java.awt.Point)
 
374
        
 
375
        Finds a land territory name or
 
376
        some sea zone name.
 
377
        
 
378
        @param java.awt.point p  a point on the map
 
379
    */
 
380
    private String findTerritoryName(Point p)
 
381
    {
 
382
        String seaName = "there be dragons";
 
383
 
 
384
        //try to find a land territory.
 
385
        //sea zones often surround a land territory
 
386
        
 
387
        Iterator keyIter = m_polygons.keySet().iterator();
 
388
        
 
389
        while (keyIter.hasNext())
 
390
        {
 
391
            String name = (String)keyIter.next();
 
392
            Collection polygons = (Collection) m_polygons.get(name);
 
393
            Iterator polyIter = polygons.iterator();
 
394
            
 
395
            while (polyIter.hasNext())
 
396
            {
 
397
                Polygon poly = (Polygon)polyIter.next();
 
398
                
 
399
                if(poly.contains(p))
 
400
                {
 
401
                    if(name.endsWith("Sea Zone") || name.startsWith("Sea Zone"))
 
402
                    {
 
403
                        seaName = name;
 
404
                    }
 
405
                    else
 
406
                    {
 
407
                        return name;
 
408
                    }
 
409
                
 
410
                }//if
 
411
                
 
412
            }//while
 
413
        
 
414
        }//while
 
415
        
 
416
        return seaName;
 
417
    }
 
418
 
 
419
 
 
420
 
 
421
    /**
 
422
       mouseEvent(java.awt.Point, java.lang.boolean, java.lang.boolean)
 
423
       
 
424
       Usage:
 
425
              left button start in territory
 
426
              left button + control, add point
 
427
              right button and ctrl write
 
428
              right button remove last
 
429
       
 
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
 
433
    */
 
434
    private void mouseEvent(Point point, boolean ctrlDown, boolean rightMouse)
 
435
    {
 
436
        if(!rightMouse && !ctrlDown)
 
437
        {
 
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>();
 
442
            else
 
443
                m_currentPlacements = new ArrayList<Point>( m_placements.get(m_currentCountry));
 
444
 
 
445
            JOptionPane.showMessageDialog(this, m_currentCountry);
 
446
        }
 
447
        else if(!rightMouse && ctrlDown)
 
448
        {
 
449
            m_currentPlacements.add(point);
 
450
        }
 
451
        else if (rightMouse && ctrlDown)
 
452
        {
 
453
                //If there isn't an existing hashmap, create one
 
454
                if(m_placements == null)
 
455
                {                       
 
456
                        m_placements = new HashMap<String, List<Point>>();
 
457
                }
 
458
                else
 
459
                {
 
460
                m_placements.put(m_currentCountry, m_currentPlacements);                        
 
461
                }
 
462
            m_currentPlacements = new ArrayList<Point>();
 
463
            System.out.println("done:" + m_currentCountry);
 
464
        }
 
465
        else if (rightMouse)
 
466
        {
 
467
            if(!m_currentPlacements.isEmpty())
 
468
            {
 
469
                m_currentPlacements.remove(m_currentPlacements.size() -1);
 
470
            }
 
471
        }
 
472
 
 
473
        repaint();
 
474
    }
 
475
 
 
476
}//end class PlacementPicker