Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package aiproject;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
/**
*
* @author Jehonathan
*/
public class UIForm extends javax.swing.JFrame {
public Reader obj;
public ArrayList cities;
public Node current;
public Scanner in;
boolean pauseresume_flag = false;
JDialog dialog = new JDialog();
public Reader obj1;
public int j = 0;
public Timer l;
public String[] contents;
public String[] contents1;
public String[] opennodes;
public String contents2[];
public String[] from_root;
public String[] to_dest;
final JFileChooser fc1 = new JFileChooser();
final JFileChooser fc2 = new JFileChooser();
public UIForm() throws IOException {
obj = new Reader();
initComponents();
customcall();
}
/**
* 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() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jComboBox1 = new javax.swing.JComboBox();
jComboBox2 = new javax.swing.JComboBox();
jLabel3 = new javax.swing.JLabel();
jComboBox3 = new javax.swing.JComboBox();
jLabel4 = new javax.swing.JLabel();
jComboBox4 = new javax.swing.JComboBox();
jLabel6 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jLabel9 = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
jLabel11 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jLabel5 = new javax.swing.JLabel();
jLabel12 = new javax.swing.JLabel();
jLabel13 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Start city");
jLabel2.setText("End city");
jButton1.setText("Find path");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel3.setText("Excluded city");
jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "nothing" }));
jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Shortest distance", "Fewest links" }));
jLabel6.setText("Hueristic Type");
jButton2.setText("pause");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("resume");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jLabel7.setText("Locations");
jLabel8.setText("Connections");
jTextField1.setText("C:\\Users\\Jehonathan\\Desktop\\AI1-preliminary\\AI1\\Locations.txt");
jTextField1.setToolTipText("");
jTextField2.setText("C:\\Users\\Jehonathan\\Desktop\\AI1-preliminary\\AI1\\Connections.txt");
jButton4.setText("Browse");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton5.setText("Browse");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jLabel9.setText("Open node");
jLabel10.setText("current path");
jLabel11.setText("backtracked path");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel7))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addGap(25, 25, 25)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField1, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jTextField2))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton1)
.addComponent(jButton4)
.addComponent(jButton5)))
.addComponent(jLabel8)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 402, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton3)
.addComponent(jLabel9)
.addComponent(jLabel10)
.addComponent(jLabel11))
.addContainerGap(67, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel7)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton4)
.addComponent(jLabel9))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel8)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton5)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel10)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel11)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3)
.addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6)
.addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(jButton3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel4)
.addGap(15, 15, 15))
);
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()
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(32, 32, 32)
.addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel13, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addGap(0, 578, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(jLabel12)
.addComponent(jLabel13)))
);
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)
.addContainerGap())
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap()))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(505, Short.MAX_VALUE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(61, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap()))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
boolean flag = true;
obj1 = new Reader();
obj1.FNAME_input_locations = jTextField1.getText();
obj1.FNAME_input_connections = jTextField2.getText();
try {
obj1.open();
} catch (IOException ex) {
// Logger.getLogger(AI1.class.getName()).log(Level.SEVERE, null, ex);
}
obj1.read();
obj1.start = jComboBox1.getSelectedItem().toString();
obj1.destination = jComboBox2.getSelectedItem().toString();
obj1.excluded = jComboBox3.getSelectedItem().toString();
if (obj1.start == null ? obj1.destination == null : obj1.start.equals(obj1.destination)) {
jLabel4.setText("Start city cannot be same as the destination city");
flag = false;
}
if (obj1.start.equals(obj1.excluded) || obj1.destination.equals(obj1.excluded)) {
jLabel4.setText("You cannot exclude a start or end city");
flag = false;
}
if (flag) {
jLabel4.setText("");
if (jComboBox4.getSelectedItem().toString().equals("Shortest distance")) {
obj1.search();
} else {
obj1.search1();
}
try {
obj1.close();
} catch (IOException ex) {
// Logger.getLogger(AI1.class.getName()).log(Level.SEVERE, null, ex);
}
if (obj1.g.results != null) {
contents2 = obj1.g.results.split("--");
opennodes = obj1.g.opennodes.split("--");
from_root = obj1.g.distance_root.split("--");
to_dest = obj1.g.estimated_dist.split("--");
}
j = 0;
l = new Timer();
//animation scheduling
l.schedule(new TimerTask() {
@Override
public void run() {
if (j >= contents2.length - 1) {
l.cancel();
}
repaint();
}
}, 0, 2000);
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// pause
pauseresume_flag = true;
l.cancel();
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
//resume
if (pauseresume_flag) {
l = new Timer();
l.schedule(new TimerTask() {
@Override
public void run() {
if (j >= contents2.length - 1) {
l.cancel();
}
// System.out.println(j);
repaint();
}
}, 0, 2000);
pauseresume_flag = false;
}
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
dialog.add(fc1);
dialog.setVisible(true);
int retval = fc1.showOpenDialog(dialog);
dialog.setVisible(true);
if (retval == JFileChooser.APPROVE_OPTION) {
jTextField1.setText(fc1.getSelectedFile().getAbsolutePath().toString());
dialog.setVisible(false);
} else {
dialog.setVisible(false);
dialog.dispose();
}
customcall();
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
dialog.add(fc2);
dialog.setVisible(true);
int retval = fc2.showOpenDialog(dialog);
dialog.setVisible(true);
if (retval == JFileChooser.APPROVE_OPTION) {
jTextField2.setText(fc2.getSelectedFile().getAbsolutePath().toString());
dialog.setVisible(false);
} else {
dialog.setVisible(false);
dialog.dispose();
}
customcall();
}
/**
* @param args the command line arguments
*/
@Override
public synchronized void paint(Graphics g) {
super.paint(g);
for (int i = 0; i < cities.size(); i++) {
//setBackground(Color.red);
g.setColor(Color.yellow);
for (int p = 0; p < contents.length; p++) {
String cord[] = contents[p].split(" ");
drawThickLine(g, (Integer.parseInt(cord[0]))/2 +250, (Integer.parseInt(cord[1]))/2 +225, (Integer.parseInt(cord[2]))/2 +250, (Integer.parseInt(cord[3]))/2 +225, 2, Color.white);
}
}
g.setFont(new Font("TimesRoman", Font.BOLD + Font.ITALIC, 14));
for (int i = 0; i < cities.size(); i++) {
g.setColor(Color.blue);
if (opennodes != null) {
if (j > opennodes.length - 1) {
j = opennodes.length - 1;
}
String[] pp = opennodes[j].split(",");
for (int k = 0; k < pp.length; k++) {
if (pp[k].equals(((Node) cities.get(i)).city)) {
g.setColor(Color.red);
}
}
}
g.drawString(((Node) cities.get(i)).city, (((Node) cities.get(i)).p.x)/2 +250, (((Node) cities.get(i)).p.y)/2 +225);
}
g.setColor(Color.red);
g.drawString("Sample", 830, 71);
drawThickLine(g, 830, 93, 880, 93, 2, Color.GRAY);
drawThickLine(g, 830, 113, 880, 113, 2, Color.GREEN);
if (contents2 != null && j < contents2.length) {
for (int t = 0; t <= j; t++) {
String cord[] = contents2[t].split(",");
for (int k = 0; k < cord.length; k++) {
drawThickLine(g, Integer.parseInt((cord[k].split("/")[0]))/2 +250, (Integer.parseInt(cord[k].split("/")[1]))/2 +225, (Integer.parseInt(cord[k].split("/")[2]))/2 +250, (Integer.parseInt(cord[k].split("/")[3]))/2 +225, 2, Color.GREEN);
}
}
}
if (contents2 != null && j < contents2.length) {
String cord[] = contents2[j].split(",");
for (int k = 0; k < cord.length; k++) {
drawThickLine(g, (Integer.parseInt(cord[k].split("/")[0]))/2 +250, (Integer.parseInt(cord[k].split("/")[1]))/2 +225, (Integer.parseInt(cord[k].split("/")[2]))/2 +250, (Integer.parseInt(cord[k].split("/")[3]))/2 +225, 2, Color.GRAY);
}
if (jComboBox4.getSelectedItem().toString().equals("Shortest distance")) {
jLabel5.setText("Open cities:" + opennodes[j].replaceFirst(",", ""));
jLabel12.setText("Distance from start city:" + from_root[j]);
jLabel13.setText("Estimated distance to destination:" + to_dest[j]);
} else {
jLabel5.setText("Open cities:" + opennodes[j].replaceFirst(",", ""));
jLabel12.setText("Links from start city:" + from_root[j]);
jLabel13.setText("Estimated fewest links to destination:" + to_dest[j]);
}
j++;
}
}
//function courtesy: http://www.rgagnon.com/javadetails/java-0260.html
public void drawThickLine(
Graphics g, int x1, int y1, int x2, int y2, int thickness, Color c) {
// The thick line is in fact a filled polygon
g.setColor(c);
int dX = x2 - x1;
int dY = y2 - y1;
// line length
double lineLength = Math.sqrt(dX * dX + dY * dY);
double scale = (double) (thickness) / (2 * lineLength);
// The x,y increments from an endpoint needed to create a rectangle...
double ddx = -scale * (double) dY;
double ddy = scale * (double) dX;
ddx += (ddx > 0) ? 0.5 : -0.5;
ddy += (ddy > 0) ? 0.5 : -0.5;
int dx = (int) ddx;
int dy = (int) ddy;
// Now we can compute the corner points...
int xPoints[] = new int[4];
int yPoints[] = new int[4];
xPoints[0] = x1 + dx;
yPoints[0] = y1 + dy;
xPoints[1] = x1 - dx;
yPoints[1] = y1 - dy;
xPoints[2] = x2 - dx;
yPoints[2] = y2 - dy;
xPoints[3] = x2 + dx;
yPoints[3] = y2 + dy;
g.fillPolygon(xPoints, yPoints, 4);
}
//initialization
private void customcall() {
try {
obj.FNAME_input_connections = jTextField2.getText();
obj.FNAME_input_locations = jTextField1.getText();
obj.open();
obj.read();
obj.close();
} catch (IOException ex) {
// Logger.getLogger(AI1.class.getName()).log(Level.SEVERE, null, ex);
}
cities = obj.getCities();
for (int i = 0; i < cities.size(); i++) {
jComboBox1.addItem(((Node) cities.get(i)).city.toString());
jComboBox2.addItem(((Node) cities.get(i)).city.toString());
jComboBox3.addItem(((Node) cities.get(i)).city.toString());
}
contents = obj.connections.split(",");
repaint();
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JComboBox jComboBox2;
private javax.swing.JComboBox jComboBox3;
private javax.swing.JComboBox jComboBox4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel12;
private javax.swing.JLabel jLabel13;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment