-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAlgoFrame.java
92 lines (66 loc) · 2.16 KB
/
AlgoFrame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
public class AlgoFrame extends JFrame {
private int canvasWidth;
private int canvasHeight;
public AlgoFrame(String title, int canvasWidth, int canvasHeight) {
super(title);
this.canvasWidth = canvasWidth;
this.canvasHeight = canvasHeight;
AlgoCanvas canvas = new AlgoCanvas();
setContentPane(canvas);
pack();
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public AlgoFrame(String title) {
this(title, 1024, 728);
}
private Circle[] circles;
public void render(Circle[] circles) {
this.circles = circles;
repaint();
}
private class AlgoCanvas extends JPanel {
public AlgoCanvas() {
super(true);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// g.drawOval(50, 50, 300, 300);
Graphics2D g2d = (Graphics2D) g;
// 抗锯齿
RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.addRenderingHints(hints);
// 具体绘制
AlgoVisHelper.setStrokeWidth(g2d, 1);
AlgoVisHelper.setColor(g2d, Color.RED);
for (Circle circle : circles) {
if (!circle.isFilled)
AlgoVisHelper.strokeCircle(g2d, circle.x, circle.y, circle.getR());
else
AlgoVisHelper.fillCircle(g2d, circle.x, circle.y, circle.getR());
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(canvasWidth, canvasHeight);
}
}
public int getCanvasWidth() {
return canvasWidth;
}
public void setCanvasWidth(int canvasWidth) {
this.canvasWidth = canvasWidth;
}
public int getCanvasHeight() {
return canvasHeight;
}
public void setCanvasHeight(int canvasHeight) {
this.canvasHeight = canvasHeight;
}
}