package Maps2;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
/**
*
* @author stu696084
*/
public class Map extends javax.swing.JFrame {
JFileChooser fileChooser = new JFileChooser();
public City start;
public City end;
/** Creates new form Map */
public Map() {
initComponents();}
public class MyPanel extends JPanel {
@Override
@SuppressWarnings("static-access")
public void paint(Graphics g) {
super.paint(g);
for(City c: City.cities) {
Color z= Color.BLACK;
if (c == start){
z = Color.RED;
}
else if (c == end) {
z = Color.BLUE;
}
g.setColor(z);
g.fillOval((int)start.p.px, (int)start.p.py, 15, 15);
g.setColor(Color.RED);
g.drawLine((int)start.p.px, (int)start.p.py, (int)end.p.px, (int)end.p.py);
}
// Place code to draw the map here
}
}
/** 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
theMap = new MyPanel();
loadMap = new javax.swing.JButton();
showMap = new javax.swing.JButton();
showDirections = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
theMap.setBackground(new java.awt.Color(102, 204, 0));
theMap.setPreferredSize(new java.awt.Dimension(1000, 650));
theMap.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
theMapMouseClicked(evt);
}
});
org.jdesktop.layout.GroupLayout theMapLayout = new org.jdesktop.layout.GroupLayout(theMap);
theMap.setLayout(theMapLayout);
theMapLayout.setHorizontalGroup(
theMapLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 1000, Short.MAX_VALUE)
);
theMapLayout.setVerticalGroup(
theMapLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 650, Short.MAX_VALUE)
);
loadMap.setText("Load Map");
loadMap.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
loadMapActionPerformed(evt);
}
});
showMap.setText("Show Map");
showMap.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
showMapActionPerformed(evt);
}
});
showDirections.setText("Show Directions");
showDirections.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
showDirectionsActionPerformed(evt);
}
});
jLabel1.setText("jLabel1");
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(50, 50, 50)
.add(theMap, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(52, 52, 52)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(showMap)
.add(loadMap)
.add(showDirections)))
.add(layout.createSequentialGroup()
.add(464, 464, 464)
.add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 310, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(80, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(6, 6, 6)
.add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 23, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(loadMap)
.add(34, 34, 34)
.add(showMap)
.add(29, 29, 29)
.add(showDirections))
.add(theMap, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addContainerGap(41, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void loadMapActionPerformed(java.awt.event.ActionEvent evt) {
int retval = fileChooser.showOpenDialog(this);
if (retval == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
Input.readFile(file);
}
// TODO add your handling code here:
}
private void showMapActionPerformed(java.awt.event.ActionEvent evt) {
for (City c : City.cities) {
System.out.println(c.name + " Is at " + c.p.px + ", " + c.p.py);
for (Road r : c.roads) {
System.out.println(" Road " + r.name + " Goes to " + r.endCity.name + " (" + r.length + ", " + r.time + ")");
}
} // TODO add your handling code here:
// TODO add your handling code here:
}
private void showDirectionsActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void theMapMouseClicked(java.awt.event.MouseEvent evt) {
for(City c : City.cities) {
if(Point.close(evt.getX(), evt.getY())==true) {
c = start;
}
else if (c==start) {
c = end;
}
}
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Map().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JButton loadMap;
private javax.swing.JButton showDirections;
private javax.swing.JButton showMap;
private javax.swing.JPanel theMap;
// End of variables declaration
}