⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 visualgraph.java

📁 OpenJGraph是一个开源的Java库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package salvo.jesus.graph.visual;import java.util.*;import java.awt.*;import java.awt.geom.*;import java.io.*;import salvo.jesus.graph.*;import salvo.jesus.graph.listener.*;import salvo.jesus.graph.visual.layout.*;import salvo.jesus.graph.visual.drawing.*;/** * The VisualGraph encapsulates a Graph object with the necessary * attributes and methods for drawing the entire graph on one or more containers. * * @author		Jesus M. Salvo Jr. * @see         salvo.jesus.graph.Graph */public class VisualGraph extends NullGraphListener  implements Serializable {  /**   * The Graph object that the VisualGraph encapsulates.   */  private Graph		graph;  /**   * A List of containers where the graph will be drawn.   */  private java.util.List      containers;  /**   * A List of VisualVertex that corresponds to the vertices in the   * graph that VisualGraph encapsulates.   */  private java.util.List      visualVertices;  /**   * A List of VisualEdge that corresponds to the edges in the   * graph that VisualGraph encapsulates.   */  private java.util.List      visualEdges;  /**   * Factory for creating painters for VisualVertices   */  private VisualVertexPainterFactory    vertexPainterFactory;  /**   * Factory for creating painters for VisualEdges   */  private VisualEdgePainterFactory      edgePainterFactory;  /**   * Factory for creating editors for VisualVertex and VisualEdges   */  private VisualGraphComponentEditorFactory  editorFactory;  /**   * The layout manager to layout the vertices in the graph.   */  private GraphLayoutManager  layoutManager;  int     linetype = salvo.jesus.graph.visual.VisualEdge.STRAIGHT_LINE;  /**   * Creates a new, empty VisualGraph object. You need to later on   * call setGraph() to set the Graph that the VisualGraph object   * will encapsulate.   */  public VisualGraph( ) {    this.containers = new ArrayList( 10 );    this.visualVertices = new ArrayList( 10 );    this.visualEdges = new ArrayList( 10 );    this.vertexPainterFactory = VisualVertexPainterFactoryImpl.getInstance();    this.edgePainterFactory = VisualEdgePainterFactoryImpl.getInstance();  }  public VisualGraph(Graph g){    this();    this.setGraph(g);  }  /**   * Returns the Containers that are registered with the VisualGraph object   * where VisualGraph will be drawn.   *   * @return	List of Containers where VisualGraph will be drawn.   * @see			#addContainer( Container )   * @see			#removeContainer( Container )   */  public java.util.List getContainers( ) {    return this.containers;  }  /**    * Registers a Container for drawing the VisualGraph. The next repaint()    * on the VisualGraph object will draw the VisualGraph on the added container.    * For the moment, limit the types to be added to be instances of GraphScrollPane.    *    * @param		c		Additional Container where VisualGraph will be drawn    * @see			#removeContainer( Container )    */  public void addContainer( Container c ) {    containers.add( c );  }  /**    * Removes a Container from drawing of the VisualGraph object. this will not    * necessarily clear or remove the existing paint on the Container. You have    * to clear the Container yourself.    *    * @param		c		Container where VisualGraph will no longer be drawn    * @see			#addContainer( Container )    */  public void removeContainer( Container c ) {    containers.remove( c );  }  /**    * Determines the dimension that contains all the VisualGraph's VisualVertex.    *    * @return	Dimension object that represents the dimension that covers    * all the VisualVertex in the VisualGraph.    */  public Dimension getMaxSize() {    VisualVertex	vvertex;    Dimension		  vvertexdimension;    Rectangle		  rectangle;    Iterator		  iterator;    int					  maxwidth = 0, maxheight = 0;    iterator = visualVertices.iterator();    while( iterator.hasNext()) {      vvertex = (VisualVertex) iterator.next();      rectangle = vvertex.getBounds();      vvertexdimension = rectangle.getSize();      if( rectangle.getY() + vvertexdimension.getHeight() > maxheight )        maxheight = (int) (rectangle.getY() + vvertexdimension.getHeight());      if( rectangle.getX() + vvertexdimension.getWidth() > maxwidth )        maxwidth = (int) (rectangle.getX() + vvertexdimension.getWidth());    }    return new Dimension( maxwidth, maxheight );  }  /**    * Determines the starting point where a VisualVertex is drawn.    *    * @return	Point object of the VisualVertex which has the minimum x and y    * coordinate.    */  public Point getStartDrawPoint() {    VisualVertex	vvertex;    Rectangle		  rectangle;    Iterator		  iterator;    int					  minx = -1, miny = -1;    iterator = visualVertices.iterator();    while( iterator.hasNext()) {      vvertex = (VisualVertex) iterator.next();      rectangle = vvertex.getBounds();      minx = (minx == -1 ? rectangle.x :             (minx < rectangle.x ? minx : rectangle.x ));      miny = (miny == -1 ? rectangle.y :             (miny < rectangle.y ? miny : rectangle.y ));    }    return new Point( minx, miny );  }  /**    * Repaints the VisualGraph in all the registered Containers.    */  public void repaint( ) {    Iterator	iterator;    Container	container;    iterator = containers.iterator();    while( iterator.hasNext()) {      container = (Container) iterator.next();      if( container != null )        container.repaint();    }  }  /**    * Returns the Graph object that the VisualGraph object encapsulates.    *    * @return	Graph object encapsulated by the VisualGraph object    */  public Graph getGraph() {    return graph;  }  /**    * Sets the Graph object that the VisualGraph object encapsulates. If an existing    * Graph object is already encapsulated, the existing Graph object is replaced    * by the new Graph object. This will not however trigger methods in the    * registered listener objects.    *    * @param	graph		The new Graph object that will be encapsulated by the VisualGraph object    */  public void setGraph( Graph graph ) {    Iterator	verticesiterator;    Iterator	edgesiterator;    Vertex		currentvertex;    HashSet		uniqueedges = new HashSet();    // Remove this visualgraph from the graph's listener    if( this.graph != null ) {      this.graph.removeListener( this );    }    // Remove the visual representation of the graph structure    visualEdges.clear( );    visualVertices.clear( );    // Set the graph    this.graph = graph;    // Create visual representation of the existing graph structure    // That is, add elements to visualedges and visualvertices    verticesiterator = this.graph.getVerticesIterator();    while( verticesiterator.hasNext() ){      currentvertex = (Vertex) verticesiterator.next( );      visualVertices.add( new VisualVertex( currentvertex, this ));      // Get the adjacent edges for each vertex in the graph      edgesiterator = this.graph.getEdges( currentvertex ).iterator();      while( edgesiterator.hasNext() ){        // Using a set guarantees that edges are unique        uniqueedges.add( (Edge) edgesiterator.next( ));      }    }    // Now create the visual representation of the edges    edgesiterator = uniqueedges.iterator();    while( edgesiterator.hasNext() ){      visualEdges.add( new VisualEdge( (Edge) edgesiterator.next(), this ));    }    // Add this visualgraph as listener to the new graph.    this.graph.addListener( this );  }  /**   * Returns the layout manager used to layout the vertices of the graph.   */  public GraphLayoutManager getGraphLayoutManager( ) {    return this.layoutManager;  }  /**    * Sets the layout manager to use to layout the vertices of the graph.    *    * @param  layoutmanager   An object implementing the GraphLayoutManager interface.    */  public void setGraphLayoutManager( GraphLayoutManager layoutmanager ) {    this.layoutManager = layoutmanager;  }    /**     * Returns an instance of the factory used to create painters for VisualVertices.     * Unless <tt>setVisualVertexPainterFactory()</tt> was called, the factory     * used by default is <tt>VisualVertexPainterFactoryImpl</tt>.     */    public VisualVertexPainterFactory getVisualVertexPainterFactory() {        return this.vertexPainterFactory;    }    /**     * Sets the factory for creating painters for VisualVertices     */    public void setVisualVertexPainterFactory( VisualVertexPainterFactory newFactory ) {        this.vertexPainterFactory = newFactory;    }    /**     * Returns an instance of the factory used to create painters for VisualEdges.     * Unless <tt>setVisualEdgePainterFactory()</tt> was called, the factory     * used by default is <tt>VisualEdgePainterFactoryImpl</tt>.     */    public VisualEdgePainterFactory getVisualEdgePainterFactory() {        return this.edgePainterFactory;    }    /**     * Sets the factory for creating painters for VisualEdges.     */    public void setVisualEdgePainterFactory( VisualEdgePainterFactory newFactory ) {        this.edgePainterFactory = newFactory;    }    /**     * Returns an instance of the factory used to create custom editors     * for VisualVertex and VisualEdge.     */    public VisualGraphComponentEditorFactory getVisualGraphComponentEditorFactory() {        return this.editorFactory;    }    /**     * Sets the factory for creating custom editors for VisualVertex and VisualEdges.     */    public void setVisualGraphComponentEditorFactory( VisualGraphComponentEditorFactory editorFactory ) {        this.editorFactory = editorFactory;    }    /**     * Returns the List of VisualVertices contained within this VisualGraph     */    public java.util.List getVisualVertices() {        return this.visualVertices;    }    /**     * Returns the List of VisualEdges contained within this VisualGraph     */    public java.util.List getVisualEdges() {        return this.visualEdges;    }  private void setLinetype( int linetype ) {    int i, size = visualEdges.size();    this.linetype = linetype;    for( i = 0; i < size; i++ ) {      ((VisualEdge) this.visualEdges.get( i )).setLinetype( linetype );    }  }  public void setOrthogonalLine(){    this.setLinetype( salvo.jesus.graph.visual.VisualEdge.ORTHOGONAL_LINE );  }  public void setStraightLine(){    this.setLinetype( salvo.jesus.graph.visual.VisualEdge.STRAIGHT_LINE );  }  /**    * Ask the layout manager to layout the vertices of the graph according    * to the rules of the layout manager.    */  public void layout() {    if( this.layoutManager != null ) this.layoutManager.layout();  }  public VisualEdge getVisualEdge( Edge edge ) {    Iterator			iterator;    VisualEdge    visualedge;    iterator = this.visualEdges.iterator();    while( iterator.hasNext( )) {      visualedge = (VisualEdge) iterator.next( );      if( visualedge.getEdge( ) == edge )        return visualedge;    }    return null;  }  public void setVisualEdge( Edge edge, VisualEdge newVisualEdge ) {    VisualEdge  existingVisualEdge = this.getVisualEdge( edge );    if( edge != null && newVisualEdge.getEdge() == edge ) {        this.visualEdges.set(  this.visualEdges.indexOf( existingVisualEdge ), newVisualEdge );    }  }  /**   * Returns the VisualVertex object for the corresponding Vertex object in the   * Graph object encapsulated by VisualGraph.   *   * @param	vertex		The Vertex object whose corresponding VisualVertex object we want   * @return					The corresponding VisualVertx object of the vertex. Returns null if   * vertex in not a Vertex in the Graph encapsulated by the VisualGraph object.   */  public VisualVertex getVisualVertex( Vertex vertex ) {    Iterator			iterator;    VisualVertex  visualvertex;    iterator = visualVertices.iterator();    while( iterator.hasNext( )) {      visualvertex = (VisualVertex) iterator.next( );      if( visualvertex.getVertex( ) == vertex )        return visualvertex;    }    return null;  }  public void setVisualVertex( Vertex vertex, VisualVertex newVisualVertex ) {    VisualVertex    existingVisualVertex = this.getVisualVertex( vertex );    // Replace the VisualVertex of that vertex with a new one.    if( existingVisualVertex != null && vertex == newVisualVertex.getVertex() ) {        this.visualVertices.set(            this.visualVertices.indexOf( existingVisualVertex ), newVisualVertex );    }  }  /**   * Returns a <tt>VisualEdge</tt> whose line is nearest the specified point.   * Only <tt>VisualEdge</tt>s that have at least a distance of 5 are considered,   * from which the <tt>VisualEdge</tt> that has the least distance among them   * are returned.   *   * @param	x		x-coordinate   * @param	y		y-coordinate   * @return	    A <tt>VisualEdge</tt> object that is nearest the specified point   */  public VisualEdge getVisualEdge( int x, int y ){    Iterator    iterator;    VisualEdge  currentVEdge;    VisualEdge  nearestVEdge = null;    double      leastDistance = 5.0;    double      currentDistance;    iterator = visualEdges.iterator();    while( iterator.hasNext() ){        currentVEdge = (VisualEdge) iterator.next();        currentDistance = currentVEdge.ptSegDist( x, y );        if( currentDistance < leastDistance) {            leastDistance = currentDistance;            nearestVEdge = currentVEdge;        }    }    return nearestVEdge;  }  /**   * Adds a Vertex object to the Graph object encapsulated by the VisualGraph object.   * This is simply a wrapper around graph.add( Vertex ).   *   * @param	vertex		The Vertex object to be added to the graph   * @see							#add( Vertex, Font )   */  public void add( Vertex vertex ) throws Exception {    graph.add( vertex );    // No need to add to visualvertices ourselves, since the

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -