Exemplo n.º 1
0
 /**
  * loadPlacements()
  *
  * <p>Loads a pre-defined file with map placement points.
  */
 private void loadPlacements() {
   try {
     System.out.println("Load a placement file");
     final String placeName =
         new FileOpen("Load A Placement File", s_mapFolderLocation, ".txt").getPathString();
     if (placeName == null) {
       return;
     }
     final FileInputStream in = new FileInputStream(placeName);
     m_placements = PointFileReaderWriter.readOneToMany(in);
     repaint();
   } catch (final FileNotFoundException ex) {
     ex.printStackTrace();
   } catch (final IOException ex) {
     ex.printStackTrace();
   } catch (final HeadlessException ex) {
     ex.printStackTrace();
   }
 }
Exemplo n.º 2
0
 /**
  * savePlacements()
  *
  * <p>Saves the placements to disk.
  */
 private void savePlacements() {
   try {
     final String fileName =
         new FileSave("Where To Save place.txt ?", "place.txt", s_mapFolderLocation)
             .getPathString();
     if (fileName == null) {
       return;
     }
     final FileOutputStream out = new FileOutputStream(fileName);
     PointFileReaderWriter.writeOneToMany(out, m_placements);
     out.flush();
     out.close();
     System.out.println("Data written to :" + new File(fileName).getCanonicalPath());
   } catch (final FileNotFoundException ex) {
     ex.printStackTrace();
   } catch (final HeadlessException ex) {
     ex.printStackTrace();
   } catch (final Exception ex) {
     ex.printStackTrace();
   }
 }
