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

📄 arrowedgepainter.java

📁 JPowerGraph is a Java library for creating directed graphs for SWT. It supports graph movement, sele
💻 JAVA
字号:
package net.sourceforge.jpowergraph.painters;

import net.sourceforge.jpowergraph.Edge;
import net.sourceforge.jpowergraph.manipulator.dragging.DraggingManipulator;
import net.sourceforge.jpowergraph.manipulator.selection.HighlightingManipulator;
import net.sourceforge.jpowergraph.pane.JGraphPane;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;

/**
 * The painter that paints the edge as the arrow.
 */
public class ArrowEdgePainter extends AbstractEdgePainter {
    /** The length of the arrow base. */
    protected static final double ARROW_BASE_LENGTH=3.0;
    
    private Color normal;
    private Color highlighted;
    private boolean disposed = false;

    public ArrowEdgePainter (Display theDisplay){
        normal = theDisplay.getSystemColor(SWT.COLOR_GRAY);
        highlighted = theDisplay.getSystemColor(SWT.COLOR_RED);
    }
    
    /**
     * Paints the supplied edge.
     *
     * @param graphPane             the graph pane
     * @param g                     the graphics
     * @param edge                  the edge to paint
     */
    public void paintEdge(JGraphPane graphPane,GC g,Edge edge) {
        HighlightingManipulator highlightingManipulator=(HighlightingManipulator)graphPane.getManipulator(HighlightingManipulator.NAME);
        boolean isHighlighted=highlightingManipulator!=null && highlightingManipulator.getHighlightedEdge()==edge;
        DraggingManipulator draggingManipulator=(DraggingManipulator)graphPane.getManipulator(DraggingManipulator.NAME);
        boolean isDragging=draggingManipulator!=null && draggingManipulator.getDraggedEdge()==edge;
        Point from=graphPane.getScreenPointForNode(edge.getFrom());
        Point to=graphPane.getScreenPointForNode(edge.getTo());
        Color color=g.getBackground();
        g.setBackground(getEdgeColor(edge,isHighlighted,isDragging));
        paintArrow(g,from.x,from.y,to.x,to.y);
        g.setBackground(color);
    }
    /**
     * Returns the color for the edge.
     *
     * @param edge                  the edge to be painted
     * @param isHighlighted         <code>true</code> if the edge is highlighted
     * @param isDragging            <code>true</code> if the edge is being dragged
     * @return                      the color for the edge
     */
    protected Color getEdgeColor(Edge edge,boolean isHighlighted,boolean isDragging) {
        if (isHighlighted || isDragging){
            return highlighted;
        }
        return normal;
    }
    /**
     * Paints the arrow.
     *
     * @param g                     the graphics
     * @param x1                    the source x coordinate
     * @param y1                    the source y coordinate
     * @param x2                    the target x coordinate
     * @param y2                    the target y coordinate
     */
    public static void paintArrow(GC g,int x1,int y1,int x2,int y2) {
        double dx;
        double dy;
        double deltaX=x1-x2;
        double deltaY=y1-y2;
        if (Math.abs(deltaY)>Math.abs(deltaX)) {
            double slope=Math.abs(deltaX/deltaY);
            dx=ARROW_BASE_LENGTH/Math.sqrt(1+slope*slope);
            dy=dx*slope;
        }
        else {
            double slope=Math.abs(deltaY/deltaX);
            dy=ARROW_BASE_LENGTH/Math.sqrt(1+slope*slope);
            dx=dy*slope;
        }
        if (deltaY>0)
            dx*=-1;
        if (deltaX<0)
            dy*=-1;
        
        int polyX1 = x2;
        int polyY1 = y2;
        int polyX2 = (int)(x1-dx);
        int polyY2 = (int)(y1-dy);
        int polyX3 = (int)(x1+dx);
        int polyY3 = (int)(y1+dy);
        
        g.fillPolygon(new int[]{polyX1, polyY1, polyX2, polyY2, polyX3, polyY3});
    }
    /**
     * Returns the distance of the point to the edge.
     *
     * @param graphPane             the graph pane
     * @param g                     the graphics object
     * @param edge                  the edge
     * @param point                 the point
     * @return                      the distance of the point from the edge
     */
    public double screenDistanceFromEdge(JGraphPane graphPane,GC g,Edge edge,Point point) {
        double px=point.x;
        double py=point.y;
        Point from=graphPane.getScreenPointForNode(edge.getFrom());
        Point to=graphPane.getScreenPointForNode(edge.getTo());
        double x1=from.x;
        double y1=from.y;
        double x2=to.x;
        double y2=to.y;
        if (px<Math.min(x1,x2)-8 || px>Math.max(x1,x2)+8 || py<Math.min(y1,y2)-8 || py>Math.max(y1,y2)+8)
            return 1000;
        double dist=1000;
        if (x1-x2!=0)
            dist=Math.abs((y2-y1)/(x2-x1)*(px-x1)+(y1-py));
        if (y1-y2!=0)
            dist=Math.min(dist,Math.abs((x2-x1)/(y2-y1)*(py-y1)+(x1-px)));
        return dist;
    }
    /**
     * Returns the outer rectangle of the edge on screen.
     *
     * @param graphPane             the graph pane
     * @param edge                  the edge
     * @param edgeScreenRectangle   the rectangle receiving the edge's coordinates
     */
    public void getEdgeScreenBounds(JGraphPane graphPane,Edge edge,Rectangle edgeScreenRectangle) {
        Point from=graphPane.getScreenPointForNode(edge.getFrom());
        Point to=graphPane.getScreenPointForNode(edge.getTo());
        edgeScreenRectangle.x = Math.min(from.x,to.x);
        edgeScreenRectangle.y = Math.min(from.y,to.y);
        edgeScreenRectangle.width = Math.abs(to.x-from.x)+1;
        edgeScreenRectangle.height = Math.abs(to.y-from.y)+1;
    }
    
    public void dispose(){
        if (!normal.isDisposed()){
            normal.dispose();
        }
        if (!highlighted.isDisposed()){
            highlighted.dispose();
        }
        disposed = true;
    }
    
    public boolean isDisposed(){
        return disposed;
    }
}

⌨️ 快捷键说明

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