Class GraphPanel<V,E>

Type Parameters:
V - The graph node data type.
E - The graph edge data type.
All Implemented Interfaces:
MouseListener, MouseMotionListener, ImageObserver, MenuContainer, Serializable, EventListener, Accessible

public class GraphPanel<V,E> extends JPanel implements MouseListener, MouseMotionListener
Simple component that visualises the nodes and edges in a UniqueGraph. The user able to move the graph nodes around by dragging. In addition attached edges will be highlighted when a node is selected. Nodes are initially placed randomly at random positions on the canvas.
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      Serial ID.
      See Also:
    • nodes

      private List<GraphPanel<V,E>.Node> nodes
      List of nodes to render.
    • edges

      private List<GraphPanel<V,E>.Edge> edges
      List of edges to render.
    • lastLocation

      private Point lastLocation
      Last location the user held down the mouse. This will be null when the user is not dragging the mouse.
    • activeNode

      private GraphPanel<V,E>.Node activeNode
      The currently selected node.
    • nodeLabel

      private Function<V,String> nodeLabel
      Function to convert node data to a string to show to the user.
    • edgeLabel

      private Function<E,String> edgeLabel
      Function to convert edge data to a string to show to the user.
    • directed

      private boolean directed
      True if the graph should be drawn as a directed graph.
  • Constructor Details

    • GraphPanel

      public GraphPanel(UniqueGraph<V,E> graph)
      Constructs a new graph panel for the given graph. Data is converted to string form for display purposes using a call to the standard Object.toString() method.
      Parameters:
      graph - The graph to visualise.
      See Also:
    • GraphPanel

      public GraphPanel(UniqueGraph<V,E> graph, Function<V,String> nodeLabel, Function<E,String> edgeLabel)
      Constructs a new graph panel for the given graph and with the given functions to convert graph data to string form.
      Parameters:
      graph - The graph to visualise.
      nodeLabel - The function to use to convert graph node data to a string to display to the user.
      edgeLabel - The function to use to convert graph edge data to a string to display to the user.
      See Also:
  • Method Details

    • setDirected

      public void setDirected(boolean directed)
      Sets whether the graph is directed. Although by definition the input UniqueGraph is directed, setting this to false will make it so arrow heads are not drawn and that a directed edge in both directions between two nodes is not drawn as a curved arc.
      Parameters:
      directed - True to draw the directed properties of the graph, false to not draw these features.
    • setGraph

      public void setGraph(UniqueGraph<V,E> graph)
      Sets the graph to display. This will replace the previous graph if any.
      Parameters:
      graph - The new graph to display.
    • findNode

      private GraphPanel<V,E>.Node findNode(Point p)
      Finds the first node that contains the given point. If such a node exists.
      Parameters:
      p - The point to check.
      Returns:
      The first node that contains the given point, or null if no such node exists.
    • paintComponent

      public void paintComponent(Graphics g1)
      Overrides:
      paintComponent in class JComponent
    • mouseDragged

      public void mouseDragged(MouseEvent e)
      Specified by:
      mouseDragged in interface MouseMotionListener
    • mouseMoved

      public void mouseMoved(MouseEvent e)
      Specified by:
      mouseMoved in interface MouseMotionListener
    • mouseClicked

      public void mouseClicked(MouseEvent e)
      Specified by:
      mouseClicked in interface MouseListener
    • mousePressed

      public void mousePressed(MouseEvent e)
      Specified by:
      mousePressed in interface MouseListener
    • mouseReleased

      public void mouseReleased(MouseEvent e)
      Specified by:
      mouseReleased in interface MouseListener
    • mouseEntered

      public void mouseEntered(MouseEvent e)
      Specified by:
      mouseEntered in interface MouseListener
    • mouseExited

      public void mouseExited(MouseEvent e)
      Specified by:
      mouseExited in interface MouseListener
    • show

      public static <V, E> void show(UniqueGraph<V,E> graph, Function<V,String> nodeLabel, Function<E,String> edgeLabel)
      Utility subroutine to show the given graph in a JFrame.
      Type Parameters:
      V - The vertex data type.
      E - The edge data type.
      Parameters:
      graph - The graph to display.
      nodeLabel - The function to use to determine node labels.
      edgeLabel - The function to use to determine edge labels.
    • show

      public static <V, E> void show(UniqueGraph<V,E> graph)
      Utility subroutine to show the given graph in a JFrame.
      Type Parameters:
      V - The vertex data type.
      E - The edge data type.
      Parameters:
      graph - The graph to display.
    • show

      public static void show(CPQ q)
      Utility subroutine to show the query graph of the given CPQ in a JFrame.
      Parameters:
      q - The CPQ to display.
    • show

      public static void show(QueryGraphCPQ cpq)
      Utility subroutine to show the given CPQ query graph in a JFrame.
      Parameters:
      cpq - The CPQ query graph to display.