Skip to main content

JXMapViewer - JButton as Waypoint

9 replies [Last post]
question
Offline
Joined: 2009-05-11

Hi

I developed a application which places the custom waypoints onto the map. I used WaypointPainter class to customize the waypoint. I have not used Gaphics object to draw the waypoint instead i used setRenderer(..)
I used JButton as a waypoints through which i added MouseListener to the waypoints to identify which waypoint was clicked.

Problem:
1) When i move the map towards left/right/top/bottom all the waypoints are strucking up in the corners. They are not losing the GeoPositions. Can you please give me the colution for this.
2) Is there any other way rather than using JButton as Waypoints and adding listeners to the JButton.

Help Me......

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
epp1628
Offline
Joined: 2010-06-04

Hi
i am trying to do an application with maps.I would like to know how i can "attach" Label to a Waypoint.The result that i want is when click the waypoint ---> appear a label or text up of waypoint.

anhsirk
Offline
Joined: 2009-05-21

hey can u pls give me the code how u added jbuttons as waypoints ..

i added mouse listner to JXMapkit instead .

fubb
Offline
Joined: 2009-05-27

I got a solution to this problem :)
Here is a complete, working example application.

- Fubb

SwingWaypoint.java
[code]
import javax.swing.JComponent;

import org.jdesktop.swingx.mapviewer.GeoPosition;
import org.jdesktop.swingx.mapviewer.Waypoint;

public class SwingWaypoint extends Waypoint{
private JComponent component;

public SwingWaypoint(JComponent component, GeoPosition gp) {
super(gp);
this.component = component;
}

public JComponent getComponent() {
return component;
}
}
[/code]

Main.java
[code]
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Point2D;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import org.jdesktop.swingx.JXMapKit;
import org.jdesktop.swingx.JXMapViewer;
import org.jdesktop.swingx.JXMapKit.DefaultProviders;
import org.jdesktop.swingx.mapviewer.GeoPosition;
import org.jdesktop.swingx.mapviewer.Waypoint;
import org.jdesktop.swingx.mapviewer.WaypointPainter;
import org.jdesktop.swingx.mapviewer.WaypointRenderer;

public class Main {

public static final void main(String args[]) {
JFrame frame = new JFrame("JXMapViewer with swingwaypoints");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JXMapKit mapkit = new JXMapKit();
mapkit.setDefaultProvider(DefaultProviders.OpenStreetMaps);
mapkit.setAddressLocationShown(true);
frame.add(mapkit);

mapkit.setAddressLocation(new GeoPosition(-7.802778, 111.263056));
mapkit.setZoom(10);

JComponent test1 = new JPanel() {
@Override
public void paint(Graphics g) {
setSize(new Dimension(24, 24));
Graphics2D g2d = (Graphics2D)g;
g2d.setClip(null);
g2d.setColor(Color.RED);
setPreferredSize(new Dimension(100, 100));
g2d.drawLine(0, 0, getWidth(), getHeight());
g2d.drawLine(0, getHeight(), getWidth(), 0);
setVisible(true);
}
};
JButton test2 = new JButton("test2");
JRadioButton test3 = new JRadioButton();

test1.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
System.out.println("Clicked the panel");
}
public void mouseEntered(MouseEvent e) {};
public void mouseExited(MouseEvent e) {};
public void mousePressed(MouseEvent e) {};
public void mouseReleased(MouseEvent e) {};
});

test2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("The test2 button was clicked");
}
});

test3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("someone clicked the radio button");
}
});

Set waypoints = new HashSet();
waypoints.add(new SwingWaypoint(test1, new GeoPosition(-7.502778, 111.263056)));
waypoints.add(new SwingWaypoint(test2, new GeoPosition(-8.02778, 111.263056)));
waypoints.add(new SwingWaypoint(test3, new GeoPosition(-7.502778, 110.5)));

JXMapViewer map = mapkit.getMainMap();
for (Waypoint wp : waypoints)
map.add(((SwingWaypoint) wp).getComponent());

WaypointPainter painter = new WaypointPainter() {
@Override
protected void doPaint(Graphics2D g, JXMapViewer map, int width,
int height) {
for (Waypoint wp : getWaypoints()) {
Point2D gp_pt = map.getTileFactory().geoToPixel(
wp.getPosition(), map.getZoom());
Rectangle rect = map.getViewportBounds();
Point pt = new Point(
(int) gp_pt.getX() - rect.x,
(int) gp_pt.getY() - rect.y);
JComponent component = ((SwingWaypoint) wp).getComponent();
component.setLocation(pt);
}
}
};
painter.setWaypoints(waypoints);

map.setOverlayPainter(painter);
frame.setBounds(100, 100, 400, 400);
frame.setVisible(true);
}
}
[/code]

Darkanzali
Offline
Joined: 2013-05-31

Hello,

How can I create more than one waypoints?

JButton jb = new JButton();

jb.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            System.out.println("The button was clicked");
            }
        });

Set<\Waypoint\> waypoints = new HashSet<\Waypoint\>();
            waypoints.add(new SwingWaypoint(jb, new GeoPosition(0,0)));
            waypoints.add(new SwingWaypoint(jb, new GeoPosition(10,10)));

for (Waypoint wp : waypoints)
            mapkit.add(((SwingWaypoint) wp).getComponent());

WaypointPainter<\Waypoint\> waypointPainter = new WaypointPainter<\Waypoint\>(){
@Override
            protected void doPaint(Graphics2D g, JXMapViewer map, int width, int height) {

                for (Waypoint wp : getWaypoints()) {
                    Point2D gp_pt = map.getTileFactory().geoToPixel(
                            wp.getPosition(), map.getZoom());
                    Rectangle rect = map.getViewportBounds();
                    Point pt = new Point(
                            (int) gp_pt.getX() - rect.x,
                            (int) gp_pt.getY() - rect.y);
                    JComponent component = ((SwingWaypoint) wp).getComponent();
                    component.setLocation(pt);
                }
            }

waypointPainter.setWaypoints(waypoints);
mapkit.setOverlayPainter(waypointPainter);
epp1628
Offline
Joined: 2010-06-04

Hi i copy this code but it shows me two errors at lines:

for (Waypoint wp : waypoints) and for (Waypoint wp : getWaypoints()) {.... //incompatible types

could you tell me what to do?i have imported the .jar swingx into my project

anhsirk
Offline
Joined: 2009-05-21

hey thats great .. its working .
Thanks a lot

question
Offline
Joined: 2009-05-11

Thank you so much
Its working
Great, i tried it many times but i couldn't able to achieve it you did it

question
Offline
Joined: 2009-05-11

Which way increases the performance of the application?
1) Adding Mouse listener to the Map
or
2) Adding Action listener to the button Component?

Joe St. Germain

I'm using a Mouse listener, and haven't seen any performance degradation.

Jos St. Germain

________________________________

From: jdnc-interest@javadesktop.org [mailto:jdnc-interest@javadesktop.org]
Sent: Tue 6/9/2009 4:06 AM
To: jdnc-resend@javadesktop.org
Subject: Re: JXMapViewer - JButton as Waypoint

Which way increases the performance of the application?
1) Adding Mouse listener to the Map
or
2) Adding Action listener to the button Component?
[Message sent by forum member 'question' (question)]

http://forums.java.net/jive/thread.jspa?messageID=349949

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net

[winmail.dat]
---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net