📄 graphlayout.java
字号:
/* * WebSphinx web-crawling toolkit * * Copyright (c) 1998-2002 Carnegie Mellon University. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */package websphinx.workbench;// Daniel Tunkelang's graph-drawing packagesimport graph.*; import gd.*;import rcm.awt.*;import java.util.*;import java.awt.*;import java.awt.image.ImageObserver;public class GraphLayout extends Canvas implements Runnable, ImageObserver { Graph graph; // graph to display double restLength = 50; // default rest length of an edge double springConstant = 100; // attraction between connected nodes double nodeCharge = 10000; // repulsion between any pair of nodes GDAlgorithm algorithm; // algorithm used for automatic graph layout boolean running = false; // is repaint thread running? boolean automaticLayout = true; // is automatic graph layout enabled? double threshold = 100; // if an iteration shows less "improvement" than // this threshold, stop iterating boolean quiescent = true; // is the graph stable? boolean dirty = true; // do we need to repaint? int interval = 100; // milliseconds between repaints int iterations = 3; // number of layout iterations per repaint Color nodeColor = Color.pink; // default background color for a node // (node label text is in Foreground color) Color edgeColor = Color.black; // default color of an edge line Color tipColor = Color.yellow; // background color of a popup tip //RenderedNode selectedNode = null; // currently selected node, or null //RenderedEdge selectedEdge = null; // currently selected edge, or null // invariant: selectedNode==null || selectedEdge == null Object tipObject = null; // node or edge currently under mouse, or null MultiLineString tip = null; // tip string displayed for tipObject, or null int tipX, tipY, tipWidth, tipHeight; // bounding box of tip GraphLayoutControlPanel controlPanel; /** * Make a GraphLayout. */ public GraphLayout () { graph = new Graph (); resetAlgorithm (); start (); } /** * Erase the graph. */ public synchronized void clear () { graph = new Graph (); changedGraph (); } /** * Get the graph. */ public synchronized Graph getGraph () { return graph; } /** * Set the graph. */ public synchronized void setGraph (Graph graph) { this.graph = graph; //selectedNode = null; //selectedEdge = null; tipObject = null; tip = null; changedGraph (); } /** * Get the graph-drawing algorithm in use. */ public synchronized GDAlgorithm getAlgorithm () { return algorithm; } /** * Set the graph-drawing algorithm. */ public synchronized void setAlgorithm (GDAlgorithm algorithm) { this.algorithm = algorithm; changedGraph (); } synchronized void resetAlgorithm () { algorithm = new AllPairsAlgorithm (springConstant, nodeCharge); changedGraph (); } /** * Get the default rest length for new edges. */ public synchronized double getRestLength () { return restLength; } /** * Set the default rest length for new edges. */ public synchronized void setRestLength (double restLength) { this.restLength = restLength; changedGraph (); } /** * Get the spring constant. */ public synchronized double getSpringConstant () { return springConstant; } /** * Set the spring constant. */ public synchronized void setSpringConstant (double springConstant) { this.springConstant = springConstant; resetAlgorithm (); } /** * Get the node charge. */ public synchronized double getNodeCharge () { return nodeCharge; } /** * Set the node charge. */ public synchronized void setNodeCharge (double nodeCharge) { this.nodeCharge = nodeCharge; resetAlgorithm (); } /** * Get the refresh interval (measured in seconds). */ public synchronized int getInterval () { return interval; } /** * Set the refresh interval (in seconds). */ public synchronized void setInterval (int interval) { this.interval = interval; } /** * Get the layout algorithm iterations per refresh. */ public synchronized int getIterations () { return iterations; } /** * Set the layout algorithm iterations per refresh. */ public synchronized void setIterations (int iterations) { this.iterations = iterations; } /** * Test whether the graph is laid out automatically. */ public synchronized boolean getAutomaticLayout () { return automaticLayout; } /** * Set whether the graph is laid out automatically. */ public synchronized void setAutomaticLayout (boolean f) { automaticLayout = f; quiescent = !automaticLayout; if (controlPanel != null) controlPanel.automatic.setState (automaticLayout); } /** * Test whether the graph is quiescent (not changing in the background). */ public synchronized boolean getQuiescent () { return quiescent; } /** * Test whether the graph layout thread is running in the background */ public synchronized boolean getRunning () { return running; } /** * Get the threshold. */ public synchronized double getThreshold () { return threshold; } /** * Set the threshold. */ public synchronized void setThreshold (double threshold) { this.threshold = threshold; changedGraph (); } /** * Get the node background color. */ public synchronized Color getNodeColor () { return nodeColor; } /** * Set the node background color. */ public synchronized void setNodeColor (Color nodeColor) { this.nodeColor = nodeColor; } /** * Get the edge color. */ public synchronized Color getEdgeColor () { return edgeColor; } /** * Set the edge color. */ public synchronized void setEdgeColor (Color edgeColor) { this.edgeColor = edgeColor; } /** * Get the popup tip color. */ public synchronized Color getTipColor () { return tipColor; } /** * Set the popup tip color. */ public synchronized void setTipColor (Color tipColor) { this.tipColor = tipColor; } /** * Get node currently under the mouse pointer, or null if no node is under the mouse. */ public synchronized RenderedNode getSelectedNode () { return tipObject instanceof RenderedNode ? (RenderedNode)tipObject : null; } /** * Get edge currently under the mouse pointer, or null if no edge is under the mouse. */ public synchronized RenderedEdge getSelectedEdge () { return tipObject instanceof RenderedEdge ? (RenderedEdge)tipObject : null; } /** * Add a node. */ public synchronized void addNode (RenderedNode node) { graph.addNode (node); graph.placeNode (node, node.x, node.y); changedGraph (); } /** * Add an edge. */ public synchronized void addEdge (RenderedEdge edge) { if (edge.restLength == 0) edge.restLength = restLength; graph.addEdge (edge); changedGraph (); } /** * Remove a node.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -