Skip to main content

change waypoint time depending

3 replies [Last post]
grobda
Offline
Joined: 2012-03-14
Points: 0

Hi all

I want to show just one location but it should change depending on the time. Like you have the information of your location depending on the time and you want to "play it like a movie". Therefore I implemented a method (waiting(1)) which waits 1 second before continuing. In the method setnewlocation() I add the new waypoints. (The Coordinates are just dummy values for testing purpose). The problem is now that the program waits but it updates the map after it is finished with the while loop in the setnewlocation() method. So it displays always only the last coordinates but it should change the display continuing.

Would be great if anybody knows how I could solve it.

The code is at the bottom

Cheers David

package ch.ethz.ikg.t_map.models;

import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JButton;

import org.jdesktop.swingx.JXMapKit;
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;

import ch.ethz.ikg.t_map.ui.MapView;

public class Model_Map implements MouseListener{

private MapView view;

public Model_Map(MapView view) {
this.view = view;

}

// JXMapKit Objekt
private JXMapKit mapKit;

public void loadMap() {

// Initialize the JXMapKit which manages the connection to
// the map provider and provides a view of these maps.
mapKit = new JXMapKit();
mapKit.setDefaultProvider(DefaultProviders.OpenStreetMaps);
mapKit.setDataProviderCreditShown(false);
mapKit.setVisible(true);
mapKit.getMainMap().addMouseListener(this);

//zoom Tools (nicht) sichtbar
mapKit.setZoomButtonsVisible(true);
mapKit.setZoomSliderVisible(true);

view.getContentPane().add(mapKit, BorderLayout.CENTER);

}

public void setnewlocation(){

Set<Waypoint> waypoints = new HashSet<Waypoint>();
WaypointPainter painter = new WaypointPainter();

double x;
x = 1.00;
while (x <= 5) {
System.out.println("test: "+x);
waiting(1);
waypoints.clear();
waypoints.add(new Waypoint( 50.0+x , 0 ));
painter.setWaypoints(waypoints);

mapKit.getMainMap().setOverlayPainter(painter);
x=x+1;

}

}

public static void waiting(int n) {
long t0, t1;
t0 = System.currentTimeMillis();

do {
t1 = System.currentTimeMillis();
} while ((t1 - t0) < (n * 1000));
}

public void mouseClicked(MouseEvent e) {
System.out.println("mouseClicked");

setnewlocation();

}

@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
System.out.println("mouseEntered");

}

@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
System.out.println("mouseExited");

}

@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
System.out.println("mousePressed");

}

@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
System.out.println("mouseReleased");

}

}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kschaefe
Offline
Joined: 2006-06-08
Points: 0

Use a javax.swing.Timer.

Karl

grobda
Offline
Joined: 2012-03-14
Points: 0

Thanks for the hint!

I implemented the java.util.Timer because I wasn't able to make the javax.swing.Timer working... But anyway it works :-D

Cheers David

kschaefe
Offline
Joined: 2006-06-08
Points: 0

Be careful.  java.util.Timer does not fire on the EDT, so unless you shift the over to the EDT with a SwingUtilities.invokeXXX call, you might end up with painting artifacts.

Karl