📄 element.java.bak
字号:
package reg2nfa;
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;
//System.out.print(this.start);
//System.out.println(" " + this.end.toString());
}
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;
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);
GradientPaint gradient = new GradientPaint(ctrl1.x, ctrl1.y,
color,
ctrl2.x, ctrl2.y,
Color.red);
g2d.setPaint(gradient);
g2d.draw(line2);
g2d.draw(line3);
g2d.setPaint(Color.black);
//g2d.draw(new Ellipse2D.Double(end.x, end.y,
// bs.getLineWidth() + 5, bs.getLineWidth() + 5));
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 l = RADIUS;
int a = p1.x, b = p1.y, c = p2.x, d = p2.y;
//double L = Math.sqrt((a-c)*(a-c) + (b-d)*(b-d));
double x0 = (3*c+a)/4;
double y0 = (3*d+b)/4;
double k, K;
if(d-b == 0) K = 0;
else K = (c-a)/(d-b);
if(K == 0) k = -1;
else k = -1/K;
double p = Math.sqrt(k*k + 1);
int y1 = (int)(RADIUS/2*p + y0);
int x1 = (int)(k*(y1-y0) + x0);
int y2 = (int)(-RADIUS/2*p + y0);
int x2 = (int)(k*(y2-y0) + x0);
return new Rectangle(x1, y1, x2, 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 + -