|
|
package com.AI; import com.DBpackage.MyDatabase;
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.sql.SQLException; import java.util.ArrayList; import java.util.Set;
class MySwing extends JFrame { private MyDatabase myDatabase; private ID3 id3; private NaryTree decisionTree;
MySwing(MyDatabase myDatabase) { this.myDatabase = myDatabase; this.myDatabase.connectDatabase(); id3 = new ID3(); decisionTree = new NaryTree(); }
private class InsertPanel extends JPanel //means panel for insert pane, not inserting a panel
{ private JLabel label_roll; private JLabel label_fname; private JLabel label_lname; private JLabel label_marks; private JLabel c; InsertPanel() { setLayout(new FlowLayout()); c = new JLabel(); label_roll =new JLabel(); label_fname =new JLabel(); label_lname =new JLabel(); label_marks =new JLabel(); JTextField roll = new JTextField(10); JTextField fname = new JTextField(10); JTextField lname = new JTextField(10); JTextField marks = new JTextField(10);
c.setText("Hello"); label_roll.setText("Roll Number: "); label_fname.setText("First Name: "); label_lname.setText("Last Name: "); label_marks.setText("Marks: ");
JButton submit = new JButton(); JButton reset = new JButton(); submit.setText("Submit"); reset.setText("Reset");
add(label_roll); add(roll); add(label_fname); add(fname); add(label_lname); add(lname); add(label_marks); add(marks); //add(ps);
add(submit); add(reset); add(c);
submit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int r,m; String fn,ln; c.setText("Button clicked!");
/*preparedStatement.setString(1, "Gary"); preparedStatement.setString(2, "Larson"); preparedStatement.setLong (3, 123);*/ //myDatabase.insertValues(sql);
r = Integer.parseInt(roll.getText()); m = Integer.parseInt(marks.getText()); fn = fname.getText(); ln = lname.getText(); myDatabase.insertValues(r,fn,ln,m); //myDatabase.deleteValues(107);
//myDatabase.updateValues("Henry",106);
} }); reset.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { roll.setText(""); fname.setText(""); lname.setText(""); marks.setText(""); c.setText("Cleared!"); } }); }
}
private class UpdatePanel extends JPanel { UpdatePanel() { JButton upd = new JButton(); JLabel new_fname_label = new JLabel(); JLabel id_label = new JLabel(); JTextField new_fname = new JTextField(10); JTextField id = new JTextField(10);
upd.setText("Update"); new_fname_label.setText("New first name: "); id_label.setText("Enter the id of the record to be updated: ");
add(new_fname_label); add(new_fname); add(id_label); add(id); add(upd); upd.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String name = new_fname.getText(); int i = Integer.parseInt(id.getText()); myDatabase.updateValues(name, i); } }); } }
private class DeletePanel extends JPanel // panel for delete not deleting a panel
{ DeletePanel() { JLabel id_label = new JLabel(); JTextField id = new JTextField(10); JButton delete_but = new JButton();
id_label.setText("Enter id of the record to be deleted"); delete_but.setText("Delete");
add(id_label); add(id); add(delete_but);
delete_but.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int i = Integer.parseInt(id.getText()); myDatabase.deleteValues(i); } }); } }
private class DisplayPanel extends JPanel { private JButton show; private JLabel show_label; DisplayPanel() { setLayout(new FlowLayout()); show = new JButton(); show_label = new JLabel(); show.setText("Display"); show_label.setText("Click to display table"); add(show_label); add(show); show.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { myDatabase.displayValues(); } }); }
}
private class StorePanel extends JPanel { private JButton store; private JLabel store_label; StorePanel() {
store = new JButton(); store_label = new JLabel();
store.setText("Store"); store_label.setText("Click to store and display table"); add(store_label); add(store); store.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { JTable table = new JTable(); Row climate; ArrayList<Row> climateArrayList = myDatabase.storeValues();
id3 = new ID3(); // id3.setTableData(climateArrayList);
//id3.createNode();
// for (Row c : climateArrayList) {
// System.out.println(c.getDay() + " " +
// c.getWeather() + " " + c.getTemperature() + " " +
// c.getHumidity() + " " + c.getWind());
// }
} catch(SQLException se) { se.printStackTrace(); } } }); }
}
private class DecisionTreePanel extends JPanel { private static final int DISTANCE_X = 4; private static final int DISTANCE_Y = 65; private static final int radius = 20; private static final int diameter = radius * 2; private int treeArity = 3; private int treeDepth = 3; private int childDistanceX;
private JPanel treeDisplayPanel; char status;
private NaryTree createDecisionTree() { NaryTree tree = new NaryTree(); try { ArrayList<Row> examples = myDatabase.storeValues(); for (Row row : examples) { for (Cell cell : row.getCellsList()) { System.out.print(cell.getAttributeName() + " " + cell.getValue() + ", "); } System.out.println(); } Set<String> attributesSet = id3.getUniqueAttributes("Play", "Day", examples);
id3.setAttributesNew(examples); id3.setModeAndPN(examples); id3.createDecisionTree(tree, attributesSet, examples); tree.displayTree(); myDatabase.deleteTree(); tree.dbTest(myDatabase);
} catch (SQLException se) { se.printStackTrace(); } return tree; } DecisionTreePanel() { // setLayout(new FlowLayout());
// setSize(800,600);
status = 'n'; JButton displayButton = new JButton(); JButton clearButton = new JButton(); JButton createTreeButton = new JButton(); displayButton.setText("Display Tree"); clearButton.setText("Clear"); createTreeButton.setText("Create Tree"); displayButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { status = 'y'; repaint(); } }); clearButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { status = 'n'; repaint(); } }); createTreeButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { decisionTree = createDecisionTree(); } }); add(displayButton); add(clearButton); add(createTreeButton); childDistanceX = 100; }
public void paintComponent(Graphics g) { super.paintComponent(g); // System.out.println("Inside paintcomponent");
Dimension size = getSize(); int x = size.width / 2; int y = ( treeDepth * DISTANCE_Y) / 2;
// drawTree(g, x, y, treeArity, treeDepth);
if(status == 'y') { // g.drawOval(x,y,diameter,diameter);
System.out.println("Inside if"); // drawMyTree(g, x, y);
displayTree(g,x,y); } }
// public void drawTree(Graphics g, int x, int y, int arity, int depth) {
// if (depth > 0) {
// int childDistanceX = (int) (Math.pow(arity, depth) * DISTANCE_X);
// int childX = x - (arity - 1) * childDistanceX / 2;
// int childY = y - DISTANCE_Y;
//
// for (int child = 0; child < arity; child++) {
// g.drawLine(x, y, childX, childY);
// drawTree(g, childX, childY, arity, depth - 1);
// childX += childDistanceX;
// }
// }
// }
public void drawMyTree(Graphics g, int x, int y) { System.out.println("Inside drawmytree"); g.drawOval(x,y,diameter,diameter); int childX = x - childDistanceX / 2; int childY = y + DISTANCE_Y; g.drawLine(x + radius ,y + diameter,childX + childDistanceX + radius,childY); g.drawLine(x + radius ,y + diameter,childX + radius,childY);
g.drawOval(childX,childY,diameter,diameter); g.drawOval(childX+childDistanceX,childY,diameter,diameter);
}
//////////////////////////////////////////////////////////////////////////////////
private void traverse(Node node, Graphics g, int x, int y, int i) { if(node == null) { System.out.println(); return; } int childX = x - childDistanceX; int childY = y + DISTANCE_Y; i++; g.drawOval(x,y,diameter,diameter); if(node.getAttributeName().equalsIgnoreCase("Yes") || node.getAttributeName().equalsIgnoreCase("No")) if(node.getValuesList() == null) { return; } for(Node.Value v: node.getValuesList()) { // g.drawOval(childX,y,diameter,diameter);
g.drawLine(x+radius, y + diameter,childX + radius, childY); traverse(v.getNext(), g, childX, childY, i); childX = childX + childDistanceX; }
} void displayTree(Graphics g, int x, int y) { System.out.println("Displaying tree"); if(decisionTree.getHead()!=null) { int i = 0; traverse(decisionTree.getHead(), g, x, y, i); } else { System.out.println("Head node not created/initialized"); } } //////////////////////////////////////////////////////////////////////////////////////////////////////////
}
public void start() { JTabbedPane jtp = new JTabbedPane();
//This creates the template on the windowed application that we will be using
jtp.setBounds(10,10,1030,640);
InsertPanel insertPanel = new InsertPanel(); com.AI.MySwing.UpdatePanel updatePanel = new com.AI.MySwing.UpdatePanel(); com.AI.MySwing.DeletePanel deletePanel = new com.AI.MySwing.DeletePanel(); com.AI.MySwing.DisplayPanel displayPanel = new com.AI.MySwing.DisplayPanel(); com.AI.MySwing.StorePanel storePanel = new com.AI.MySwing.StorePanel(); DecisionTreePanel decisionTreePanel = new DecisionTreePanel();
//This adds the first and second tab to our tabbed pane object and names it
jtp.addTab("Insert", insertPanel); jtp.addTab("Update", updatePanel); jtp.addTab("Delete", deletePanel); jtp.addTab("Display", displayPanel); jtp.addTab("Store", storePanel); jtp.addTab("Decision-Tree", decisionTreePanel);
getContentPane().add(jtp); setLayout(null); setVisible(true); setSize(1050,700); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) {
System.out.println("Closing"); myDatabase.closeConnection(); dispose(); }
}); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); }
}
|