?? arrow.java
字號:
package com.sunking.tp.framework;
import java.awt.*;
/**
*
* <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 LineRenderer
*/
public class Arrow implements LineRenderer {
private static final long serialVersionUID = -3000000000000000001L;
/**
*所屬連線
*/
protected AssoicatorLine line;
/**
*箭頭的繪畫范圍
*/
transient protected Polygon polygon;
/**
* @param line 所屬連線
*/
public Arrow(AssoicatorLine line) {
this.line = line;
}
/**
*取得箭頭的繪畫范圍
*/
public Rectangle getBounds() {
return getPolygon().getBounds();
}
/**
*繪制箭頭
*/
public void paint(java.awt.Graphics g) {
if(line.getAssociateType() == 1){
return;
}
Color oldC= g.getColor();
g.setColor(javax.swing.UIManager.getColor("Desktop.background"));
g.fillPolygon(getPolygon());
g.setColor(Color.black);
if(line.getAssociateType() == 3){
g.fillPolygon(getPolygon());
}else{
g.drawPolygon(getPolygon());
}
g.setColor(oldC);
}
/**
*取得默認的繪畫范圍
* @return
*/
protected Polygon getPolygon(){
if(polygon == null){
polygon = getDefaultPolygon();
}
return polygon;
}
/**
*根據(jù)所屬連線的位置定位三個點的偏移點.
*/
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());
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>
* @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
* @version 1.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>
* @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
* @version 1.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>
* @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
* @version 1.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;
}
}