Exemplo n.º 3
0
  /**
   * Constructor PlacementPicker(java.lang.String)
   *
   * <p>Setus up all GUI components, initializes variables with default or needed values, and
   * prepares the map for user commands.
   *
   * @param java .lang.String mapName name of map file
   */
  public PlacementPicker(final String mapName) {
    super("Placement Picker");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    if (!placeDimensionsSet) {
      try {
        File file = null;
        if (s_mapFolderLocation != null && s_mapFolderLocation.exists())
          file = new File(s_mapFolderLocation, "map.properties");
        if (file == null || !file.exists())
          file = new File(new File(mapName).getParent() + File.separator + "map.properties");
        if (file.exists()) {
          double scale = unit_zoom_percent;
          int width = unit_width;
          int height = unit_height;
          boolean found = false;
          final String scaleProperty = MapData.PROPERTY_UNITS_SCALE + "=";
          final String widthProperty = MapData.PROPERTY_UNITS_WIDTH + "=";
          final String heightProperty = MapData.PROPERTY_UNITS_HEIGHT + "=";

          final FileReader reader = new FileReader(file);
          final LineNumberReader reader2 = new LineNumberReader(reader);
          int i = 0;
          while (true) {
            reader2.setLineNumber(i);
            final String line = reader2.readLine();
            if (line == null) break;
            if (line.contains(scaleProperty)) {
              try {
                scale =
                    Double.parseDouble(
                        line.substring(line.indexOf(scaleProperty) + scaleProperty.length())
                            .trim());
                found = true;
              } catch (final NumberFormatException ex) {
              }
            }
            if (line.contains(widthProperty)) {
              try {
                width =
                    Integer.parseInt(
                        line.substring(line.indexOf(widthProperty) + widthProperty.length())
                            .trim());
                found = true;
              } catch (final NumberFormatException ex) {
              }
            }
            if (line.contains(heightProperty)) {
              try {
                height =
                    Integer.parseInt(
                        line.substring(line.indexOf(heightProperty) + heightProperty.length())
                            .trim());
                found = true;
              } catch (final NumberFormatException ex) {
              }
            }
          }
          reader2.close();
          i++;
          if (found) {
            final int result =
                JOptionPane.showConfirmDialog(
                    new JPanel(),
                    "A map.properties file was found in the map's folder, "
                        + "\r\n do you want to use the file to supply the info for the placement box size? "
                        + "\r\n Zoom = "
                        + scale
                        + ",  Width = "
                        + width
                        + ",  Height = "
                        + height
                        + ",    Result = ("
                        + ((int) (scale * width))
                        + "x"
                        + ((int) (scale * height))
                        + ")",
                    "File Suggestion",
                    1);
            // if (result == 2)
            // return;
            if (result == 0) {
              unit_zoom_percent = scale;
              PLACEWIDTH = (int) (unit_zoom_percent * width);
              PLACEHEIGHT = (int) (unit_zoom_percent * height);
              placeDimensionsSet = true;
            }
          }
        }
      } catch (final Exception ex) {
      }
    }
    if (!placeDimensionsSet
        || JOptionPane.showConfirmDialog(
                new JPanel(),
                "Placement Box Size already set ("
                    + PLACEWIDTH
                    + "x"
                    + PLACEHEIGHT
                    + "), "
                    + "do you wish to continue with this?\r\nSelect Yes to continue, Select No to override and change the size.",
                "Placement Box Size",
                JOptionPane.YES_NO_OPTION)
            == 1) {
      try {
        final String result = getUnitsScale();
        try {
          unit_zoom_percent = Double.parseDouble(result.toLowerCase());
        } catch (final NumberFormatException ex) {
        }
        final String width =
            JOptionPane.showInputDialog(
                null,
                "Enter the unit's image width in pixels (unscaled / without zoom).\r\n(e.g. 48)");
        if (width != null) {
          try {
            PLACEWIDTH = (int) (unit_zoom_percent * Integer.parseInt(width));
          } catch (final NumberFormatException ex) {
          }
        }
        final String height =
            JOptionPane.showInputDialog(
                null,
                "Enter the unit's image height in pixels (unscaled / without zoom).\r\n(e.g. 48)");
        if (height != null) {
          try {
            PLACEHEIGHT = (int) (unit_zoom_percent * Integer.parseInt(height));
          } catch (final NumberFormatException ex) {
          }
        }
        placeDimensionsSet = true;
      } catch (final Exception ex) {
      }
    }

    File file = null;
    if (s_mapFolderLocation != null && s_mapFolderLocation.exists())
      file = new File(s_mapFolderLocation, "polygons.txt");
    if (file == null || !file.exists())
      file = new File(new File(mapName).getParent() + File.separator + "polygons.txt");
    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) {
      try {
        System.out.println("Polygons : " + file.getPath());
        m_polygons =
            PointFileReaderWriter.readOneToManyPolygons(new FileInputStream(file.getPath()));
      } catch (final IOException ex1) {
        ex1.printStackTrace();
      }
    } else {
      try {
        System.out.println("Select the Polygons file");
        final String polyPath =
            new FileOpen("Select A Polygon File", s_mapFolderLocation, ".txt").getPathString();
        if (polyPath != null) {
          System.out.println("Polygons : " + polyPath);
          m_polygons = PointFileReaderWriter.readOneToManyPolygons(new FileInputStream(polyPath));
        } else {
          System.out.println("Polygons file not given. Will run regardless");
        }
      } catch (final IOException ex1) {
        ex1.printStackTrace();
      }
    }
    createImage(mapName);
    final JPanel imagePanel = createMainPanel();
    /*
    Add a mouse listener to show
    X : Y coordinates on the lower
    left corner of the screen.
    */
    imagePanel.addMouseMotionListener(
        new MouseMotionAdapter() {
          @Override
          public void mouseMoved(final MouseEvent e) {
            m_location.setText("x:" + e.getX() + " y:" + e.getY());
            m_currentSquare = new Point(e.getPoint());
            repaint();
          }
        });
    /*
       Add a mouse listener to monitor
    for right mouse button being
    clicked.
    */
    imagePanel.addMouseListener(
        new MouseAdapter() {
          @Override
          public void mouseClicked(final MouseEvent e) {
            mouseEvent(
                e.getPoint(),
                e.isControlDown() || e.isShiftDown(),
                SwingUtilities.isRightMouseButton(e));
          }
        });
    // set up the image panel size dimensions ...etc
    imagePanel.setMinimumSize(new Dimension(m_image.getWidth(this), m_image.getHeight(this)));
    imagePanel.setPreferredSize(new Dimension(m_image.getWidth(this), m_image.getHeight(this)));
    imagePanel.setMaximumSize(new Dimension(m_image.getWidth(this), m_image.getHeight(this)));
    // set up the layout manager
    this.getContentPane().setLayout(new BorderLayout());
    this.getContentPane().add(new JScrollPane(imagePanel), BorderLayout.CENTER);
    this.getContentPane().add(m_location, BorderLayout.SOUTH);
    // set up the actions
    final Action openAction =
        new AbstractAction("Load Placements") {
          private static final long serialVersionUID = -2894085191455411106L;

          public void actionPerformed(final ActionEvent event) {
            loadPlacements();
          }
        };
    openAction.putValue(Action.SHORT_DESCRIPTION, "Load An Existing Placement File");
    final Action saveAction =
        new AbstractAction("Save Placements") {
          private static final long serialVersionUID = -3341738809601318716L;

          public void actionPerformed(final ActionEvent event) {
            savePlacements();
          }
        };
    saveAction.putValue(Action.SHORT_DESCRIPTION, "Save The Placements To File");
    final Action exitAction =
        new AbstractAction("Exit") {
          private static final long serialVersionUID = -9093426903644867897L;

          public void actionPerformed(final ActionEvent event) {
            System.exit(0);
          }
        };
    exitAction.putValue(Action.SHORT_DESCRIPTION, "Exit The Program");
    // set up the menu items
    final JMenuItem openItem = new JMenuItem(openAction);
    openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
    final JMenuItem saveItem = new JMenuItem(saveAction);
    saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
    final JMenuItem exitItem = new JMenuItem(exitAction);
    // set up the menu bar
    final JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);
    final JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic('F');
    fileMenu.add(openItem);
    fileMenu.add(saveItem);
    fileMenu.addSeparator();
    fileMenu.add(exitItem);
    s_showAllMode = false;
    s_showOverflowMode = false;
    s_showIncompleteMode = false;
    s_incompleteNum = 1;
    showAllModeItem = new JCheckBoxMenuItem("Show All Placements Mode", false);
    showAllModeItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(final ActionEvent event) {
            s_showAllMode = showAllModeItem.getState();
            repaint();
          }
        });
    showOverflowModeItem = new JCheckBoxMenuItem("Show Overflow Mode", false);
    showOverflowModeItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(final ActionEvent event) {
            s_showOverflowMode = showOverflowModeItem.getState();
            repaint();
          }
        });
    showIncompleteModeItem = new JCheckBoxMenuItem("Show Incomplete Placements Mode", false);
    showIncompleteModeItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(final ActionEvent event) {
            if (showIncompleteModeItem.getState()) {
              final String num =
                  JOptionPane.showInputDialog(
                      null,
                      "Enter the minimum number of placements each territory must have.\r\n(examples: 1, 4, etc.)");
              try {
                s_incompleteNum = Math.max(1, Math.min(50, Integer.parseInt(num)));
              } catch (final Exception ex) {
                s_incompleteNum = 1;
              }
            }
            s_showIncompleteMode = showIncompleteModeItem.getState();
            repaint();
          }
        });
    final JMenu editMenu = new JMenu("Edit");
    editMenu.setMnemonic('E');
    editMenu.add(showAllModeItem);
    editMenu.add(showOverflowModeItem);
    editMenu.add(showIncompleteModeItem);
    menuBar.add(fileMenu);
    menuBar.add(editMenu);
  } // end constructor
