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

📄 assoicatorline.java

📁 java 绘图方面的源代码
💻 JAVA
字号:
package com.sunking.tp.framework;import java.awt.*;import javax.swing.*;import javax.swing.event.*;import java.awt.geom.*;import java.util.*;/** * <p>Title: </p> * <p>Description: 连线</p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a> * @version 1.0 * @see Assoicator */public class AssoicatorLine extends Line2D.Double implements java.io.Serializable{    private static final long serialVersionUID = -3000000000000000003L;	/**	*连线的源和目录	*/    public JTPComponent source,dest;	/**	*连线类型	*/    private int associateType;	/**	*连线修饰器	*/    private Vector vecRenderer = new Vector();    /**     *     * @param source 连线源     * @param associator 连接器     */    public AssoicatorLine(JTPComponent source,Assoicator associator){        dest = associator.getAssoicator();        associateType = associator.getAssociateType();        this.source = source;        setLinePoints((Component)source,(Component)dest);        addRenderer(new Arrow(this));    }    /**     *取得连接源     * @return     */    public JTPComponent getSource(){        return source;    }    /**     *取得连接目标     * @return     */    public JTPComponent getDest(){        return dest;    }    /**     *取得连接类型     * @return     */    public int getAssociateType(){        return associateType;    }    /**     *判断是否包含一个点,主要用于点击时的选取动作     * @param p     * @return     */    public boolean contains(Point2D p) {        return contains((int)p.getX(),(int)p.getY());    }    public boolean contains(int x, int y)  {        return insideSegment(x,y,(int)getX1(),(int)getY1(),(int)getX2(),(int)getY2());    }    protected boolean insideSegment(int x, int y, int x0, int y0, int x1, int y1) {        if (x1 == x0)            return (x == x0) && (y >= Math.min(y0,y1)) && (y <= Math.max(y0,y1));        Rectangle bounds = new Rectangle(x0,y0,1,1);        int fudge = (int)Math.round(insideTolerance());        bounds.add(x1,y1);        bounds.grow(fudge,fudge);        if (!bounds.contains(x,y)) return false;        double slope = (double)(y1-y0) / (double)(x1-x0);        double fx = (slope * (double)(x-x0)) + (double)y0;        return Math.abs(fx - (double)y) < insideTolerance(slope);    }    protected double insideTolerance(double slope) {        return Math.max(insideTolerance(),Math.abs(slope));    }    protected double insideTolerance() {        return 2.5;    }    /**     *绘制线和修饰器     * @param g     */    public void drawLine(Graphics g){        Point2D pStart = getP1();        Point2D pEnd = getP2();        g.drawLine((int)pStart.getX(),(int)pStart.getY(),                   (int)pEnd.getX(),(int)pEnd.getY());        for (int i = 0; i < vecRenderer.size(); i++) {            LineRenderer renderer = (LineRenderer)vecRenderer.get(i);            renderer.paint(g);        }    }    /**     *绘制选取状态     * @param g     */    public void drawHandle(Graphics g){        Graphics2D g2=(Graphics2D)g;        g2.setStroke(new BasicStroke(2));        drawLine(g);        g2.setStroke(new BasicStroke(1));    }    /**     *添加修饰器     * @param renderer     */    public void addRenderer(LineRenderer renderer){        vecRenderer.add(renderer);    }    /**     *移取修饰器     * @param renderer     */    public void removeRenderer(LineRenderer renderer){        vecRenderer.remove(renderer);    }    /**     *根据连接源和连接目标定位线的位置     * @param source     * @param target     */    private void setLinePoints(Component source,Component target){        Component cSource =(Component)source;        Component cDest =(Component)dest;        int x1,y1,x2,y2;        x1=y1=x2=y2=0;        x1=cSource.getX()+(cSource.getWidth()/2);        y1=cSource.getY()+(cSource.getHeight()/2);        x2=cDest.getX()+(cDest.getWidth()/2);        y2=cDest.getY()+(cDest.getHeight()/2);        setLine(x1,y1,x2,y2);        setLine(getPointShortToComponent(cSource),getPointShortToComponent(cDest));    }    private Point getPointShortToComponent(Component c){        int x1=c.getX();        int y1=c.getY();        int x2=x1+c.getWidth();        int y2=y1+c.getHeight();        if(intersectsLine(x1,y1,x2,y1)){//top            return new Point(x1+c.getWidth()/2,y1);        }else if(intersectsLine(x1,y2,x2,y2)){//bottom            return new Point(x1+c.getWidth()/2,y2);        }else if(intersectsLine(x2,y1,x2,y2)){//right            return new Point(x2,y1+c.getHeight()/2);        }else{//left            return new Point(x1,y1+c.getHeight()/2);        }    }}

⌨️ 快捷键说明

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