My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package org.penemunxt.projects.penemunxtexplorer.pc.map;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.awt.image.VolatileImage;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.event.*;

import org.penemunxt.graphics.pc.Images;
import org.penemunxt.projects.penemunxtexplorer.pc.connection.*;
import org.penemunxt.projects.penemunxtexplorer.pc.map.processing.*;

public class MapVisulaisation extends JPanel implements MouseListener,
MouseMotionListener, MouseWheelListener {
public final static int MAP_FRAME_LAST = -100;

final static float MAP_DEFAULT_SCALE_FACTOR = 0.004f;
public final static int MAP_MIN_SCALE = 1;
public final static int MAP_MAX_SCALE = 150;
final static int MAP_INIT_SCALE = 35;

int mapScale;
double mapRotate;
Point mapCenter;

Point mapStartDrag;
Point mapCenterBeforeDrag;

DataShare DS;
VolatileImage OSI;
MapProcessors mapProcessors;
int mapCurrentFrame;
boolean mapCentered;

public ArrayList<ChangeListener> mapScaleChanged = new ArrayList<ChangeListener>();
public ArrayList<ChangeListener> mapCenterChanged = new ArrayList<ChangeListener>();
public ArrayList<MouseListener> mapMouseClick = new ArrayList<MouseListener>();

private void fireMapScaleChanged(ChangeEvent ce) {
for (ChangeListener listener : mapScaleChanged) {
listener.stateChanged(ce);
}
}

private void fireMapCenterChanged(ChangeEvent ce) {
for (ChangeListener listener : mapCenterChanged) {
listener.stateChanged(ce);
}
}

private void fireMapMouseClick(MouseEvent me) {
for (MouseListener listener : mapMouseClick) {
listener.mouseClicked(me);
}
}

public int getMapScale() {
return mapScale;
}

public void setMapScale(int mapScale) {
this.setMapScale(mapScale, true);
}

public void setMapScale(int mapScale, boolean triggerChanged) {
this.mapScale = Math.max(Math.min(mapScale, MAP_MAX_SCALE),
MAP_MIN_SCALE);
this.refresh();

if (triggerChanged && mapScaleChanged != null) {
fireMapScaleChanged(new ChangeEvent(this));
}
}

public double getMapRotate() {
return mapRotate;
}

public void setMapRotate(double mapRotate) {
this.mapRotate = mapRotate;
}

public Point getMapCenter() {
return mapCenter;
}

public void setMapCenter(Point mapCenter) {
this.setMapCenter(mapCenter, true);
}

public void setMapCenter(Point mapCenter, boolean triggerChanged) {
this.mapCenter = mapCenter;
this.refresh();

if (triggerChanged && mapScaleChanged != null) {
fireMapCenterChanged(new ChangeEvent(this));
}
}

public int getMapCurrentFrame() {
return mapCurrentFrame;
}

public void setMapCurrentFrame(int mapCurrentFrame) {
if (mapCurrentFrame == MAP_FRAME_LAST) {
if (DS != null && DS.NXTRobotData != null) {
this.mapCurrentFrame = DS.NXTRobotData.size();
} else {
this.mapCurrentFrame = 0;
}

} else {
this.mapCurrentFrame = mapCurrentFrame;
}

// this.refresh();
}

public MapProcessors getMapProcessors() {
return mapProcessors;
}

public void setMapProcessors(MapProcessors mapProcessors) {
this.mapProcessors = mapProcessors;
}

public DataShare getDS() {
return DS;
}

public void setDS(DataShare dS) {
DS = dS;
}

public MapVisulaisation(MapProcessors mapProcessors) {
this(0, 0, null, mapProcessors, true);
}

public MapVisulaisation(int mapScale, MapProcessors mapProcessors,
boolean enableInteraction) {
this(mapScale, 0, null, mapProcessors, enableInteraction);
}

public MapVisulaisation(int mapScale, double mapRotate,
MapProcessors mapProcessors, boolean enableInteraction) {
this(mapScale, mapRotate, null, mapProcessors, enableInteraction);
}

public MapVisulaisation(int mapScale, double mapRotate, Point mapCenter,
MapProcessors mapProcessors, boolean enableInteraction) {
super();
this.reset();
if (mapScale >= MAP_MIN_SCALE && mapScale <= MAP_MAX_SCALE) {
this.mapScale = mapScale;
}

this.mapRotate = mapRotate;

if (mapCenter != null) {
this.mapCenter = mapCenter;
}

this.mapProcessors = mapProcessors;

if (enableInteraction) {
this.addMouseMotionListener(this);
this.addMouseListener(this);
this.addMouseWheelListener(this);
}

this.setCursor(new Cursor(Cursor.MOVE_CURSOR));
mapCentered = false;
}

public Image getMapImage() {
if (OSI != null) {
try {
return Images.toBufferedImage(OSI);
} catch (Exception e) {
return null;
}
} else {
return null;
}
}

public void refresh() {
repaint();
}

public void reset() {
this.resetMapCenter();
this.resetMapScale();
}

public void resetMapScale() {
this.setMapScale(MAP_INIT_SCALE);
}

public void resetMapCenter() {
Point mapDataCenter;
if (this.DS != null) {
// mapDataCenter = MapUtilities.getCenterPos(this.DS.NXTRobotData);
} else {
mapDataCenter = new Point(0, 0);
}

this.setMapCenter(new Point((this.getWidth() / 2),
(this.getHeight() / 2)));
}

@Override
public void paintComponent(Graphics g) {

if (mapCentered == false && mapCenter.x == 0 && mapCenter.y == 0) {
resetMapCenter();
mapCentered = true;
}

if (OSI == null || OSI.getWidth() != getWidth()
|| OSI.getHeight() != getHeight()) {
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice()
.getDefaultConfiguration();

OSI = createVolatileImage(this.getWidth(), this.getHeight());
}

Graphics OSIG = OSI.getGraphics();
OSIG.clearRect(0, 0, OSI.getWidth(), OSI.getHeight());

if (mapProcessors != null && DS != null && DS.NXTRobotData != null) {
mapProcessors.processData(DS.NXTRobotData, mapCurrentFrame,
(mapScale * MAP_DEFAULT_SCALE_FACTOR), mapRotate,
(int) mapCenter.getX(), (int) mapCenter.getY(), OSIG);
}
g.drawImage(OSI, 0, 0, null);
}

@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if(e.isControlDown()){
double mapRotateTemp = Math.toDegrees(this.getMapRotate());
mapRotateTemp += -e.getWheelRotation() * 5;
this.setMapRotate(Math.toRadians(mapRotateTemp%360));
}else{
int mapScaleTemp = this.getMapScale();
mapScaleTemp += -e.getWheelRotation() * 2;
mapScaleTemp = Math.max(mapScaleTemp, MAP_MIN_SCALE);
mapScaleTemp = Math.min(mapScaleTemp, MAP_MAX_SCALE);
this.setMapScale(mapScaleTemp);
}

refresh();
}