Exemplo n.º 4
0
  /**
   * Constructor CenterPicker(java.lang.String) Setus up all GUI components, initializes variables
   * with default or needed values, and prepares the map for user commands.
   *
   * @param java .lang.String mapName name of map file
   */
  public CenterPicker(final String mapName) {
    super("Center Picker");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    File file = null;
    if (s_mapFolderLocation != null && s_mapFolderLocation.exists()) {
      file = new File(s_mapFolderLocation, "polygons.txt");
    }
    if (file == null || !file.exists()) {
      file = new File(new File(mapName).getParent() + File.separator + "polygons.txt");
    }
    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 names?",
                "File Suggestion",
                1)
            == 0) {
      try {
        m_polygons =
            PointFileReaderWriter.readOneToManyPolygons(new FileInputStream(file.getPath()));
      } catch (final IOException ex1) {
        System.out.println("Something wrong with your Polygons file: " + ex1);
        ex1.printStackTrace();
      }
    } else {
      try {
        final String polyPath =
            new FileOpen("Select A Polygon File", s_mapFolderLocation, ".txt").getPathString();
        if (polyPath != null) {
          m_polygons = PointFileReaderWriter.readOneToManyPolygons(new FileInputStream(polyPath));
        }
      } catch (final IOException ex1) {
        System.out.println("Something wrong with your Polygons file: " + ex1);
        ex1.printStackTrace();
      }
    }
    createImage(mapName);
    final JPanel imagePanel = createMainPanel();
    /*
     * Add a mouse listener to show
     * X : Y coordinates on the lower
     * left corner of the screen.
     */
    imagePanel.addMouseMotionListener(
        new MouseMotionAdapter() {
          @Override
          public void mouseMoved(final MouseEvent e) {
            m_location.setText("x:" + e.getX() + " y:" + e.getY());
          }
        });
    // Add a mouse listener to monitor for right mouse button being clicked.
    imagePanel.addMouseListener(
        new MouseAdapter() {
          @Override
          public void mouseClicked(final MouseEvent e) {
            mouseEvent(e.getPoint(), SwingUtilities.isRightMouseButton(e));
          }
        });
    // set up the image panel size dimensions ...etc
    imagePanel.setMinimumSize(new Dimension(m_image.getWidth(this), m_image.getHeight(this)));
    imagePanel.setPreferredSize(new Dimension(m_image.getWidth(this), m_image.getHeight(this)));
    imagePanel.setMaximumSize(new Dimension(m_image.getWidth(this), m_image.getHeight(this)));
    // set up the layout manager
    this.getContentPane().setLayout(new BorderLayout());
    this.getContentPane().add(new JScrollPane(imagePanel), BorderLayout.CENTER);
    this.getContentPane().add(m_location, BorderLayout.SOUTH);
    // set up the actions
    final Action openAction =
        new AbstractAction("Load Centers") {
          private static final long serialVersionUID = 2712234474452114083L;

          @Override
          public void actionPerformed(final ActionEvent event) {
            loadCenters();
          }
        };
    openAction.putValue(Action.SHORT_DESCRIPTION, "Load An Existing Center Points File");
    final Action saveAction =
        new AbstractAction("Save Centers") {
          private static final long serialVersionUID = -4519036149978621171L;

          @Override
          public void actionPerformed(final ActionEvent event) {
            saveCenters();
          }
        };
    saveAction.putValue(Action.SHORT_DESCRIPTION, "Save The Center Points To File");
    final Action exitAction =
        new AbstractAction("Exit") {
          private static final long serialVersionUID = -5631457890653630218L;

          @Override
          public void actionPerformed(final ActionEvent event) {
            System.exit(0);
          }
        };
    exitAction.putValue(Action.SHORT_DESCRIPTION, "Exit The Program");
    // set up the menu items
    final JMenuItem openItem = new JMenuItem(openAction);
    openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
    final JMenuItem saveItem = new JMenuItem(saveAction);
    saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
    final JMenuItem exitItem = new JMenuItem(exitAction);
    // set up the menu bar
    final JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);
    final JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic('F');
    fileMenu.add(openItem);
    fileMenu.add(saveItem);
    fileMenu.addSeparator();
    fileMenu.add(exitItem);
    menuBar.add(fileMenu);
  } // end constructor