package graphpackage;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Random;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
public class GraphPanel extends JComponent
{
private static final int WIDTH = 800;
private static final int HIGHT = 600;
private static final int RADIUS = 15;
private static final Random rnd = new Random();
private ControlPanel control = new ControlPanel();
private int radius = RADIUS;
private Kind kind = Kind.Circular;
private ArrayList<Node> nodes = new ArrayList<Node>();
private ArrayList<Node> selected = new ArrayList<Node>();
private ArrayList<Edge> edges = new ArrayList<Edge>();
private Point mousePt = new Point(WIDTH / 2, HIGHT / 2);
private Rectangle mouseRect = new Rectangle();
private boolean selecting = false;
public static void main(String[] args) throws Exception
{
String url = "jdbc:mysql://localhost:3306/";
String dbName = "student";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try
{
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url+dbName,userName,password);
Statement st = conn.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM node");
while (res.next())
{
int id = res.getInt("node_id");
String n = res.getString("name");
String c = res.getString("city");
String g = res.getString("gender");
String j = res.getString("job");
String o = res.getString("organisation");
String r = res.getString("relationship");
System.out.println(id + "\t" + n+ "\t" + c+ "\t" + g+ "\t" + j+ "\t" +o+ "\t" +r);
}
int val = st.executeUpdate("INSERT into node VALUES("+3+","+"'Sonu'"+","+"'Hydrabad'"+","+"'F'"+","+"'Assistent Professor'"+","+"'IIIT'"+","+"'Married'"+")");
if(val==1)
System.out.print("Successfully inserted value");
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame f = new JFrame("Graph Simulator");
f.setDefaultCloseOperation(JFrame.EXITONCLOSE);
GraphPanel gp = new GraphPanel();
f.add(gp.control, BorderLayout.NORTH);
f.add(new JScrollPane(gp), BorderLayout.CENTER);
f.getRootPane().setDefaultButton(gp.control.defaultButton);
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
});
}
public GraphPanel()
{
this.setOpaque(true);
this.addMouseListener(new MouseHandler());
this.addMouseMotionListener(new MouseMotionHandler());
}
public Dimension getPreferredSize()
{
return new Dimension(WIDTH, HIGHT);
}
public void paintComponent(Graphics g)
{
g.setColor(new Color(0x00f0f0f0));
g.fillRect(0, 0, getWidth(), getHeight());
for (Edge e : edges)
{
e.draw(g);
}
for (Node n : nodes)
{
n.draw(g);
}
if (selecting)
{
g.setColor(Color.darkGray);
g.drawRect(mouseRect.x, mouseRect.y,mouseRect.width, mouseRect.height);
}
}
private class MouseHandler extends MouseAdapter
{ public void mouseReleased(MouseEvent e)
{
selecting = false;
mouseRect.setBounds(0, 0, 0, 0);
if (e.isPopupTrigger())
{
showPopup(e);
}
e.getComponent().repaint();
}
public void mousePressed(MouseEvent e)
{
mousePt = e.getPoint();
if (e.isShiftDown())
{
Node.selectToggle(nodes, mousePt);
} else if (e.isPopupTrigger())
{
Node.selectOne(nodes, mousePt);
showPopup(e);
}
else if (Node.selectOne(nodes, mousePt))
{
selecting = false;
}
else
{
Node.selectNone(nodes);
selecting = true;
}
e.getComponent().repaint();
}
private void showPopup(MouseEvent e)
{
control.popup.show(e.getComponent(), e.getX(), e.getY());
}
}
private class MouseMotionHandler extends MouseMotionAdapter
{
Point delta = new Point();
public void mouseDragged(MouseEvent e)
{
if (selecting)
{
mouseRect.setBounds(Math.min(mousePt.x, e.getX()),Math.min(mousePt.y,e.getY()),Math.abs(mousePt.x - e.getX()),Math.abs(mousePt.y - e.getY()));
Node.selectRect(nodes, mouseRect);
}
else
{
delta.setLocation(e.getX() - mousePt.x,e.getY() - mousePt.y);Node.updatePosition(nodes, delta);
mousePt = e.getPoint();
}
e.getComponent().repaint();
}
}
public JToolBar getControlPanel()
{
return control;
}
private class ControlPanel extends JToolBar
{
JTextField noOfNodes= new JTextField(10);
private Action newNode = new NewNodeAction("New");
private Action clearAll = new ClearAction("Clear");
private Action kind = new KindComboAction("Kind");
private Action color = new ColorAction("Color");
private Action connect = new ConnectAction("Connect");
private Action delete = new DeleteAction("Delete");
private Action random = new RandomAction("Create",noOfNodes);
private JButton defaultButton = new JButton(newNode);
private JComboBox kindCombo = new JComboBox();
private ColorIcon hueIcon = new ColorIcon(Color.blue);
private JPopupMenu popup = new JPopupMenu();
final JButton button = new JButton("Options");
ControlPanel()
{
this.setLayout(new FlowLayout(FlowLayout.LEFT));
this.setBackground(Color.lightGray);
this.add(new JLabel("No Of Nodes :"));
this.add(noOfNodes);
this.add(new JButton(random));
this.add(button);
this.add(defaultButton);
this.add(new JButton(clearAll));
this.add(kindCombo);
this.add(new JButton(color));
this.add(new JLabel(hueIcon));
JSpinner js = new JSpinner();
js.setModel(new SpinnerNumberModel(RADIUS, 5, 100, 5));
js.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
JSpinner s = (JSpinner) e.getSource();
radius = (Integer) s.getValue();
Node.updateRadius(nodes, radius);
GraphPanel.this.repaint();
}
});
this.add(new JLabel("Size:"));
this.add(js);
popup.add(new JMenuItem(newNode));
popup.add(new JMenuItem(color));
popup.add(new JMenuItem(connect));
popup.add(new JMenuItem(delete));
JMenu subMenu = new JMenu("Kind");
for (Kind k : Kind.values())
{
kindCombo.addItem(k);
subMenu.add(new JMenuItem(new KindItemAction(k)));
}
popup.add(subMenu);
kindCombo.addActionListener(kind);
}
class KindItemAction extends AbstractAction
{
private Kind k;
public KindItemAction(Kind k)
{
super(k.toString());
this.k = k;
}
public void actionPerformed(ActionEvent e)
{
kindCombo.setSelectedItem(k);
}
}
}
private class ClearAction extends AbstractAction
{
public ClearAction(String name)
{
super(name);
}
public void actionPerformed(ActionEvent e)
{
nodes.clear();
edges.clear();
repaint();
}
}
private class ColorAction extends AbstractAction
{
public ColorAction(String name)
{
super(name);
}
public void actionPerformed(ActionEvent e)
{
Color color = control.hueIcon.getColor();
color = JColorChooser.showDialog(GraphPanel.this, "Choose a color", color);
if (color != null)
{
Node.updateColor(nodes, color);
control.hueIcon.setColor(color);
control.repaint();
repaint();
}
}
}
private class ConnectAction extends AbstractAction
{
public ConnectAction(String name)
{
super(name);
}
public void actionPerformed(ActionEvent e)
{
Node.getSelected(nodes, selected);
if (selected.size() > 1)
{
for (int i = 0; i < selected.size() - 1; ++i)
{
Node n1 = selected.get(i);
Node n2 = selected.get(i + 1);
edges.add(new Edge(n1, n2));
}
}
repaint();
}
}
private class DeleteAction extends AbstractAction
{
public DeleteAction(String name)
{
super(name);
}
public void actionPerformed(ActionEvent e)
{
ListIterator<Node> iter = nodes.listIterator();
while (iter.hasNext())
{
Node n = iter.next();
if (n.isSelected())
{
deleteEdges(n);
iter.remove();
}
}
repaint();
}
private void deleteEdges(Node n)
{
ListIterator<Edge> iter = edges.listIterator();
while (iter.hasNext())
{
Edge e = iter.next();
if (e.n1 == n || e.n2 == n)
{
iter.remove();
}
}
}
}
private class KindComboAction extends AbstractAction
{
public KindComboAction(String name)
{
super(name);
}
public void actionPerformed(ActionEvent e)
{
JComboBox combo = (JComboBox) e.getSource();
kind = (Kind) combo.getSelectedItem();
Node.updateKind(nodes, kind);
repaint();
}
}
private class NewNodeAction extends AbstractAction
{
public NewNodeAction(String name)
{
super(name);
}
public void actionPerformed(ActionEvent e)
{
Node.selectNone(nodes);
Point p = mousePt.getLocation();
Color color = control.hueIcon.getColor();
Node old=nodes.get(nodes.size()-1);
Node n = new Node(p, radius, color, kind,old.nodeNumber()+1);
n.setSelected(true);
nodes.add(n);
repaint();
}
}
private class RandomAction extends AbstractAction
{
JTextField noOfNodes;
public RandomAction(String name,JTextField t)
{
super(name);
noOfNodes=t;
}
public void actionPerformed(ActionEvent e)
{
int num=Integer.parseInt(noOfNodes.getText());
Color color = control.hueIcon.getColor();
for (int i = 0; i < num; i++)
{ if (i== 15) break;
Point p = new Point(rnd.nextInt(getWidth()), rnd.nextInt(getHeight()));
nodes.add(new Node(p, radius, color, kind,(i+1)));
}
repaint();
}
}
/*** The kinds of node in a graph.*/
private enum Kind
{
Circular, Rounded, Square;
}
/*** An Edge is a pair of Nodes. */
private static class Edge
{
private Node n1;
private Node n2;
public Edge(Node n1, Node n2)
{
this.n1 = n1;
this.n2 = n2;
}
public void draw(Graphics g)
{
Point p1 = n1.getLocation();
Point p2 = n2.getLocation();
g.setColor(Color.darkGray);
g.drawLine(p1.x, p1.y, p2.x, p2.y);
}
}
/*** A Node represents a node in a graph. */
private static class Node
{
private Point p;
private int r;
private int nodenum;
private Color color;
private Kind kind;
private boolean selected = false;
private Rectangle b = new Rectangle();
/*** Construct a new node. */
public Node(Point p, int r, Color color, Kind kind,int n)
{
this.p = p;
this.r = r;
this.color = color;
this.kind = kind;
nodenum=n;
setBoundary(b);
}
/** * Calculate this node's rectangular boundary. */
private void setBoundary(Rectangle b)
{
b.setBounds(p.x - r, p.y - r, 2 * r, 2 * r);
}
/** * Draw this node. */
public void draw(Graphics g)
{
g.setColor(this.color);
if (this.kind == Kind.Circular)
{
g.fillOval(b.x, b.y, b.width, b.height);
g.setColor(Color.white);
g.drawString(String.valueOf(nodenum),b.x+r,b.y+r);
}
else if (this.kind == Kind.Rounded)
{
g.fillRoundRect(b.x, b.y, b.width, b.height, r, r);
g.setColor(Color.white);
g.drawString(String.valueOf(nodenum),b.x+r,b.y+r);
}
else if (this.kind == Kind.Square)
{
g.fillRect(b.x, b.y, b.width, b.height);
g.setColor(Color.white);
g.drawString(String.valueOf(nodenum),b.x+r,b.y+r);
}
if (selected)
{
g.setColor(Color.darkGray);
g.drawRect(b.x, b.y, b.width, b.height);
}
}
/** * Return this node's location. */
public Point getLocation()
{
return p;
}
public int nodeNumber()
{
return nodenum;
}
/** * Return true if this node contains p. */
public boolean contains(Point p)
{
return b.contains(p);
}
/** * Return true if this node is selected. */
public boolean isSelected()
{
return selected;
}
/** * Mark this node as selected. */
public void setSelected(boolean selected)
{
this.selected = selected;
}
/** * Collected all the selected nodes in list. */
public static void getSelected(List<Node> list, List<Node> selected)
{
selected.clear();
for (Node n : list)
{
if (n.isSelected())
{
selected.add(n);
}
}
}
/** * Select no nodes. */
public static void selectNone(List<Node> list)
{
for (Node n : list)
{
n.setSelected(false);
}
}
/** * Select a single node; return true if not already selected. */
public static boolean selectOne(List<Node> list, Point p)
{
for (Node n : list)
{
if (n.contains(p))
{
if (!n.isSelected())
{
Node.selectNone(list);
n.setSelected(true);
}
return true;
}
}
return false;
}
/** * Select each node in r. */
public static void selectRect(List<Node> list, Rectangle r)
{
for (Node n : list)
{
n.setSelected(r.contains(n.p));
}
}
/** * Toggle selected state of each node containing p. */
public static void selectToggle(List<Node> list, Point p)
{
for (Node n : list)
{
if (n.contains(p))
{
n.setSelected(!n.isSelected());
}
}
}
/** * Update each node's position by d (delta). */
public static void updatePosition(List<Node> list, Point d)
{
for (Node n : list)
{
if (n.isSelected())
{
n.p.x += d.x;
n.p.y += d.y;
n.setBoundary(n.b);
}
}
}
/** * Update each node's radius r. */
public static void updateRadius(List<Node> list, int r)
{
for (Node n : list)
{
if (n.isSelected())
{
n.r = r;
n.setBoundary(n.b);
}
}
}
/** * Update each node's color. */
public static void updateColor(List<Node> list, Color color)
{
for (Node n : list)
{
if (n.isSelected())
{
n.color = color;
}
}
}
/** * Update each node's kind. */
public static void updateKind(List<Node> list, Kind kind)
{
for (Node n : list)
{
if (n.isSelected())
{
n.kind = kind;
}
}
}
}
private static class ColorIcon implements Icon
{
private static final int WIDE = 20;
private static final int HIGH = 20;
private Color color;
public ColorIcon(Color color)
{
this.color = color;
}
public Color getColor()
{
return color;
}
public void setColor(Color color)
{
this.color = color;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
g.setColor(color);
g.fillRect(x, y, WIDE, HIGH);
}
public int getIconWidth()
{
return WIDE;
}
public int getIconHeight()
{
return HIGH;
}
}
}