/* * MainJFrame.java * * Created on 10 april 2008, 17: * @todo: Eliminate dirty hacking intruduced to het Compass displayed properly */ package routegui; import java.text.DateFormat; import java.util.Calendar; import java.util.Date; import java.util.logging.Level; import java.util.logging.Logger; import org.jdesktop.swingx.mapviewer.GeoPosition; import java.util.TimerTask; import java.util.Random; import java.awt.Color; import java.awt.Graphics2D; import java.util.HashSet; import java.util.Set; import javax.swing.JFileChooser; import org.jdesktop.swingx.JXMapViewer; import org.jdesktop.swingx.mapviewer.Waypoint; import org.jdesktop.swingx.mapviewer.WaypointPainter; /* * @author evert */ import org.jdesktop.swingx.mapviewer.WaypointRenderer; import org.jdesktop.swingx.painter.CompoundPainter; import org.jdesktop.swingx.painter.Painter; public class MainJFrame extends javax.swing.JFrame { /** Creates new form MainJFrame */ private boolean powerState = false; private java.util.Timer clockTimer = new java.util.Timer(); private Random generator = new Random(); private SimulateDrive simulateDrive; /** * Revert state of PND, either ON -> OFF or OFF -> ON */ private void powerDevice() { powerState = ! powerState; if (powerState) { jTogglePower.setText("Turn OFF"); } else { jTogglePower.setText("Turn ON"); } jControl.setVisible(powerState); jLabelSnelheid.setVisible(powerState); jCompass.setVisible(powerState); jButtonEndSimulation.setVisible(powerState); } private void updateSpeed(double snelheid) { jLabelSnelheid.setText(String.valueOf(snelheid).concat(" KM")); if (snelheid < 50) { jLabelSnelheid.setForeground(Color.green); } else if (snelheid < 100) { jLabelSnelheid.setForeground(Color.yellow); } else { jLabelSnelheid.setForeground(Color.red); } jLabelSnelheid.repaint(); } public MainJFrame() { initComponents(); clockTimer.scheduleAtFixedRate(new TickTimerTask(), 0, 1000); /* Hack to allow init of components of OFF */ powerState = true; powerDevice(); jXMapKit1.setZoom(3); } /** tack to update clock one a second */ class TickTimerTask extends TimerTask { @Override public void run() { Date rightNow = Calendar.getInstance().getTime(); jLabelTime.setText(DateFormat.getTimeInstance().format(rightNow)); } } class SimulateDrive implements Runnable { private boolean killMe = false; private GPS route = new GPS(); public void terminate() { killMe = true; } private SimulateDrive(String routeFile) { route.init(routeFile); } @Override @SuppressWarnings("unchecked") //@todo: warnings Painter mapping public void run() { /** * @todo: Find a proper way to display both the route and current location at the same time * Guru help site at http://today.java.net/pub/a/today/2007/11/13/mapping-mashups-with-jxmapviewer.html */ Painter defaultOverlay = jXMapKit1.getMainMap().getOverlayPainter(); Painter textOverlay = new Painter() { @Override public void paint(Graphics2D g, JXMapViewer map, int w, int h) { g.setPaint(new Color(0, 0, 0, 150)); g.fillRoundRect(5, 5, 280, 30, 10, 10); g.setPaint(Color.WHITE); g.drawString("Images provided by OpenStreetMap.org", 20, 20); } }; CompoundPainter cp = new CompoundPainter(); cp.setPainters(textOverlay, defaultOverlay); cp.setCacheable(false); jXMapKit1.getMainMap().setOverlayPainter(cp); Set waypoints = new HashSet(); WaypointPainter painter = new WaypointPainter(); //painter.setWaypoints(waypoints); painter.setRenderer(new WaypointRenderer() { @Override public boolean paintWaypoint(Graphics2D g, JXMapViewer map, Waypoint wp) { g.setColor(Color.RED); g.drawLine(0, 0, 0, 0); return true; } }); /* Initial fetch data */ route.next(); long prevTime = route.data.calendar.getTime().getTime(); Date curTime; int i = 0; /* Center map at starting point */ jXMapKit1.setAddressLocation(route.data.position); do { try { waypoints.add(new Waypoint(route.data.position)); jXMapKit1.setAddressLocation(route.data.position); updateSpeed(route.data.speed); if (route.data.bearing != null) { Compass foo = (Compass) jCompass; foo.setAngle(route.data.bearing.intValue()); } /* Specify amount of sleep time needed */ curTime = route.data.calendar.getTime(); jLabelTime.setText(DateFormat.getTimeInstance().format(curTime)); Thread.sleep(Math.max(0, (curTime.getTime() - prevTime) / jSliderSimulationSpeed.getValue())); prevTime = curTime.getTime(); } catch (InterruptedException ex) { Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, null, ex); } } while (route.next() && !killMe); painter.setWaypoints(waypoints); jXMapKit1.getMainMap().setOverlayPainter(painter); } } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // //GEN-BEGIN:initComponents private void initComponents() { jTogglePower = new javax.swing.JToggleButton(); jControl = new javax.swing.JTabbedPane(); jPanel1 = new javax.swing.JPanel(); jXMapKit1 = new org.jdesktop.swingx.JXMapKit(); jPanel2 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jButtonQ = new javax.swing.JButton(); jButtonOK = new javax.swing.JButton(); jTextFieldDestination = new javax.swing.JTextField(); jButtonBcsp = new javax.swing.JButton(); jPanel3 = new javax.swing.JPanel(); jTextFieldFileSimulation = new javax.swing.JTextField(); jButtonFileSimulationBrowse = new javax.swing.JButton(); jLabelFileSimulation = new javax.swing.JLabel(); jSliderSimulationSpeed = new javax.swing.JSlider(); jButtonSimulationStatus = new javax.swing.JButton(); jLabelSimulationSpeed = new javax.swing.JLabel(); jLabelNames = new javax.swing.JLabel(); jLabelTime = new javax.swing.JLabel(); jLabelSnelheid = new javax.swing.JLabel(); jButtonEndSimulation = new javax.swing.JButton(); jCompass = new Compass(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTogglePower.setText("Turn OFF"); jTogglePower.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTogglePowerActionPerformed(evt); } }); jControl.setTabPlacement(javax.swing.JTabbedPane.LEFT); jControl.setFont(jControl.getFont().deriveFont((jControl.getFont().getStyle() | java.awt.Font.ITALIC) | java.awt.Font.BOLD, jControl.getFont().getSize()+11)); jXMapKit1.setDefaultProvider(org.jdesktop.swingx.JXMapKit.DefaultProviders.OpenStreetMaps); jXMapKit1.setScrollableTracksViewportWidth(true); jXMapKit1.setZoom(1); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jXMapKit1, javax.swing.GroupLayout.DEFAULT_SIZE, 595, Short.MAX_VALUE) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jXMapKit1, javax.swing.GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE) .addContainerGap()) ); jControl.addTab("Map", jPanel1); jLabel1.setText("Destination :"); jButtonQ.setText("q"); jButtonQ.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonQActionPerformed(evt); } }); jButtonOK.setText("OK currently hardcoded to 52.161087, 4.490153"); jButtonOK.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonOKActionPerformed(evt); } }); jTextFieldDestination.setText("