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

📄 jgraphpane.java

📁 JPowerGraph is a Java library for creating directed graphs for Swing. It supports graph movement, se
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                Edge edge=(Edge)iterator.previous();
                EdgePainter edgePainter=getPainterForEdge(edge);
                double distance=edgePainter.screenDistanceFromEdge(this,edge,point);
                if (distance<minDistance) {
                    minDistance=distance;
                    nearestEdge=edge;
                }
            }
            return nearestEdge;
        }
    }
    /**
     * Returns the position of the node on the screen.
     *
     * @param node                      the node whose on-screen position is required
     * @return                          the position of the node on screen
     */
    public Point getScreenPointForNode(Node node) {
        Point point=(Point)m_nodePositions.get(node);
        if (point==null) {
            point=new Point();
            graphToScreenPoint(new Point2D.Double(node.getX(),node.getY()),point);
            m_nodePositions.put(node,point);
        }
        return point;
    }
    /**
     * Updates the map of screen positions of nodes.
     */
    protected void updateNodeScreenPositions() {
        synchronized (m_graph) {
            Point2D graphPoint=new Point2D.Double();
            Iterator nodes=m_graph.getNodes().iterator();
            while (nodes.hasNext()) {
                Node node=(Node)nodes.next();
                Point point=(Point)m_nodePositions.get(node);
                if (point==null) {
                    point=new Point();
                    m_nodePositions.put(node,point);
                }
                graphPoint.setLocation(node.getX(),node.getY());
                graphToScreenPoint(graphPoint,point);
            }
        }
    }
    /**
     * Returns the screen bounds of given node.
     *
     * @param node                      the node for which the bounds must be returned
     * @param nodeScreenRectangle       the rectangle receiving the node's coordinates
     */
    public void getNodeScreenBounds(Node node,Rectangle nodeScreenRectangle) {
        NodePainter nodePainter=getPainterForNode(node);
        nodePainter.getNodeScreenBounds(this,node, nodeSize,nodeScreenRectangle);
    }
    /**
     * Repaints the given node.
     *
     * @param node                      the node that needs to be repainted
     */
    public void repaintNode(Node node) {
        Rectangle nodeScreenRectangle=new Rectangle();
        getNodeScreenBounds(node,nodeScreenRectangle);
        repaint(nodeScreenRectangle);
    }
    /**
     * Returns the screen bounds of given edge.
     *
     * @param edge                      the edge for which the bounds must be returned
     * @param edgeScreenRectangle       the rectangle receiving the edge's coordinates
     */
    public void getEdgeScreenBounds(Edge edge,Rectangle edgeScreenRectangle) {
        EdgePainter edgePainter=getPainterForEdge(edge);
        edgePainter.getEdgeScreenBounds(this,edge,edgeScreenRectangle);
    }
    /**
     * Repaints the given edge.
     *
     * @param edge                      the edge that needs to be repainted
     */
    public void repaintEdge(Edge edge) {
        Rectangle edgeScreenRectangle=new Rectangle();
        getEdgeScreenBounds(edge,edgeScreenRectangle);
        edgeScreenRectangle.grow(5,5);
        repaint(edgeScreenRectangle);
    }
    
    public void refreshLegend(Graph graph) {
        m_legend.clear();
        for (Iterator i = graph.getNodes().iterator(); i.hasNext();) {
            Node node = (Node) i.next();
            NodePainter nodePainter = getPainterForNode(node);
            m_legend.add(nodePainter, node.getClass(), node.getNodeType());
        }
    }
    
    /**
     * Returns the node painter for nodes of a type that does not 
     * have a specific node painter. 
     * 
     * @see setDefaultNodePainter(NodePainter theNodePainter)
     * @see setNodePainter(Class theClass, NodePainter theNodePainter)
     * @see getNodePainter(Class theClass)
     * 
     * @return NodePainter
     */
    public NodePainter getDefaultNodePainter() {
        return defaultNodePainter;
    }
    
    /**
     * Sets the node painter for nodes of a type that does not 
     * have a specific node painter. 
     * 
     * @see getDefaultNodePainter();
     * @see setNodePainter(Class theClass, NodePainter theNodePainter)
     * @see getNodePainter(Class theClass)
     * 
     * @param theNodePainter The new default NodePainter
     */
    public void setDefaultNodePainter(NodePainter theNodePainter) {
        this.defaultNodePainter = theNodePainter;
    }
    
    /**
     * Returns the node painter for nodes of the specified class. This
     * method will return null if no node painter is specified for this 
     * class.
     * 
     * @see setNodePainter(Class theClass, NodePainter theNodePainter)
     * 
     * @param theClass The Node class type
     * 
     * @return NodePainter
     */
    public NodePainter getNodePainter(Class theClass) {
        return (NodePainter) nodePainters.get(theClass);
    }
    
    /**
     * Sets the node painter for nodes of the specified class. To clear the 
     * specified node painter specify null for the nodePainter
     * 
     * @see getNodePainter(Class theClass)
     * 
     * @param theClass The Node class type
     * @param theNodePainter The new NodePainter for the specified class type
     * 
     * @return NodePainter
     */
    public void setNodePainter(Class theClass, NodePainter theNodePainter) {
        nodePainters.put(theClass, theNodePainter);
        refreshLegend(m_graph);
    }
    
    /**
     * Returns the edge painter for edges of a type that does not 
     * have a specific edge painter. 
     * 
     * @see setDefaultEdgePainter(EdgePainter theEdgePainter)
     * @see setEdgePainter(Class theClass, EdgePainter theEdgePainter)
     * @see getEdgePainter(Class theClass)
     * 
     * @return EdgePainter
     */
    public EdgePainter getDefaultEdgePainter() {
        return defaultEdgePainter;
    }
    
    /**
     * Sets the edge painter for nodes of a type that does not 
     * have a specific edge painter. 
     * 
     * @see getDefaultEdgePainter();
     * @see setEdgePainter(Class theClass, EdgePainter theEdgePainter)
     * @see getEdgePainter(Class theClass)
     * 
     * @param theEdgePainter The new default EdgePainter
     */
    public void setDefaultEdgePainter(EdgePainter theEdgePainter) {
        this.defaultEdgePainter = theEdgePainter;
        refreshLegend(m_graph);
    }
    
    /**
     * Returns the edge painter for nodes of the specified class. This
     * method will return null if no edge painter is specified for this 
     * class.
     * 
     * @see setEdgePainter(Class theClass, EdgePainter theEdgePainter)
     * 
     * @param theClass The Edge class type
     * 
     * @return EdgePainter
     */
    public EdgePainter getEdgePainter(Class theClass) {
        return (EdgePainter) edgePainters.get(theClass);
    }
    
    /**
     * Sets the edge painter for nodes of the specified class. To clear the 
     * specified edge painter specify null for the edgePainter
     * 
     * @see getEdgePainter(Class theClass)
     * 
     * @param theClass The Edge class type
     * @param theEdgePainter The new EdgePainter for the specified class type
     * 
     * @return EdgePainter
     */
    public void setEdgePainter(Class theClass, EdgePainter theEdgePainter) {
        edgePainters.put(theClass, theEdgePainter);
    }
    
    public void setNodeSize(int theNodeSize) {
        this.nodeSize = theNodeSize;
    }

    public void clearNodeFilter() {
        hiddenNodeTypes.clear();    
    }
    
    public void addFilteredNode(Class class1) {
        hiddenNodeTypes.add(class1);
    }
    
    /**
     * Processes the component event.
     *
     * @param e                         the event
     */
    protected void processComponentEvent(ComponentEvent e) {
        super.processComponentEvent(e);
        updateNodeScreenPositions();
        repaint();
    }
    /**
     * Processes the mouse event.
     *
     * @param e                         the event
     */
    protected void processMouseEvent(MouseEvent e) {
        super.processMouseEvent(e);
        switch (e.getID()) {
            case MouseEvent.MOUSE_PRESSED:
            if (!hasFocus() && isRequestFocusEnabled())
                requestFocus();
            for (int i=0;i<m_manipulators.size() && !e.isConsumed();i++)
                ((Manipulator)m_manipulators.get(i)).mousePressed(e);
            break;
        case MouseEvent.MOUSE_RELEASED:
            for (int i=0;i<m_manipulators.size() && !e.isConsumed();i++)
                ((Manipulator)m_manipulators.get(i)).mouseReleased(e);
            break;
        case MouseEvent.MOUSE_CLICKED:
            for (int i=0;i<m_manipulators.size() && !e.isConsumed();i++)
                ((Manipulator)m_manipulators.get(i)).mouseClicked(e);
            break;
        case MouseEvent.MOUSE_ENTERED:
            for (int i=0;i<m_manipulators.size() && !e.isConsumed();i++)
                ((Manipulator)m_manipulators.get(i)).mouseEntered(e);
            break;
        case MouseEvent.MOUSE_EXITED:
            for (int i=0;i<m_manipulators.size() && !e.isConsumed();i++)
                ((Manipulator)m_manipulators.get(i)).mouseExited(e);
            break;
        }
    }
    /**
     * Processes the mouse motion events.
     *
     * @param e                         mouse event
     */
    protected void processMouseMotionEvent(MouseEvent e) {
        super.processMouseMotionEvent(e);
        switch (e.getID()) {
        case MouseEvent.MOUSE_MOVED:
            for (int i=0;i<m_manipulators.size() && !e.isConsumed();i++)
                ((Manipulator)m_manipulators.get(i)).mouseMoved(e);
            break;
        case MouseEvent.MOUSE_DRAGGED:
            for (int i=0;i<m_manipulators.size() && !e.isConsumed();i++)
                ((Manipulator)m_manipulators.get(i)).mouseDragged(e);
            break;
        }
    }
    /**
     * Processes the key event.
     *
     * @param e                         the event
     */
    protected void processKeyEvent(KeyEvent e) {
        super.processKeyEvent(e);
        switch (e.getID()) {
        case KeyEvent.KEY_TYPED:
            for (int i=0;i<m_manipulators.size() && !e.isConsumed();i++)
                ((Manipulator)m_manipulators.get(i)).keyTyped(e);
            break;
        case KeyEvent.KEY_PRESSED:
            for (int i=0;i<m_manipulators.size() && !e.isConsumed();i++)
                ((Manipulator)m_manipulators.get(i)).keyPressed(e);
            break;
        case KeyEvent.KEY_RELEASED:
            for (int i=0;i<m_manipulators.size() && !e.isConsumed();i++)
                ((Manipulator)m_manipulators.get(i)).keyReleased(e);
            break;
        }
    }
    /**
     * Processes the focus event.
     *
     * @param e                         the event
     */
    protected void processFocusEvent(FocusEvent e) {
        super.processFocusEvent(e);
        switch (e.getID()) {
        case FocusEvent.FOCUS_GAINED:
            for (int i=0;i<m_manipulators.size();i++)
                ((Manipulator)m_manipulators.get(i)).focusGained(e);
            break;
        case FocusEvent.FOCUS_LOST:
            for (int i=0;i<m_manipulators.size();i++)
                ((Manipulator)m_manipulators.get(i)).focusLost(e);
            break;
        }
    }
    /**
     * Overridden to notify the manipulators that the scroll position has changed.
     *
     * @param rectangle                 the rectangle
     */
    public void scrollRectToVisible(Rectangle rectangle) {
        super.scrollRectToVisible(rectangle);
        for (int i=0;i<m_manipulators.size();i++){
            ((Manipulator)m_manipulators.get(i)).notifyGraphPaneScrolled();
        }
    }

    /**
     * The handler of graph events.
     */
    protected class GraphHandler implements GraphListener {
        public void graphLayoutUpdated(Graph graph) {
            if (SwingUtilities.isEventDispatchThread()) {
                updateNodeScreenPositions();
                repaint();
            }
            else
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        updateNodeScreenPositions();
                        repaint();
                    }
                });
        }
        public void graphUpdated(Graph graph) {
            if (SwingUtilities.isEventDispatchThread())
                repaint();
            else
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        repaint();
                    }
                });
        }
        public void graphContentsChanged(Graph graph) {
            m_nodePositions.clear();
            repaint();
        }
        public void elementsAdded(Graph graph,Collection nodes,Collection edges) {
            refreshLegend(graph);
            repaint();
        }
        public void elementsRemoved(Graph graph,Collection nodes,Collection edges) {
            if (nodes!=null) {
                Iterator iterator=nodes.iterator();
                while (iterator.hasNext()) {
                    Node node=(Node)iterator.next();
                    m_nodePositions.remove(node);
                }
            }
            refreshLegend(graph);
            repaint();
        }
    }

    /**
     * The handler of lens events.
     */
    protected class LensHandler implements LensListener {
        public void lensUpdated(Lens lens) {
            updateNodeScreenPositions();
            repaint();
        }
    }
}

⌨️ 快捷键说明

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