Extend the graph drawing program Graph.java, GraphPanel.java adding the followin
ID: 3772460 • Letter: E
Question
Extend the graph drawing program Graph.java, GraphPanel.java adding the following features:
Add a button that when pressed prints the path from node X to node Y, where nodes X and Y are specified by the user with text fields or dialog boxes. If a path from X to Y does not exist the program should indicate this. The path should be printed as a list of nodes from X to Y.
Use a recursive method to find the path based on Path.java.
The graph may include loops (the graph search method should avoid going into infinite loops).
All code is at the bottom:
Graph.java:
GraphPanel.java:
Path.java:
Graph.java : http://pastebin.com/fusi2Z61
GraphPanel.java : http://pastebin.com/APGtVK6N
Path.java : http://pastebin.com/BkNAJwuf
Explanation / Answer
GraphPanel.java
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class GraphPanel extends JPanel
{
public GraphPanel(ToolBar aToolBar, Graph aGraph)
{
toolBar = aToolBar;
graph = aGraph;
setBackground(Color.WHITE);
addMouseListener(new
MouseAdapter()
{
public void mousePressed(MouseEvent event)
{
Point2D mousePoint = event.getPoint();
Node n = graph.findNode(mousePoint);
Edge e = graph.findEdge(mousePoint);
Object tool = toolBar.getSelectedTool();
if (tool == null) // select
{
if (e != null)
{
selected = e;
}
else if (n != null)
{
selected = n;
dragStartPoint = mousePoint;
dragStartBounds = n.getBounds();
}
else
{
selected = null;
}
}
else if (tool instanceof Node)
{
Node prototype = (Node) tool;
Node newNode = (Node) prototype.clone();
boolean added = graph.add(newNode, mousePoint);
if (added)
{
selected = newNode;
dragStartPoint = mousePoint;
dragStartBounds = newNode.getBounds();
}
else if (n != null)
{
selected = n;
dragStartPoint = mousePoint;
dragStartBounds = n.getBounds();
}
}
else if (tool instanceof Edge)
{
if (n != null) rubberBandStart = mousePoint;
}
lastMousePoint = mousePoint;
repaint();
}
public void mouseReleased(MouseEvent event)
{
Object tool = toolBar.getSelectedTool();
if (rubberBandStart != null)
{
Point2D mousePoint = event.getPoint();
Edge prototype = (Edge) tool;
Edge newEdge = (Edge) prototype.clone();
if (graph.connect(newEdge,
rubberBandStart, mousePoint))
selected = newEdge;
}
validate();
repaint();
lastMousePoint = null;
dragStartBounds = null;
rubberBandStart = null;
}
});
addMouseMotionListener(new
MouseMotionAdapter()
{
public void mouseDragged(MouseEvent event)
{
Point2D mousePoint = event.getPoint();
if (dragStartBounds != null)
{
if (selected instanceof Node)
{
Node n = (Node) selected;
Rectangle2D bounds = n.getBounds();
n.translate(
dragStartBounds.getX() - bounds.getX()
+ mousePoint.getX() - dragStartPoint.getX(),
dragStartBounds.getY() - bounds.getY()
+ mousePoint.getY() - dragStartPoint.getY());
}
}
lastMousePoint = mousePoint;
repaint();
}
});
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Rectangle2D bounds = getBounds();
Rectangle2D graphBounds = graph.getBounds(g2);
graph.draw(g2);
if (selected instanceof Node)
{
Rectangle2D grabberBounds = ((Node) selected).getBounds();
drawGrabber(g2, grabberBounds.getMinX(), grabberBounds.getMinY());
drawGrabber(g2, grabberBounds.getMinX(), grabberBounds.getMaxY());
drawGrabber(g2, grabberBounds.getMaxX(), grabberBounds.getMinY());
drawGrabber(g2, grabberBounds.getMaxX(), grabberBounds.getMaxY());
}
if (selected instanceof Edge)
{
Line2D line = ((Edge) selected).getConnectionPoints();
drawGrabber(g2, line.getX1(), line.getY1());
drawGrabber(g2, line.getX2(), line.getY2());
}
if (rubberBandStart != null)
{
Color oldColor = g2.getColor();
g2.setColor(PURPLE);
g2.draw(new Line2D.Double(rubberBandStart, lastMousePoint));
g2.setColor(oldColor);
}
}
public void removeSelected()
{
if (selected instanceof Node)
{
graph.removeNode((Node) selected);
}
else if (selected instanceof Edge)
{
graph.removeEdge((Edge) selected);
}
selected = null;
repaint();
}
public static void drawGrabber(Graphics2D g2, double x, double y)
{
final int SIZE = 5;
Color oldColor = g2.getColor();
g2.setColor(PURPLE);
g2.fill(new Rectangle2D.Double(x - SIZE / 2,
y - SIZE / 2, SIZE, SIZE));
g2.setColor(oldColor);
}
public Dimension getPreferredSize()
{
Rectangle2D bounds
= graph.getBounds((Graphics2D) getGraphics());
return new Dimension(
(int) bounds.getMaxX(),
(int) bounds.getMaxY());
}
private Graph graph;
private ToolBar toolBar;
private Point2D lastMousePoint;
private Point2D rubberBandStart;
private Point2D dragStartPoint;
private Rectangle2D dragStartBounds;
private Object selected;
private static final Color PURPLE = new Color(0.7f, 0.4f, 0.7f);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.