Revision 20900 trunk/extensions/ext3Dgui/src/com/iver/ai2/gvsig3dgui/behavior/RectangleBehavior3D.java

View differences:

RectangleBehavior3D.java
7 7
import java.awt.geom.Point2D;
8 8
import java.awt.geom.Rectangle2D;
9 9

  
10
import org.apache.log4j.Logger;
11
import org.gvsig.osgvp.Vec3;
12
import org.gvsig.osgvp.exceptions.OSGVPException;
13
import org.gvsig.osgvp.exceptions.node.NodeException;
14
import org.gvsig.osgvp.features.SelectionRectangle;
15
import org.gvsig.osgvp.features.Polygon.PolygonType;
16
import org.gvsig.osgvp.planets.PlanetViewer;
17
import org.gvsig.osgvp.viewer.Intersections;
18

  
19
import com.iver.ai2.gvsig3d.map3d.GraphicLayer3D;
10 20
import com.iver.ai2.gvsig3d.map3d.MapContext3D;
11 21
import com.iver.andami.messages.NotificationManager;
12 22
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
......
14 24
import com.iver.cit.gvsig.fmap.tools.Events.RectangleEvent;
15 25
import com.iver.cit.gvsig.fmap.tools.Listeners.RectangleListener;
16 26

  
17
import es.upv.ai2.osgvp.Vec3;
18
import es.upv.ai2.osgvp.features.SelectionRectangle;
19
import es.upv.ai2.osgvp.features.Polygon.PolygonType;
20
import es.upv.ai2.osgvp.planets.PlanetViewer;
21
import es.upv.ai2.osgvp.viewer.Intersections;
27
public class RectangleBehavior3D extends RectangleBehavior {
22 28

  
23
public class RectangleBehavior3D extends RectangleBehavior {
24
	
25 29
	private Point2D m_FirstPoint;
26 30
	private Point2D m_LastPoint;
27 31
	private RectangleListener listener;
......
29 33
	private int height;
30 34
	private int width;
31 35

  
36
	private static Logger logger = Logger.getLogger(GraphicLayer3D.class
37
			.getName());
32 38

  
33 39
	public RectangleBehavior3D(RectangleListener zili) {
34 40
		super(zili);
35 41
		// TODO Auto-generated constructor stub
36 42
		listener = zili;
37
		rectangle = new SelectionRectangle();
38
		
43
		try {
44
			rectangle = new SelectionRectangle();
45
		} catch (OSGVPException e) {
46
			logger.error("Command: "
47
					+ "Error creating new selection rectangle.", e);
48
		}
49

  
39 50
	}
40 51

  
41 52
	@Override
42 53
	public void mouseDragged(MouseEvent e) {
43 54
		// TODO Auto-generated method stub
44
//		super.mouseDragged(e);
55
		// super.mouseDragged(e);
45 56
		// Painting
46
		rectangle.setMouseCoords((int)e.getPoint().getX(),height -(int) e.getPoint().getY());
47
		rectangle.update();
48
		
57
		rectangle.setMouseCoords((int) e.getPoint().getX(), height
58
				- (int) e.getPoint().getY());
59
		try {
60
			rectangle.update();
61
		} catch (OSGVPException e1) {
62
			logger.error("Command: " + "Error updating rectangle selection.",
63
					e1);
64
		}
65

  
49 66
		rectangle.setType(PolygonType.FILLED_POLYGON);
50 67

  
51
		
52
		
53
		
54 68
	}
55 69

  
56 70
	@Override
57 71
	public void mousePressed(MouseEvent e) {
58 72
		// TODO Auto-generated method stub
59 73
		MapContext3D mcontext = (MapContext3D) getMapControl().getMapContext();
60
		((PlanetViewer)mcontext.getCanvas3d().getOSGViewer()).addNodeToHUD(rectangle);
74
		try {
75
			((PlanetViewer) mcontext.getCanvas3d().getOSGViewer())
76
					.addNodeToHUD(rectangle);
77
		} catch (NodeException e1) {
78
			logger.error("Command: "
79
					+ "Error adding the rectangle selecton to the hud node.",
80
					e1);
81
		}
61 82
		height = mcontext.getCanvas3d().getHeight();
62
		width =mcontext.getCanvas3d().getWidth();
63
		
83
		width = mcontext.getCanvas3d().getWidth();
84

  
64 85
		if (e.getButton() == MouseEvent.BUTTON1) {
65 86
			m_FirstPoint = e.getPoint();
66 87
			// Put here the code to star drawing the rectangle
67
			rectangle.setFirstPoint((int)m_FirstPoint.getX(),height - (int)m_FirstPoint.getY());
68
			rectangle.setMouseCoords((int)m_FirstPoint.getX(),height -(int)m_FirstPoint.getY());
88
			rectangle.setFirstPoint((int) m_FirstPoint.getX(), height
89
					- (int) m_FirstPoint.getY());
90
			rectangle.setMouseCoords((int) m_FirstPoint.getX(), height
91
					- (int) m_FirstPoint.getY());
69 92

  
70
			rectangle.update();
93
			try {
94
				rectangle.update();
95
			} catch (OSGVPException e1) {
96
				logger.error("Command: "
97
						+ "Error updating rectangle selection.", e1);
98
			}
71 99
			rectangle.setType(PolygonType.EMPTY_POLYGON);
72 100
		}
73 101
	}
74 102

  
75 103
	@Override
76 104
	public void mouseReleased(MouseEvent e) throws BehaviorException {
77
		
78
		if (m_FirstPoint == null) return;
79
		Point2D p1  = new Point2D.Double();
105

  
106
		if (m_FirstPoint == null)
107
			return;
108
		Point2D p1 = new Point2D.Double();
80 109
		Point2D p2 = new Point2D.Double();
81 110
		Point pScreen = e.getPoint();
82
		
83
//		ViewPort vp = getMapControl().getMapContext().getViewPort();
84
		
85
//		p1 = vp.toMapPoint(m_FirstPoint);
86
//		p2 = vp.toMapPoint(pScreen);
111

  
112
		// ViewPort vp = getMapControl().getMapContext().getViewPort();
113

  
114
		// p1 = vp.toMapPoint(m_FirstPoint);
115
		// p2 = vp.toMapPoint(pScreen);
87 116
		Vec3 p1Vec = coordinatesIntersection(m_FirstPoint);
88
		p1.setLocation(p1Vec.x(),p1Vec.y());
117
		p1.setLocation(p1Vec.x(), p1Vec.y());
89 118
		Vec3 p2Vec = coordinatesIntersection(pScreen);
90
		p2.setLocation(p2Vec.x(),p2Vec.y());
91
		
119
		p2.setLocation(p2Vec.x(), p2Vec.y());
120

  
92 121
		if (e.getButton() == MouseEvent.BUTTON1) {
93
			//	Fijamos el nuevo extent
122
			// Fijamos el nuevo extent
94 123
			Rectangle2D.Double r = new Rectangle2D.Double();
95 124
			r.setFrameFromDiagonal(p1, p2);
96
			
125

  
97 126
			Rectangle2D rectPixel = new Rectangle();
98 127
			rectPixel.setFrameFromDiagonal(m_FirstPoint, pScreen);
99
			
128

  
100 129
			RectangleEvent event = new RectangleEvent(r, e, rectPixel);
101 130
			listener.rectangle(event);
102
			
131

  
103 132
		}
104
		
133

  
105 134
		// Deleted rectangle
106
		rectangle.setMouseCoords((int)m_FirstPoint.getX(),height -(int)m_FirstPoint.getY());
107
		rectangle.update();
135
		rectangle.setMouseCoords((int) m_FirstPoint.getX(), height
136
				- (int) m_FirstPoint.getY());
137
		try {
138
			rectangle.update();
139
		} catch (OSGVPException e1) {
140
			logger.error("Command: " + "Error updating rectangle selection.",
141
					e1);
142
		}
108 143
		rectangle.setType(PolygonType.EMPTY_POLYGON);
109
		
144

  
110 145
		m_FirstPoint = null;
111 146
		m_LastPoint = null;
112 147
	}
......
114 149
	@Override
115 150
	public void paintComponent(Graphics g) {
116 151
		// TODO Auto-generated method stub
117
//		super.paintComponent(g);
152
		// super.paintComponent(g);
118 153
		MapContext3D mcontext = (MapContext3D) getMapControl().getMapContext();
119 154
		mcontext.getCanvas3d().repaint();
120 155
	}
121 156

  
122 157
	public Vec3 coordinatesIntersection(Point2D pScreen) {
123 158
		Vec3 intersection;
124
	
159

  
125 160
		// System.err.println("Coordenadas de pantalla " + pScreen.getX() + ","+
126 161
		// pScreen.getY());
127 162
		MapContext3D mcontext = (MapContext3D) getMapControl().getMapContext();
......
135 170

  
136 171
			// System.err.println("Interseccion de osg " + hit.x() + ","+
137 172
			// hit.y());
138
			if (mcontext.getProjection().getAbrev().compareToIgnoreCase("EPSG:4326") == 0) {
139
				Vec3 geoPt = mcontext.getPlanet().convertXYZToLatLongHeight(hit);
173
			if (mcontext.getProjection().getAbrev().compareToIgnoreCase(
174
					"EPSG:4326") == 0) {
175
				Vec3 geoPt = mcontext.getPlanet()
176
						.convertXYZToLatLongHeight(hit);
140 177
				// Swap the coordinates X and Y, because they are invert.
141
				intersection = new Vec3(geoPt.y(),geoPt.x(),geoPt.z());
178
				intersection = new Vec3(geoPt.y(), geoPt.x(), geoPt.z());
142 179
			} else {
143 180
				intersection = hit;
144 181
			}
145 182
		} else {
146
			if (mcontext.getProjection().getAbrev().compareToIgnoreCase("EPSG:4326") == 0) {
183
			if (mcontext.getProjection().getAbrev().compareToIgnoreCase(
184
					"EPSG:4326") == 0) {
147 185
				pWorld.setLocation(360, 120);
148
				intersection = new Vec3(360,120,0);
149
			} else{
150
				intersection = new Vec3(1e100,1e100,0);
151
//				pWorld.setLocation(1e100, 1e100);
186
				intersection = new Vec3(360, 120, 0);
187
			} else {
188
				intersection = new Vec3(1e100, 1e100, 0);
189
				// pWorld.setLocation(1e100, 1e100);
152 190
			}
153 191
		}
154
		NotificationManager.addInfo("Obteniendo punto de informacion "+ intersection.x() + "    ,   " + intersection.y() +"  ,  "+ intersection.z());
192
		NotificationManager.addInfo("Obteniendo punto de informacion "
193
				+ intersection.x() + "    ,   " + intersection.y() + "  ,  "
194
				+ intersection.z());
155 195
		return intersection;
156 196
	}
157
	
158 197

  
159 198
}

Also available in: Unified diff