/** * Set the view of the scrollable picture to show the map location * * @param mapLocation "(x, y)" */ public void setView(String mapLocation) { int x = Integer.parseInt(mapLocation.substring(1, mapLocation.indexOf(','))); int y = Integer.parseInt( mapLocation.substring(mapLocation.indexOf(", ") + 2, mapLocation.length() - 1)); Point point = new Point(x - (mapScrollPane.getWidth() / 2), y - (mapScrollPane.getHeight() / 2)); map.showLocation(true, x, y); mapScrollPane.getViewport().setViewPosition(point); }
/** * Create plot info panel * * @return panel */ private JPanel createMapPanel() { JPanel panel = new JPanel(new BorderLayout(), true); // create an raised, etched, titled border Border etchedBorder = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED); TitledBorder titledBorder = BorderFactory.createTitledBorder(etchedBorder, "Map"); titledBorder.setTitleJustification(TitledBorder.LEFT); panel.setBorder(titledBorder); // load image java.net.URL imgURL = getClass().getResource("/resources/map.png"); map = new ScrollablePicture(new ImageIcon(imgURL), 10); // map = new ScrollablePicture(new ImageIcon("resources/map.png"), 10); mapScrollPane = new JScrollPane(map); mapScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); mapScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); map.addMouseListener( new MouseListener() { /** * Get the coordinates of a mouse click event * * @param e MouseEvent */ @Override public void mouseClicked(MouseEvent e) { if (cemeteryPlotterFrame.cemeteryPlotterPlot.isEditable()) cemeteryPlotterFrame.cemeteryPlotterPlot.setMapLocationField(e.getPoint()); } @Override public void mousePressed(MouseEvent e) {} @Override public void mouseReleased(MouseEvent e) {} @Override public void mouseEntered(MouseEvent e) { if (cemeteryPlotterFrame.cemeteryPlotterPlot.isEditable()) mapScrollPane.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); } @Override public void mouseExited(MouseEvent e) {} }); // add map scroll pane to main panel panel.add(mapScrollPane, BorderLayout.CENTER); return panel; }
/** Clear map data by hiding the location indicator */ public void clearMapData() { map.showLocation(false, 0, 0); }