package navi.gui;
/**
* @author Thi H.
* @author Enis
* @author Nikolaj S.
*
* Gruppe 22 (PRG-PR-2011)
*/
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import navi.Dijkstra;
import navi.Knoten;
import navi.gui.AutoCompletion;
public class GUI extends javax.swing.JFrame {
private static final long serialVersionUID = 1L;
int sIndex1 = -1;
int sIndex2 = -1;
int pIndex1 = -1;
int pIndex2 = -1;
boolean removing = false;
Delay delay;
final String TITLE = "OSM jNavigator V1.0";
/** Creates new form MAPGUI */
public GUI() {
initComponents();
setLocationRelativeTo(null);
}
/**
* 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.
*/
// <editor-fold defaultstate="collapsed"
// <editor-fold defaultstate="collapsed"
// <editor-fold defaultstate="collapsed"
// <editor-fold defaultstate="collapsed"
// <editor-fold defaultstate="collapsed"
// <editor-fold defaultstate="collapsed"
// desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jFileChooser1 = new javax.swing.JFileChooser();
search = new javax.swing.ButtonGroup();
ways = new javax.swing.ButtonGroup();
jDialog1 = new javax.swing.JDialog();
jPanel2 = new javax.swing.JPanel();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jPanel3 = new javax.swing.JPanel();
jRadioButton3 = new javax.swing.JRadioButton();
jRadioButton4 = new javax.swing.JRadioButton();
jButton2 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
karte1 = new navi.gui.Karte();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jComboBox1 = new javax.swing.JComboBox();
jComboBox2 = new javax.swing.JComboBox();
jButton1 = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jSeparator1 = new javax.swing.JPopupMenu.Separator();
jMenuItem5 = new javax.swing.JMenuItem();
jMenuItem6 = new javax.swing.JMenuItem();
jMenuItem7 = new javax.swing.JMenuItem();
jSeparator2 = new javax.swing.JPopupMenu.Separator();
jMenuItem2 = new javax.swing.JMenuItem();
jMenu3 = new javax.swing.JMenu();
jMenuItem4 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
jFileChooser1.setFileFilter(new OSMFilter());
jDialog1.setTitle("EINSTELLUNGEN");
jDialog1.setBounds(new java.awt.Rectangle(0, 0, 330, 230));
jDialog1.setFont(new java.awt.Font("Tahoma", 0, 10));
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "PFAD", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.DEFAULT_POSITION));
jPanel2.setPreferredSize(new java.awt.Dimension(150, 83));
search.add(jRadioButton1);
jRadioButton1.setText("KÜRZESTER");
search.add(jRadioButton2);
jRadioButton2.setText("SCHNELLSTER");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
jPanel2Layout.createSequentialGroup().addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jRadioButton1).addComponent(jRadioButton2))
.addContainerGap(62, Short.MAX_VALUE)));
jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
jPanel2Layout.createSequentialGroup().addComponent(jRadioButton1).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jRadioButton2)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("TYP"));
jPanel3.setPreferredSize(new java.awt.Dimension(150, 142));
ways.add(jRadioButton3);
jRadioButton3.setText("ZU FUß");
jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton3ActionPerformed(evt);
}
});
ways.add(jRadioButton4);
jRadioButton4.setText("MIT DEM AUTO");
jRadioButton4.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton4ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
jPanel3Layout.createSequentialGroup().addContainerGap()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jRadioButton3).addComponent(jRadioButton4))
.addContainerGap(56, Short.MAX_VALUE)));
jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
jPanel3Layout.createSequentialGroup().addComponent(jRadioButton3).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jRadioButton4)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
jButton2.setText("OK");
jButton2.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/navi/gui/images/earth-icon.png"))); // NOI18N
jLabel1.setText("jLabel1");
javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
jDialog1.getContentPane().setLayout(jDialog1Layout);
jDialog1Layout.setHorizontalGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
jDialog1Layout
.createSequentialGroup()
.addContainerGap()
.addGroup(
jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(
jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, Short.MAX_VALUE)).addContainerGap()));
jDialog1Layout.setVerticalGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
jDialog1Layout
.createSequentialGroup()
.addContainerGap()
.addGroup(
jDialog1Layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jDialog1Layout.createSequentialGroup().addComponent(jLabel1).addGap(11, 11, 11).addComponent(jButton2))
.addGroup(
jDialog1Layout
.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap()));
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle(TITLE);
jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
karte1.addMouseWheelListener(new java.awt.event.MouseWheelListener() {
@Override
public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
karte1MouseWheelMoved(evt);
}
});
karte1.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
karte1MouseClicked(evt);
}
@Override
public void mousePressed(java.awt.event.MouseEvent evt) {
karte1MousePressed(evt);
}
@Override
public void mouseReleased(java.awt.event.MouseEvent evt) {
karte1MouseReleased(evt);
}
});
karte1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
@Override
public void mouseDragged(java.awt.event.MouseEvent evt) {
karte1MouseDragged(evt);
}
});
javax.swing.GroupLayout karte1Layout = new javax.swing.GroupLayout(karte1);
karte1.setLayout(karte1Layout);
karte1Layout.setHorizontalGroup(karte1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 925, Short.MAX_VALUE));
karte1Layout.setVerticalGroup(karte1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(karte1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(karte1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
jTextArea1.setColumns(20);
jTextArea1.setFont(new java.awt.Font("Arial", 0, 12));
jTextArea1.setRows(5);
jTextArea1.setEnabled(true);
jTextArea1.setEditable(false);
jScrollPane1.setViewportView(jTextArea1);
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel());
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});
jComboBox2.setModel(new javax.swing.DefaultComboBoxModel());
jComboBox2.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox2ActionPerformed(evt);
}
});
jButton1.setText("BERECHNEN");
jButton1.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jMenu1.setText("Datei");
jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
jMenuItem1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/navi/gui/images/open.png"))); // NOI18N
jMenuItem1.setText("Öffnen");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem1);
jMenu1.add(jSeparator1);
jMenuItem5.setText("PRG-Medium");
jMenuItem5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/navi/gui/images/openrecent.png")));
jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem5ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem5);
jMenuItem6.setText("PRG-Small");
jMenuItem6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/navi/gui/images/openrecent.png")));
jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem6ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem6);
jMenuItem7.setText("PRG-Frankfurt");
jMenuItem7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/navi/gui/images/openrecent.png")));
jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem7ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem7);
jMenu1.add(jSeparator2);
jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
jMenuItem2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/navi/gui/images/exit.png")));
jMenuItem2.setText("Beenden");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem2);
jMenuBar1.add(jMenu1);
jMenu3.setText("Einstellungen");
jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
jMenuItem4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/navi/gui/images/preference.png"))); // NOI18N
jMenuItem4.setText("Option");
jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem4ActionPerformed(evt);
}
});
jMenu3.add(jMenuItem4);
jMenuBar1.add(jMenu3);
setJMenuBar(jMenuBar1);
jMenu2.setText("Simulation");
jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
jMenuItem3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/navi/gui/images/play.png"))); // NOI18N
jMenuItem3.setText("Starten");
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem3ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem3);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jComboBox2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)).addContainerGap()));
layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap()
.addGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE).addGap(78, 78, 78).addComponent(jButton1).addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 505, Short.MAX_VALUE))
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addContainerGap()));
pack();
}// </editor-fold>//GEN-END:initComponents
void startSimulation() {
delay = new Delay();
delay.start();
jTextArea1.setText("SIMULATION!\n\n");
}
private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jMenuItem3ActionPerformed
/** Simulation-MENÜ */
if (delay == null && karte1.shortestpath != null) {
startSimulation();
}
}// GEN-LAST:event_jMenuItem3ActionPerformed
private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jMenuItem4ActionPerformed
/** Option-MENÜ */
jDialog1.setLocationRelativeTo(null);
jDialog1.setVisible(true);
}// GEN-LAST:event_jMenuItem4ActionPerformed
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jMenuItem2ActionPerformed
/** jFrame schießen */
this.dispose();
}// GEN-LAST:event_jMenuItem2ActionPerformed
private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jRadioButton3ActionPerformed
jRadioButton1.setSelected(true);
jRadioButton1.setEnabled(false);
jRadioButton2.setEnabled(false);
}// GEN-LAST:event_jRadioButton3ActionPerformed
private void jRadioButton4ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jRadioButton4ActionPerformed
if (!jRadioButton1.isEnabled()) {
jRadioButton1.setEnabled(true);
jRadioButton2.setEnabled(true);
}
}// GEN-LAST:event_jRadioButton4ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton2ActionPerformed
if (jRadioButton3.isSelected() || jRadioButton4.isSelected()) {
karte1.createGraph(jRadioButton4.isSelected());
}
jDialog1.setVisible(false);
}// GEN-LAST:event_jButton2ActionPerformed
/** jComboBox #1 */
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jComboBox1ActionPerformed
if (jComboBox1.getItemCount() > 0 && !removing) {
try {
if (sIndex1 != -1) {
((Knoten) jComboBox1.getItemAt(sIndex1)).isSelected(false);
}
sIndex1 = jComboBox1.getSelectedIndex();
((Knoten) jComboBox1.getSelectedItem()).isSelected(true);
repaint();
} catch (Exception e) {
System.err.println("jc1_actionperf exception: " + e);
}
}
}// GEN-LAST:event_jComboBox1ActionPerformed
/** jComboBox #2 */
private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jComboBox2ActionPerformed
if (jComboBox2.getItemCount() > 0 && !removing) {
try {
if (sIndex2 != -1) {
((Knoten) jComboBox2.getItemAt(sIndex2)).isSelected(false);
}
sIndex2 = jComboBox2.getSelectedIndex();
((Knoten) jComboBox2.getSelectedItem()).isSelected(true);
repaint();
} catch (Exception e) {
System.err.println("jc2_actionperf exception: " + e);
}
}
}// GEN-LAST:event_jComboBox2ActionPerformed
/** Berechnung des Pfades */
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton1ActionPerformed
if (jComboBox1.getSelectedIndex() != jComboBox2.getSelectedIndex()) {
karte1.SP = getNearestPoint(((Knoten) jComboBox1.getSelectedItem()).getLocation(), 0);
karte1.ZP = getNearestPoint(((Knoten) jComboBox2.getSelectedItem()).getLocation(), 1);
int[] pred = null;
/** OPTION: nach welchem Pfad soll der Graph durchgesucht werden */
if (jRadioButton1.isSelected()) {
// shortest path
pred = Dijkstra.dijkstraS(karte1.G, karte1.MapNode.indexOf(karte1.SP), (jRadioButton3.isSelected()) ? "fw" : "f");
} else {
// fastest path
pred = Dijkstra.dijkstraF(karte1.G, karte1.MapNode.indexOf(karte1.SP), (jRadioButton3.isSelected()) ? "fw" : "f");
}
karte1.shortestpath = Dijkstra.shortestPath(karte1.G, karte1.MapNode.indexOf(karte1.SP), karte1.MapNode.indexOf(karte1.ZP), pred);
if (karte1.shortestpath == null) {
jTextArea1.append("Es existiert kein Pfad zum Ziel\n");
} else {
jTextArea1.setText("");
int strecke = 0;
karte1.rIndex = 0;
karte1.strassen = new ArrayList<String>();
karte1.kreuzungen = new ArrayList<Point2D>();
for (int idx = 0; idx < karte1.shortestpath.size() - 1; idx++) {
int i = karte1.G.getIndex(karte1.shortestpath.get(idx).getId());
int j = karte1.G.getIndex(karte1.shortestpath.get(idx + 1).getId());
if (idx == 0) {
strecke = karte1.G.getWeight(i, j);
jTextArea1.append("Wir beginnen unsere Reise in " + jComboBox1.getSelectedItem().toString() + "\n");
jTextArea1.append("Fogen Sie dem Straßenverlauf " + karte1.G.getStreet(i, j).replace("unbekannte", "unbekannter") + "\n");
} else {
if (!karte1.G.getStreet(i, j).equals(karte1.G.getStreet(karte1.G.getIndex(karte1.shortestpath.get(idx - 1).getId()), i))) {
jTextArea1.append("Nach ca. " + strecke + " Metern biegen Sie in die " + karte1.G.getStreet(i, j) + " ab\n");
strecke = karte1.G.getWeight(i, j);
karte1.strassen.add(karte1.G.getStreet(i, j));
karte1.kreuzungen.add(karte1.G.getVertex(i).getLocation());
} else {
strecke += karte1.G.getWeight(i, j);
}
}
}
karte1.kreuzungen.add(karte1.shortestpath.get(karte1.shortestpath.size() - 1).getLocation());
jTextArea1.append("Gratulation, in ca. " + strecke + " Metern erreichen Sie " + jComboBox2.getSelectedItem().toString() + "!\n");
}
repaint();
}
}// GEN-LAST:event_jButton1ActionPerformed
/** Delay-Klasse */
class Delay extends Thread {
@Override
public void run() {
int rest = 0;
boolean info = false;
if (karte1.shortestpath != null && !karte1.shortestpath.isEmpty()) {
for (int i = 0; i < karte1.shortestpath.size() - 1; i++) {
/** Speed in m/s */
int velocity = karte1.G.getSpeed(karte1.G.getIndex(karte1.shortestpath.get(i).getId()), karte1.G.getIndex(karte1.shortestpath.get(i + 1).getId())) * 1000 / 3600 / 10;
karte1.LP1 = new Point2D.Double(karte1.shortestpath.get(i).getLocation().getX(), karte1.shortestpath.get(i).getLocation().getY());
karte1.LP2 = new Point2D.Double(karte1.shortestpath.get(i + 1).getLocation().getX(), karte1.shortestpath.get(i + 1).getLocation().getY());
int weight = (int) karte1.LP1.distance(karte1.LP2);
if (weight >= velocity + rest) {
while (weight >= velocity + rest) {
double winkel = karte1.getAngle(karte1.LP1, karte1.LP2);
double X = (velocity + rest) * Math.sin(Math.toRadians(winkel)) * (1) + karte1.LP1.getX();
double Y = (velocity + rest) * Math.cos(Math.toRadians(winkel)) * (1) + karte1.LP1.getY();
rest = 0;
weight -= velocity;
karte1.LP1.setLocation(X, Y);
/** Informationstafel ====================== */
if (karte1.LP1.distance(karte1.kreuzungen.get(karte1.rIndex)) < 30 && !info) {
info = true;
if (karte1.rIndex == karte1.kreuzungen.size() - 1) {
jTextArea1.append("---> Sie sind fast angegekommen!\n");
} else {
jTextArea1.append("---> Nach ca. 30 Metern biegen Sie in die " + karte1.strassen.get(karte1.rIndex) + " ab!\n");
}
}
/** AffineTransform ====================== */
karte1.jFrameWinkel = winkel;
Point point = new Point(karte1.getWidth() / 2, karte1.getHeight() / 2);
karte1.setTX(point.x - karte1.LP1.getX() * karte1.getScale());
karte1.setTY(point.y - karte1.LP1.getY() * karte1.getScale());
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
if (weight < velocity) {
rest = -1 * weight;
}
} else {
rest -= weight;
}
if (karte1.LP2.equals(karte1.kreuzungen.get(karte1.rIndex))) {
karte1.rIndex += 1;
info = false;
}
}
karte1.LP1 = null;
karte1.rIndex = 0;
repaint();
}
delay = null;
}
}
/** Berechnung des alpha-Winkels */
public static double getAlphaWinkel(Point2D P1, Point2D P2, Point2D P3) {
Point2D V1 = new Point2D.Double(P1.getX() - P2.getX(), P1.getY() - P2.getY());
Point2D V2 = new Point2D.Double(P3.getX() - P2.getX(), P3.getY() - P2.getY());
double SP = (V1.getX() * V2.getX()) + (V1.getY() * V2.getY());
double N1 = ((V1.getX() * V1.getX()) + (V1.getY() * V1.getY()));
double N2 = ((V2.getX() * V2.getX()) + (V2.getY() * V2.getY()));
double alpha = SP / (Math.sqrt(N1) * Math.sqrt(N2));
alpha = Math.toDegrees(Math.acos(alpha));
return (int) alpha;
}
/** Welcher Punkt des Straßennetzes liegt am nähsten? */
private Knoten getNearestPoint(Point2D location, int status) {
int entfernung = 1;
boolean isFound = false;
Knoten fk = null;
ArrayList<String> test = new ArrayList<String>();
test.add("highway");
if (jRadioButton3.isSelected()) {
test.add("footway");
}
while (!isFound) {
for (Knoten nk : karte1.MapNode) {
if (test.contains(nk.getType())) {
if (location.distance(nk.getLocation()) < entfernung) {
isFound = true;
if (status == 0) {
if (pIndex1 != -1) {
karte1.MapNode.get(pIndex1).isSelected(false);
}
pIndex1 = karte1.MapNode.indexOf(nk);
nk.isSelected(true);
} else {
if (pIndex2 != -1) {
karte1.MapNode.get(pIndex2).isSelected(false);
}
pIndex2 = karte1.MapNode.indexOf(nk);
nk.isSelected(true);
}
return nk;
}
}
}
entfernung += 1;
}
return fk;
}
/** .OSM Datei öffnen */
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jMenuItem1ActionPerformed
int returnVal = jFileChooser1.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
loadMap(jFileChooser1.getSelectedFile().getPath());
} else {
System.out.println("File access cancelled by user.");
}
}
/** Schnellzugriff #1 */
private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jMenuItem5ActionPerformed
loadMap("src/navi/osm/prg-medium.osm");
}
/** Schnellzugriff #2 */
private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jMenuItem6ActionPerformed
loadMap("src/navi/osm/prg-small.osm");
}
/** Schnellzugriff #3 */
private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jMenuItem7ActionPerformed
loadMap("src/navi/osm/prg-frankfurt.osm");
}
/** neue Karte initialisieren */
private void loadMap(String path) {
try {
if (jComboBox1.getItemCount() > 0) {
sIndex1 = -1;
sIndex2 = -1;
pIndex1 = -1;
pIndex2 = -1;
karte1.SP = null;
karte1.ZP = null;
jComboBoxRemoveAlmostAll(jComboBox1);
jComboBoxRemoveAlmostAll(jComboBox2);
}
karte1.readOSMKarte(path);
AutoCompletion.enable(jComboBox1);
AutoCompletion.enable(jComboBox2);
jRadioButton1.setSelected(true);
jRadioButton4.setSelected(true);
for (int i = 0; i < karte1.MapNodes.size(); i++) {
if (karte1.MapNodes.get(i).getType() != null && karte1.MapNodes.get(i).getTags().containsKey("addr:street")) {
jComboBox1.addItem(karte1.MapNodes.get(i));
jComboBox2.addItem(karte1.MapNodes.get(i));
}
}
this.setTitle(TITLE + ": " + path);
} catch (Exception e) {
System.out.println("problem accessing file" + e);
}
}
private void karte1MouseClicked(java.awt.event.MouseEvent evt) {
if (evt.getButton() == MouseEvent.BUTTON1) {
Point point = new Point((int) ((evt.getX() - karte1.getTX()) / karte1.getScale()), (int) ((evt.getY() - karte1.getTY()) / karte1.getScale()));
if (karte1.MapNodes != null) {
for (Knoten kn : karte1.MapNodes) {
if (kn.getLocation().distance(point) < 2) {
jTextArea1.append(kn.getTags().get("addr:street") + " " + kn.getTags().get("addr:housenumber") + " \n");
break;
}
}
}
}
}
private void karte1MousePressed(java.awt.event.MouseEvent evt) {
if (!karte1.isDragged() && evt.getButton() == MouseEvent.BUTTON3) {
karte1.setDragging(true);
karte1.setPreviousPoint(new Point(evt.getPoint()));
}
}
private void karte1MouseReleased(java.awt.event.MouseEvent evt) {
if (karte1.isDragged() && evt.getButton() == MouseEvent.BUTTON3) {
karte1.setDragging(false);
}
}
private void karte1MouseDragged(java.awt.event.MouseEvent evt) {
if (karte1.isDragged()) {
Point point = evt.getPoint();
karte1.setTX(karte1.getTX() + point.getX() - karte1.getPreviousPoint().getX());
karte1.setTY(karte1.getTY() + point.getY() - karte1.getPreviousPoint().getY());
karte1.setPreviousPoint(point);
repaint();
}
}
/** Zoomen */
private void karte1MouseWheelMoved(java.awt.event.MouseWheelEvent evt) {// GEN-FIRST:event_karte1MouseWheelMoved
double factor = Math.pow(1.1, evt.getWheelRotation());
Point point = evt.getPoint();
karte1.setTX(karte1.getTX() + (1 - factor) * (point.getX() - karte1.getTX()));
karte1.setTY(karte1.getTY() + (1 - factor) * (point.getY() - karte1.getTY()));
karte1.setScale(karte1.getScale() * factor);
repaint();
}// GEN-LAST:event_karte1MouseWheelMoved
/** jComboBox leeren beim Neuladen der .OSM Datei */
private void jComboBoxRemoveAlmostAll(JComboBox jc) {
try {
while (jc.getItemCount() > 0) {
removing = true;
jc.removeItemAt(0);
}
removing = false;
} catch (Exception e) {
System.err.println("remove exception: " + e);
}
}
/**
* @param args
* the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
// <editor-fold defaultstate="collapsed"
// desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase
* /tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
// </editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new GUI().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JComboBox jComboBox2;
private javax.swing.JDialog jDialog1;
private javax.swing.JFileChooser jFileChooser1;
private javax.swing.JLabel jLabel1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem5;
private javax.swing.JMenuItem jMenuItem6;
private javax.swing.JMenuItem jMenuItem7;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
private javax.swing.JRadioButton jRadioButton4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JPopupMenu.Separator jSeparator1;
private javax.swing.JPopupMenu.Separator jSeparator2;
private javax.swing.JTextArea jTextArea1;
private navi.gui.Karte karte1;
private javax.swing.ButtonGroup search;
private javax.swing.ButtonGroup ways;
// End of variables declaration//GEN-END:variables
}