From: Casson S. <cas...@ea...> - 2004-06-27 17:32:37
|
Hi all, I am trying to write some GeoTools code to let the user choose a feature via a mouse click. The first step I presume is to get the coordinates of the mouse click. I have tried a number of configurations using JApplet and JFrame and have been getting results I do not understand. I suspect I am doing something wrong in either GeoTools or Java as opposed to having run into a bug. I believe I have included the pertinent parts of the code below. I can provide complete code if desired. I am using the binary release 5. In both JApplet and JFrame, when I add the StyledMapPane to a container hierarchy with other components the GeoMouseEvent ..getMapCoordinate(pnt), getPixelCoordinate(pnt), and getCoordinate(mapCS,pnt) all return the pixel coordinates and not the map coordinates (NC State Plane in this case). This occurs when I uncomment the lines in Frame1() which a toolbar and button to the contentpane of the JFrame (or JApplet). When these lines are left commented out, or the the mapPane is set as the contentpane, then I get the state plane coordinates. There have been intermediate situations in which the GeoMouseEvent functions initially returned pixel coordinates, and then after a rezise or zoom, would start returning state plane coordinates. I have not been able to recreate this, however. Even when the functions are returning state plane coordinates, the coordinates are relative to the the original pixel to map coordinate transform, and do not reflect any zoom or pan events that have occured. QUESTIONS. Am I missing some initialization step to set up the map coordinates? I have tried setting the coordinate system of the Renderer directly in addition to the StyledMapPane, but this has not helped. Is there some assumption about where the StyledMapPane should be in the container hierarchcy that I am missing? Do I need to mannually reset the transform from pixel to map coordinates after a zoom or pan event? I am using the zoom and pan events built into StyledMapPane. Anything else I am missing? Thanks. Casson -----Beginning of Code------ public class Frame1 extends JFrame implements MouseListener { protected StyledMapPane mapPane;// = new StyledMapPane(); // The display mapPane; protected MapContext map = new DefaultMapContext(); private CoordinateSystem mapCS; // CS defined in setCS; public static void main (String[] args) { JFrame aFrame = new Frame1(); aFrame.setSize(new Dimension(500,500)); aFrame.addWindowListener(new java.awt.event.WindowAdapter(){ public void windowClosing(java.awt.event.WindowEvent e) {System.exit(0);}}); } public Frame1() { super(); mapPane = new StyledMapPane(setCS()); mapPane.setMinimumSize(new Dimension(50,50)); mapPane.setBorder(BorderFactory.createLineBorder(Color.BLUE)); //Add the MouseListener to the MapPane; mapPane.addMouseListener(this); getContentPane().add(mapPane,BorderLayout.CENTER); //If this section is included then all getCoordinate functions // for GeoMouseEvent Return pixel coordinates. // JButton rstBut = new JButton (); // rstBut.setText(""); // JToolBar cToolBar = new JToolBar(); // cToolBar.add(rstBut); // cToolBar.setPreferredSize(new Dimension(300,30)); // getContentPane().add(cToolBar,BorderLayout.NORTH); addMap(); pack(); setVisible(true); } public void addMap() { try { //Create MapContext Add coordinate system; //mapPane.setCoordinateSystem(setCS()); DefaultMapLayer lineLayer = makeLineLayer("dotairport.shp",Color.RED); DefaultMapLayer polyLayer = makePolyLayer("polygons.shp",Color.GRAY); map.addLayer(polyLayer); map.addLayer(lineLayer); mapPane.setMapContext(map); } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); } } -------[skip bunch of stuff]-------- -------Mouse Clicked code----- public void mouseClicked(MouseEvent e) { java.awt.geom.Point2D pnt = null; GeoMouseEvent ge = (GeoMouseEvent) e; String mappnt = ge.getMapCoordinate(pnt).toString(); String pixpnt = ge.getPixelCoordinate(pnt).toString(); String tr = ge.getTextToMap().toString(); String cmp = ge.getComponent().getClass().getName(); String map2pnt = ":"; try { map2pnt = ge.getCoordinate(mapCS,pnt).toString(); } catch (Exception te){ te.printStackTrace(); } System.out.println(map2pnt + " " + cmp + " " + pixpnt + " " + mappnt + " " + tr); } Casson Stallings cas...@ea... |