📄 dsarrow.java
字号:
package drawsmart.itsv.basic;import java.awt.Graphics;import drawsmart.itsv.framework.LineRenderer;import java.awt.*;/** * <p>Title: 绘制器</p> * <p>Description: 绘制直线的箭头</p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author not attributable * @version 2.0 */public abstract class DSArrow implements LineRenderer { /** *所属连线 */ protected DSLine line; /** *箭头的绘画范围 */ transient protected Polygon polygon; /** * @param line 所属连线 */ public DSArrow(DSLine line) { this.line = line; } /** *取得箭头的绘画范围 */ public Rectangle getBounds() { return getPolygon().getBounds(); } /** *绘制箭头 */ public void paint(java.awt.Graphics g) { Color oldC= g.getColor(); g.setColor(javax.swing.UIManager.getColor("Desktop.background")); g.fillPolygon(getPolygon()); g.setColor(Color.black); // g.drawPolygon(getPolygon()); g.setColor(oldC); } /** *取得默认的绘画范围 * @return */ protected Polygon getPolygon(){ //每次刷新都需要重新绘制箭头 polygon = getDefaultPolygon(); return polygon; } /** *根据所属连线的位置定位三个点的偏移点. */ private Polygon getDefaultPolygon(){ Locator locs[] = new Locator[] { new PolarCoordinate( 10, 0.5 + Math.PI ), new PolarCoordinate( 0, 0.0 ), new PolarCoordinate( 10, -0.5 + Math.PI ) }; int xArray[] = new int[locs.length]; int yArray[] = new int[locs.length]; for ( int i = 0; i < locs.length; i++ ) { xArray[i] = getArrowLocator( locs[i] ).x(); yArray[i] = getArrowLocator( locs[i] ).y(); } return new Polygon( xArray, yArray, locs.length); } protected Locator getArrowLocator( Locator loc ) { Locator source = new DrawingPoint((int)line.getX1(),(int)line.getY1()); Locator dest = new DrawingPoint((int)line.getX2(),(int)line.getY2()); //System.out.println("X1="+line.getX1()+" Y1="+line.getY1()+" X2="+line.getX2()+" Y2="+line.getY2()); Locator relative = new DrawingPoint(dest.x()-source.x(), dest.y()-source.y()); PolarCoordinate coord = new PolarCoordinate( loc.r(), loc.theta() + relative.theta() ); return new DrawingPoint( dest.x() + coord.x(), dest.y() + coord.y() ); }}/** * * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p * @version 2.0 */interface Locator { public abstract int r(); public abstract double theta(); public abstract int x(); public abstract int y();}/** * * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @version 2.0 */class PolarCoordinate implements Locator { protected int r; protected double theta; public PolarCoordinate(int r, double theta) { this.r = r; this.theta = theta; } public int r() { return r; } public double theta() { return theta; } public int x() { return (int)( r * Math.cos( theta ) ); } public int y() { return (int)( r * Math.sin( theta ) ); }}/** * * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @version 2.0 */class DrawingPoint implements Locator{ protected int x; protected int y; public DrawingPoint(int x, int y) { this.x = x; this.y = y; } public int x() { return x; } public int y() { return y; } public int r() { int myX = x(); int myY = y(); return (int)Math.sqrt((myX * myX) + (myY * myY)); } public double theta() { double theta = Math.atan((double)y()/(double)x()); if (x() < 0) theta = theta + Math.PI; return theta; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -