?? element.java
字號:
package reg402;
import java.awt.*;
import java.awt.geom.*;
public abstract class Element {
protected Color color; //圖形顏色
protected BasicStroke bs; //畫筆的大小
public Element(BasicStroke bs, Color color) {
this.color = color;
this.bs = bs;
}
public void setStroke(BasicStroke bs) {
this.bs = bs;
}
public void setColor(Color color) {
this.color = color;
}
public abstract void draw(Graphics2D g2D); //繪畫圖形
public abstract Rectangle getBounds(); //圖形的大小
//類功能:繪制箭頭形的線
public static class ArrowLine extends Element implements Constants {
private Point start, ctrl1, ctrl2, end;
private String label = "ε";
public ArrowLine(Point start, Point end,
BasicStroke bs, Color color, String label) {
super(bs, color);
this.start = this.ctrl2 = start;
this.end = this.ctrl1 = end;
this.label = label;
}
public ArrowLine(Point p1, Point p2, Point p3, Point p4,
BasicStroke bs, Color color, String label) {
super(bs, color);
this.start = p1;
this.ctrl1 = p2;
ctrl1.x=p2.x+5;
ctrl1.y=p2.y+5;
this.ctrl2 = p3;
this.end = p4;
this.label = label;
}
public void draw(Graphics2D g2d) {
g2d.setStroke(bs);
g2d.setColor(color);
//有閉包運算的時候繪制三條線
Line2D line1 = new Line2D.Double(start.x, start.y,
ctrl1.x, ctrl1.y);
Line2D line2 = new Line2D.Double(ctrl1.x, ctrl1.y,
ctrl2.x, ctrl2.y);
Line2D line3 = new Line2D.Double(ctrl2.x, ctrl2.y,
end.x, end.y);
Rectangle arrow = arrowPoint(ctrl2, end);
g2d.draw(line1);
g2d.draw(line2);
g2d.draw(line3);
//給直線繪制箭頭
g2d.draw(new Line2D.Double(new Point(arrow.x, arrow.y), end));
g2d.draw(new Line2D.Double(new Point(arrow.width, arrow.height), end));
g2d.setPaint(Color.darkGray);
g2d.setFont(new Font("Helvetica", Font.BOLD + Font.ITALIC, 20));
//寫出轉換字符
g2d.drawString(label, (ctrl1.x + ctrl2.x)/2 - 5, (ctrl1.y + ctrl2.y)/2 -5);
}
//函數功能:計算箭頭的兩頭坐標
private Rectangle arrowPoint(Point p1, Point p2) {
int a = p1.x, b = p1.y;
int c = p2.x, d = p2.y;
double x0, y0, k, m, n, q;
double L = Math.sqrt((a-c)*(a-c) + (b-d)*(b-d));
double x1, y1, x2, y2;
if(c != a && b != d) {
x0 = c - Math.sqrt(2)/2*RADIUS*(c-a)/L;
y0 = d - Math.sqrt(2)/2*RADIUS*(d-b)/L;
k = (a-c)/(d-b);
m = 1 + k*k;
n = -2*c + 2*k*(y0 - k*x0) - 2*d*k;
q = (y0 - k*x0)*(y0 - k*x0) - 2*d*(y0 - k*x0) + d*d
- RADIUS*RADIUS + c*c;
x1 = (-n + Math.sqrt(n*n - 4*m*q))/(2*m);
y1 = k*x1 + (y0 - k*x0);
x2 = (-n - Math.sqrt(n*n - 4*m*q))/(2*m);
y2 = k*x2 + (y0 - k*x0);
}
else if(c == a && b != d) {
k = 0;
x0 = c;
y0 = d - Math.sqrt(2)/2*RADIUS*(d-b)/L;
x1 = c - Math.sqrt(2)/2*RADIUS;
y1 = y0;
x2 = c + Math.sqrt(2)/2*RADIUS;
y2 = y0;
}
else if(c != a && b == d) {
k = 1;
x0 = c - Math.sqrt(2)/2*RADIUS*(c-a)/L;
y0 = b;
x1 = x0;
y1 = d - Math.sqrt(2)/2*RADIUS;
x2 = x0;
y2 = d + Math.sqrt(2)/2*RADIUS;
}
else return null;
return new Rectangle((int)x1,(int)y1, (int)x2, (int)y2);
}
public Rectangle getBounds() {
return new Rectangle();
}
}
//類功能:繪制圓形,即自動機的狀態
public static class Circle extends Element {
private Ellipse2D.Double circle;
public Circle(Point center, double radius, BasicStroke bs, Color color) {
super(bs, color);
circle = new Ellipse2D.Double(center.x - radius,
center.y - radius,
2.0*radius, 2.0*radius);
}
public Shape getShape() {
return circle;
}
public Rectangle getBounds() {
return circle.getBounds();
}
public void modify(Point center, Point circum) {
double radius = center.distance(circum);
circle.x = center.x - (int)radius;
circle.y = center.y - (int)radius;
circle.width = circle.height = 2*radius;
}
public void draw(Graphics2D g2D) {
g2D.setPaint(color);
g2D.draw(circle);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -