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

📄 shapenodepainter.java

📁 JPowerGraph is a Java library for creating directed graphs for SWT. It supports graph movement, sele
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }
    /**
     * Returns the background color of the node.
     *
     * @param isHighlighted         <code>true</code> if the node is highlighted
     * @param isSelected            <code>true</code> if the node is selected
     * @param isDragging            <code>true</code> if the node is being dragged
     * @return                      the background color
     */
    public Color getBackgroundColor(boolean isHighlighted,boolean isSelected,boolean isDragging) {
        if (isHighlighted || isDragging || isSelected){
            return borderColor;
        }
        return backgroundColor;
    }
    /**
     * Returns the text color of the node
     *
     * @param isHighlighted         <code>true</code> if the node is highlighted
     * @param isSelected            <code>true</code> if the node is selected
     * @param isDragging            <code>true</code> if the node is being dragged
     * @return                      the background color
     */
    public Color getTextColor(boolean isHighlighted,boolean isSelected,boolean isDragging) {
        if (isHighlighted || isDragging || isSelected){
            return textColor;
        }
        return textColor;
    }
    /**
     * Checks whether given point is inside the node.
     *
     * @param graphPane             the graph pane
     * @param node                  the node
     * @param point                 the point
     * @return                      <code>true</code> if the point is in the node
     */
    public boolean isInNode(JGraphPane graphPane,Node node,Point point, int size) {
        Rectangle nodeScreenRectangle=new Rectangle(0, 0, 0, 0);
        getNodeScreenBounds(graphPane,node, size, nodeScreenRectangle);
        return nodeScreenRectangle.contains(point);
    }
    
    /**
     * Returns the outer rectangle of the node on screen.
     *
     * @param graphPane             the graph pane
     * @param node                  the node
     * @param nodeScreenRectangle   the rectangle receiving the node's coordinates
     */
    public void getNodeScreenBounds(JGraphPane graphPane,Node node, int size, Rectangle nodeScreenRectangle) {
        Point nodePoint=graphPane.getScreenPointForNode(node);
        String label=node.getLabel();
        
        if (g == null){
            Image im = new Image(parent.getDisplay(), 100, 100); 
            g = new GC(im);
        }
        
        if (fontMetrics == null){
            fontMetrics = g.getFontMetrics();
        }
        
        if (size == NodePainter.LARGE){ 
            int width=1;
            int height=1;
            if (label!=null) {
                width+=stringWidth(g, label)+6;
                height+=fontMetrics.getAscent() + fontMetrics.getDescent()+4;
            }
            else {
                width+=40;
                height+=20;
            }
            
            nodeScreenRectangle.x = nodePoint.x-width/2;
            nodeScreenRectangle.y = nodePoint.y-height/2;
            nodeScreenRectangle.width = width;
            nodeScreenRectangle.height = height;
        }
        else if (size == NodePainter.SMALL){
            int width = 20 + stringWidth(g, label);
            int height = 8;
            nodeScreenRectangle.x = nodePoint.x-width/2;
            nodeScreenRectangle.y = nodePoint.y-height/2;
            nodeScreenRectangle.width = width;
            nodeScreenRectangle.height = height;
        }
    }
    
    private int stringWidth(GC g, String s){
        return g.stringExtent(s).x;
    }
    
    /**
     * Retruns the tool-tip for given point.
     *
     * @param graphPane             the graph pane
     * @param node                  the node
     * @param point                 the point
     * @return                      the tool-tip at given point (or <code>null</code>)
     */
    public String getToolTipText(JGraphPane graphPane,Node node,Point point) {
        return null;
    }
    
    public void paintLegendItem(JGraphPane graphPane, GC g, Point thePoint, String legendText){
        if (fontMetrics == null){
            fontMetrics = g.getFontMetrics();
        }
        
        int padding = 2;
        int imageWidth = 18;
        int imageHeight = 8;
        int imageX = thePoint.x;
        int imageY = thePoint.y; 
        int textX = imageX + imageWidth + (padding * 3);
        int textY = imageY - 3;
        
        Color bgColor = getBackgroundColor(false, false, false);
        Color boColor = getBorderColor(false, false, false);
        Color teColor = getTextColor(false, false, false);
        
        Color oldFGColor = g.getForeground();
        Color oldBGColor = g.getBackground();
        if (shape == RECTANGLE){
            g.setBackground(bgColor);
            g.fillRectangle(imageX, imageY, imageWidth, imageHeight);
            g.setForeground(boColor);
            g.drawRectangle(imageX, imageY, imageWidth, imageHeight);
        }
        else if (shape == ELLIPSE){
            g.setBackground(bgColor);
            g.fillOval(imageX, imageY, imageWidth, imageHeight);
            g.setForeground(boColor);
            g.drawOval(imageX, imageY, imageWidth, imageHeight);
        }
        else if (shape == TRIANGLE){
            int x1 = imageX;
            int y1 = imageY + imageHeight;
            int x2 = x1 + imageWidth/2;
            int y2 = imageY - imageHeight/2;
            int x3 = x1 + imageWidth;
            int y3 = y1;
            
            g.setBackground(bgColor);
            g.fillPolygon(new int[]{x1, y1, x2, y2, x3, y3});
            g.setForeground(boColor);
            g.drawPolygon(new int[]{x1, y1, x2, y2, x3, y3});
        }
        g.setBackground(oldBGColor);
        g.setForeground(teColor);
        g.drawString(legendText, textX, textY);
        g.setForeground(oldFGColor);
        g.setBackground(oldBGColor);
    }
    
    public Point getLegendItemSize(JGraphPane graphPane, String legendText){
        if (g == null){
            Image im = new Image(parent.getDisplay(), 100, 100); 
            g = new GC(im);
        }
        
        if (fontMetrics == null){
            fontMetrics = g.getFontMetrics();
        }
        
        int padding = 2;
        int imageWidth = 18;
        int imageHeight = 8;
        int stringWidth = stringWidth(g, legendText);
        int width = imageWidth + stringWidth + (padding*3);
        int height= Math.max(imageHeight, fontMetrics.getAscent() + fontMetrics.getDescent() + 4);
        return new Point(width, height);
    }
    
    public Color getBorderColor() {
        return borderColor;
    }
    
    public Color getBackgroundColor() {
        return backgroundColor;
    }
    
    public void dispose(){
        if (!backgroundColor.isDisposed()){
            backgroundColor.dispose();
        }
        if (!borderColor.isDisposed()){
            borderColor.dispose();
        }
        if (!textColor.isDisposed()){
            textColor.dispose();
        }
        disposed = true;
    }
    
    public boolean isDisposed(){
        return disposed;
    }
}

⌨️ 快捷键说明

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