public class PiccolloSwingTest extends JFrame {
public PiccolloSwingTest() {
getContentPane().add(canvas);
}
private static final long serialVersionUID = 1L;
private static String proxyHost = "";
private static String proxyPort = "";
private static java.util.Properties systemProperties = System
.getProperties();
private static int WIDTH;
private static int HEIGHT;
private static int X;
private static int Y;
private static int CENTER_X;
private static int CENTER_Y;
private static PCamera camera = new PCamera();
private static JMapViewer gisMap = new JMapViewer();
private static PSwingCanvas canvas = new PSwingCanvas() {
public void setBounds(int x, int y, int width, int height) {
super.setBounds(x, y, width, height);
osm.setBounds(x, y, width, height);
canvas.getLayer().setBounds(x, y, width, height);
gisMap.setBounds(x, y, width, height);
gisMap.setPreferredSize(new Dimension(width, height));
camera.setBounds(0, 0, width, height);
adjustPiccolo(camera);
};
};
private static PSwing osm = new PSwing(gisMap);
private static PLayer display = new PLayer();
static int defaultZoom = gisMap.getZoom();
/**
* @param args
*/
public static void main(final String[] args) {
systemProperties.setProperty("http.proxyHost", proxyHost);
systemProperties.setProperty("http.proxyPort", proxyPort);
// cleanup listeners
MouseWheelListener[] mwls = gisMap.getMouseWheelListeners();
for (int i = 0; i < mwls.length; i++) {
MouseWheelListener mWL = mwls[i];
gisMap.removeMouseWheelListener(mWL);
}
MouseListener[] mls = gisMap.getMouseListeners();
for (int i = 0; i < mls.length; i++) {
MouseListener mouseListener = mls[i];
gisMap.removeMouseListener(mouseListener);
}
MouseMotionListener[] mmls = gisMap.getMouseMotionListeners();
for (int i = 0; i < mmls.length; i++) {
MouseMotionListener mouseMotionListener = mmls[i];
gisMap.removeMouseMotionListener(mouseMotionListener);
}
for (PInputEventListener l : canvas.getInputEventListeners()) {
canvas.removeInputEventListener(l);
}
camera.addInputEventListener(new InteractionHandler2(gisMap, defaultZoom));
X = 0;
Y = 0;
WIDTH = OsmMercator.LonToX(180, defaultZoom);
HEIGHT = OsmMercator.LatToY(-90, defaultZoom);
CENTER_X = OsmMercator.LonToX(0, defaultZoom);
CENTER_Y = OsmMercator.LatToY(0, defaultZoom);
PPath world = PPath.createRectangle(X, Y, WIDTH, HEIGHT);
world.setStrokePaint(Color.black);
display.addChild(world);
world.setPaint(null);
PPath NW = PPath.createRectangle(X + 10, Y + 10, (WIDTH / 2) - 20,
(HEIGHT / 2) - 20);
NW.setStrokePaint(Color.red);
NW.setVisible(true);
display.addChild(NW);
NW.setPaint(null);
PPath NE = PPath.createRectangle(CENTER_X + 10, Y + 10, (WIDTH / 2) - 20,
(HEIGHT / 2) - 20);
NE.setStrokePaint(Color.green);
display.addChild(NE);
NE.setPaint(null);
PPath SW = PPath.createRectangle(X + 10, CENTER_Y + 10, (WIDTH / 2) - 20,
(HEIGHT / 2) - 20);
SW.setStrokePaint(Color.blue);
SW.setPaint(null);
display.addChild(SW);
SW.setPaint(null);
PPath SE = PPath.createRectangle(CENTER_X + 10, CENTER_Y + 10,
(WIDTH / 2) - 20, (HEIGHT / 2) - 20);
SE.setStrokePaint(Color.yellow);
display.addChild(SE);
SE.setPaint(null);
PPath NS = PPath.createLine(CENTER_X, Y, CENTER_X, Y + HEIGHT);
NS.setStrokePaint(Color.black);
display.addChild(NS);
PPath EW = PPath.createLine(X, CENTER_Y, X + WIDTH, CENTER_Y);
EW.setStrokePaint(Color.black);
display.addChild(EW);
display.addChild(PPath.createEllipse(CENTER_X - 5, CENTER_Y - 5, 10, 10));
display.addChild(PPath.createEllipse(X - 5, Y - 5, 10, 10));
display.addChild(PPath.createEllipse(WIDTH - 5, Y - 5, 10, 10));
display.addChild(PPath.createEllipse(X - 5, HEIGHT - 5, 10, 10));
display.addChild(PPath.createEllipse(WIDTH - 5, HEIGHT - 5, 10, 10));
camera.addLayer(display);
// get neutral lat/lon coords
Coordinate c1 = gisMap.getPosition(new Point(0, 0));
// translate into piccolo coordinates
Point p = new Point();
p.x = OsmMercator.LonToX(c1.getLon(), defaultZoom);
p.y = OsmMercator.LatToY(c1.getLat(), defaultZoom);
// adjust piccolo
// camera.setBounds(0, 0, canvas.getCamera().getWidth(), canvas.getCamera()
// .getHeight());
camera.setViewOffset(p.x, p.y);
canvas.getLayer().addChild(osm);
canvas.getLayer().addChild(camera);
osm.moveToBack();
// canvas.getCamera().repaint();
// frame setup
final PiccolloSwingTest frame = new PiccolloSwingTest();
frame.setBounds(50, 50, 200, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void setBounds(int x, int y, int width, int height) {
super.setBounds(x, y, width, height);
canvas.setBounds(x, y, width, height);
}
private static void adjustPiccolo(PCamera camera) {
Point p1 = gisMap.getMapPosition(90, -180, false);
Point p2 = gisMap.getMapPosition(-90, 180, false);
// map is always square, so scale can be calculated without squaring for
// distance
double scaleMap = p2.x - p1.x + 1;
double scaleDef = OsmMercator.LonToX(180, defaultZoom) + 1;
// scale in map/default
double scale = Math.round(1000 * scaleMap / scaleDef) / 1000d;
double xMod = (double) p1.x / scale;
double yMod = (double) p1.y / scale;
camera.setViewOffset(xMod, yMod);
}
}