Skip to content

Commit 9465ab4

Browse files
committed
Chapter 03 completed codes added.
1 parent 68f1f3e commit 9465ab4

File tree

13 files changed

+757
-0
lines changed

13 files changed

+757
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import java.awt.*;
2+
import java.util.LinkedList;
3+
import javax.swing.*;
4+
5+
public class AlgoFrame extends JFrame{
6+
7+
private int canvasWidth;
8+
private int canvasHeight;
9+
10+
public AlgoFrame(String title, int canvasWidth, int canvasHeight){
11+
12+
super(title);
13+
14+
this.canvasWidth = canvasWidth;
15+
this.canvasHeight = canvasHeight;
16+
17+
AlgoCanvas canvas = new AlgoCanvas();
18+
setContentPane(canvas);
19+
pack();
20+
21+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
22+
setResizable(false);
23+
24+
setVisible(true);
25+
}
26+
27+
public AlgoFrame(String title){
28+
29+
this(title, 1024, 768);
30+
}
31+
32+
public int getCanvasWidth(){return canvasWidth;}
33+
public int getCanvasHeight(){return canvasHeight;}
34+
35+
// data
36+
private MonteCarloPiData data;
37+
public void render(MonteCarloPiData data){
38+
this.data = data;
39+
repaint();
40+
}
41+
42+
private class AlgoCanvas extends JPanel{
43+
44+
public AlgoCanvas(){
45+
// 双缓存
46+
super(true);
47+
}
48+
49+
@Override
50+
public void paintComponent(Graphics g) {
51+
super.paintComponent(g);
52+
53+
Graphics2D g2d = (Graphics2D)g;
54+
55+
// 抗锯齿
56+
RenderingHints hints = new RenderingHints(
57+
RenderingHints.KEY_ANTIALIASING,
58+
RenderingHints.VALUE_ANTIALIAS_ON);
59+
hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
60+
g2d.addRenderingHints(hints);
61+
62+
// 具体绘制
63+
AlgoVisHelper.setStrokeWidth(g2d, 3);
64+
AlgoVisHelper.setColor(g2d, AlgoVisHelper.Blue);
65+
Circle circle = data.getCircle();
66+
AlgoVisHelper.strokeCircle(g2d, circle.getX(), circle.getY(), circle.getR());
67+
68+
for(int i = 0 ; i < data.getPointsNumber() ; i ++){
69+
Point p = data.getPoint(i);
70+
if(circle.contain(p))
71+
AlgoVisHelper.setColor(g2d, AlgoVisHelper.Red);
72+
else
73+
AlgoVisHelper.setColor(g2d, AlgoVisHelper.Green);
74+
75+
AlgoVisHelper.fillCircle(g2d, p.x, p.y, 3);
76+
}
77+
}
78+
79+
@Override
80+
public Dimension getPreferredSize(){
81+
return new Dimension(canvasWidth, canvasHeight);
82+
}
83+
}
84+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import javax.swing.*;
2+
import java.awt.*;
3+
import java.awt.geom.Ellipse2D;
4+
import java.awt.geom.GeneralPath;
5+
import java.awt.geom.Line2D;
6+
import java.awt.geom.Rectangle2D;
7+
import java.lang.InterruptedException;
8+
9+
10+
public class AlgoVisHelper {
11+
12+
private AlgoVisHelper(){}
13+
14+
public static final Color Red = new Color(0xF44336);
15+
public static final Color Pink = new Color(0xE91E63);
16+
public static final Color Purple = new Color(0x9C27B0);
17+
public static final Color DeepPurple = new Color(0x673AB7);
18+
public static final Color Indigo = new Color(0x3F51B5);
19+
public static final Color Blue = new Color(0x2196F3);
20+
public static final Color LightBlue = new Color(0x03A9F4);
21+
public static final Color Cyan = new Color(0x00BCD4);
22+
public static final Color Teal = new Color(0x009688);
23+
public static final Color Green = new Color(0x4CAF50);
24+
public static final Color LightGreen = new Color(0x8BC34A);
25+
public static final Color Lime = new Color(0xCDDC39);
26+
public static final Color Yellow = new Color(0xFFEB3B);
27+
public static final Color Amber = new Color(0xFFC107);
28+
public static final Color Orange = new Color(0xFF9800);
29+
public static final Color DeepOrange = new Color(0xFF5722);
30+
public static final Color Brown = new Color(0x795548);
31+
public static final Color Grey = new Color(0x9E9E9E);
32+
public static final Color BlueGrey = new Color(0x607D8B);
33+
public static final Color Black = new Color(0x000000);
34+
public static final Color White = new Color(0xFFFFFF);
35+
36+
37+
public static void strokeCircle(Graphics2D g, int x, int y, int r){
38+
39+
Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
40+
g.draw(circle);
41+
}
42+
43+
public static void fillCircle(Graphics2D g, int x, int y, int r){
44+
45+
Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
46+
g.fill(circle);
47+
}
48+
49+
public static void strokeRectangle(Graphics2D g, int x, int y, int w, int h){
50+
51+
Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
52+
g.draw(rectangle);
53+
}
54+
55+
public static void fillRectangle(Graphics2D g, int x, int y, int w, int h){
56+
57+
Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
58+
g.fill(rectangle);
59+
}
60+
61+
public static void setColor(Graphics2D g, Color color){
62+
g.setColor(color);
63+
}
64+
65+
public static void setStrokeWidth(Graphics2D g, int w){
66+
int strokeWidth = w;
67+
g.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
68+
}
69+
70+
public static void pause(int t) {
71+
try {
72+
Thread.sleep(t);
73+
}
74+
catch (InterruptedException e) {
75+
System.out.println("Error sleeping");
76+
}
77+
}
78+
79+
public static void putImage(Graphics2D g, int x, int y, String imageURL){
80+
81+
ImageIcon icon = new ImageIcon(imageURL);
82+
Image image = icon.getImage();
83+
84+
g.drawImage(image, x, y, null);
85+
}
86+
87+
public static void drawText(Graphics2D g, String text, int centerx, int centery){
88+
89+
if(text == null)
90+
throw new IllegalArgumentException("Text is null in drawText function!");
91+
92+
FontMetrics metrics = g.getFontMetrics();
93+
int w = metrics.stringWidth(text);
94+
int h = metrics.getDescent();
95+
g.drawString(text, centerx - w/2, centery + h);
96+
}
97+
98+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.awt.*;
2+
import java.util.LinkedList;
3+
import javax.swing.*;
4+
5+
public class AlgoVisualizer {
6+
7+
private static int DELAY = 40;
8+
9+
private MonteCarloPiData data;
10+
private AlgoFrame frame;
11+
private int N;
12+
13+
public AlgoVisualizer(int sceneWidth, int sceneHeight, int N){
14+
15+
if(sceneWidth != sceneHeight)
16+
throw new IllegalArgumentException("This demo must be run in a square window!");
17+
18+
this.N = N;
19+
Circle circle = new Circle(sceneWidth/2, sceneHeight/2, sceneWidth/2);
20+
data = new MonteCarloPiData(circle);
21+
22+
// 初始化视图
23+
EventQueue.invokeLater(() -> {
24+
frame = new AlgoFrame("Monte Carlo", sceneWidth, sceneHeight);
25+
26+
new Thread(() -> {
27+
run();
28+
}).start();
29+
});
30+
}
31+
32+
public void run(){
33+
34+
for(int i = 0 ; i < N ; i ++){
35+
36+
if( i % 100 == 0) {
37+
frame.render(data);
38+
AlgoVisHelper.pause(DELAY);
39+
System.out.println(data.estimatePi());
40+
}
41+
42+
int x = (int)(Math.random() * frame.getCanvasWidth());
43+
int y = (int)(Math.random() * frame.getCanvasHeight());
44+
data.addPoint(new Point(x, y));
45+
}
46+
47+
}
48+
49+
public static void main(String[] args) {
50+
51+
int sceneWidth = 800;
52+
int sceneHeight = 800;
53+
int N = 20000;
54+
55+
AlgoVisualizer vis = new AlgoVisualizer(sceneWidth, sceneHeight, N);
56+
}
57+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.awt.*;
2+
import javax.swing.*;
3+
4+
public class Circle {
5+
6+
private int x, y, r;
7+
8+
public Circle(int x, int y, int r){
9+
this.x = x;
10+
this.y = y;
11+
this.r = r;
12+
}
13+
14+
public int getX(){ return x; }
15+
public int getY(){ return y; }
16+
public int getR(){ return r; }
17+
18+
public boolean contain(Point p){
19+
return Math.pow(p.x - x, 2) + Math.pow(p.y - y, 2) <= r*r;
20+
}
21+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.LinkedList;
2+
import java.awt.*;
3+
4+
public class MonteCarloPiData {
5+
6+
private Circle circle;
7+
private LinkedList<Point> points;
8+
private int insideCircle = 0;
9+
10+
public MonteCarloPiData(Circle circle){
11+
this.circle = circle;
12+
points = new LinkedList<Point>();
13+
}
14+
15+
public Circle getCircle(){
16+
return circle;
17+
}
18+
19+
public int getPointsNumber(){
20+
return points.size();
21+
}
22+
23+
public Point getPoint(int i){
24+
if(i < 0 || i >= points.size())
25+
throw new IllegalArgumentException("out of bound in getPoint!");
26+
27+
return points.get(i);
28+
}
29+
30+
public void addPoint(Point p){
31+
points.add(p);
32+
if(circle.contain(p))
33+
insideCircle ++;
34+
}
35+
36+
public double estimatePi(){
37+
38+
if(points.size() == 0)
39+
return 0.0;
40+
41+
int circleArea = insideCircle;
42+
int squareArea = points.size();
43+
return (double)circleArea * 4 / squareArea;
44+
}
45+
}

0 commit comments

Comments
 (0)