@Override
public void mousePressed(MouseEvent e) {
if (mapCenter != null) {
mapCenterBeforeDrag = (Point) mapCenter.clone();
mapStartDrag = new Point(e.getX(), e.getY());
}

refresh();
}

@Override
public void mouseClicked(MouseEvent e) {
AffineTransform currentMapTransform = MapUtilities.getMapTransform(
(int) mapCenter.getX(), (int) mapCenter.getY(),
(mapScale * MAP_DEFAULT_SCALE_FACTOR), mapRotate);
if (currentMapTransform != null) {
try {
Point2D p = currentMapTransform.inverseTransform(
e.getPoint(), null);
MouseEvent me = new MouseEvent(this, e.getID(), e.getWhen(), e.getModifiers(), (int) p.getX(), (int) p.getY(), e.getClickCount(), e.isPopupTrigger());
fireMapMouseClick(me);
} catch (NoninvertibleTransformException e1) {
}
}
}

@Override
public void mouseDragged(MouseEvent e) {
if (mapCenterBeforeDrag != null && mapStartDrag != null) {
int x = (int) (mapCenterBeforeDrag.getX() + (e.getX() - mapStartDrag
.getX()));
int y = (int) (mapCenterBeforeDrag.getY() + (e.getY() - mapStartDrag
.getY()));
this.getMapCenter().setLocation(x, y);
this.setMapCenter(this.getMapCenter());
}

refresh();
}

@Override
public void mouseEntered(MouseEvent e) {
}

@Override
public void mouseExited(MouseEvent e) {
}

@Override
public void mouseReleased(MouseEvent e) {
}

@Override
public void mouseMoved(MouseEvent e) {
}
}

Change log

r298 by peterf.se on May 18, 2010   Diff
[No log message]
Go to: 
Project members, sign in to write a code review

Older revisions

r292 by peterf.se on Feb 13, 2010   Diff
[No log message]
r213 by peterf.se on Jan 3, 2010   Diff
[No log message]
r208 by peterf.se on Jan 1, 2010   Diff
[No log message]
All revisions of this file

File info

Size: 8217 bytes, 318 lines

File properties

svn:mime-type
text/plain
Powered by Google Project Hosting