Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 505781b

Browse files
Chapter 09 codes updated.
1 parent d3e1ad2 commit 505781b

File tree

44 files changed

+432
-664
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+432
-664
lines changed

‎09-Fractal-Drawing/02-Recursive-Circle-Drawing/src/AlgoFrame.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
1-
import java.awt.Graphics2D;
2-
import java.awt.Graphics;
3-
import java.awt.Dimension;
4-
import java.awt.Color;
5-
import java.awt.RenderingHints;
6-
import java.util.ArrayList;
7-
import java.util.Map;
8-
import java.util.HashMap;
9-
1+
import java.awt.*;
102
import javax.swing.*;
113

124
public class AlgoFrame extends JFrame{
135

146
private int canvasWidth;
157
private int canvasHeight;
16-
private JPanel canvas;
178

189
public AlgoFrame(String title, int canvasWidth, int canvasHeight){
1910

@@ -41,8 +32,8 @@ public AlgoFrame(String title){
4132
public int getCanvasHeight(){return canvasHeight;}
4233

4334
// data
44-
CircleData data;
45-
public void setData(CircleData data){
35+
privateCircleData data;
36+
public void render(CircleData data){
4637
this.data = data;
4738
repaint();
4839
}

‎09-Fractal-Drawing/02-Recursive-Circle-Drawing/src/AlgoVisHelper.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import javax.swing.*;
22
import java.awt.*;
33
import java.awt.geom.Ellipse2D;
4-
54
import java.awt.geom.Rectangle2D;
65
import java.lang.InterruptedException;
76

8-
97
public class AlgoVisHelper {
108

119
private AlgoVisHelper(){}
@@ -33,35 +31,35 @@ private AlgoVisHelper(){}
3331
public static final Color White = new Color(0xFFFFFF);
3432

3533

36-
staticpublic void strokeCircle(Graphics2D g, int x, int y, int r){
34+
publicstatic void strokeCircle(Graphics2D g, int x, int y, int r){
3735

3836
Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
3937
g.draw(circle);
4038
}
4139

42-
staticpublic void fillCircle(Graphics2D g, int x, int y, int r){
40+
publicstatic void fillCircle(Graphics2D g, int x, int y, int r){
4341

4442
Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
4543
g.fill(circle);
4644
}
4745

48-
staticpublic void strokeRectangle(Graphics2D g, int x, int y, int w, int h){
46+
publicstatic void strokeRectangle(Graphics2D g, int x, int y, int w, int h){
4947

5048
Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
5149
g.draw(rectangle);
5250
}
5351

54-
staticpublic void fillRectangle(Graphics2D g, int x, int y, int w, int h){
52+
publicstatic void fillRectangle(Graphics2D g, int x, int y, int w, int h){
5553

5654
Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
5755
g.fill(rectangle);
5856
}
5957

60-
staticpublic void setColor(Graphics2D g, Color color){
58+
publicstatic void setColor(Graphics2D g, Color color){
6159
g.setColor(color);
6260
}
6361

64-
staticpublic void setStrokeWidth(Graphics2D g, int w){
62+
publicstatic void setStrokeWidth(Graphics2D g, int w){
6563
int strokeWidth = w;
6664
g.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
6765
}
Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import javafx.scene.input.MouseButton;
2-
31
import java.awt.*;
4-
import java.awt.event.*;
5-
import javax.swing.*;
62

73
public class AlgoVisualizer {
84

@@ -11,38 +7,37 @@ public class AlgoVisualizer {
117
private CircleData data;
128
private AlgoFrame frame;
139

14-
public AlgoVisualizer(AlgoFrame frame, CircleData data){
10+
public AlgoVisualizer(int sceneWidth, int sceneHeight){
11+
12+
int R = Math.min(sceneWidth, sceneHeight)/2 - 2;
13+
data = new CircleData(sceneWidth/2, sceneHeight/2, R, R/2, 2);
1514

16-
this.frame = frame;
17-
this.data = data;
18-
this.setData(data);
15+
EventQueue.invokeLater(() -> {
16+
frame = new AlgoFrame("Fractal Visualizer", sceneWidth,sceneHeight);
17+
18+
new Thread(() -> {
19+
run();
20+
}).start();
21+
});
1922
}
2023

21-
public void run(){
24+
private void run(){
25+
26+
setData();
2227

23-
this.setData(data);
24-
AlgoVisHelper.pause(DELAY);
2528
}
2629

27-
private void setData(CircleData data){
28-
frame.setData(data);
30+
private void setData(){
31+
32+
frame.render(data);
33+
AlgoVisHelper.pause(DELAY);
2934
}
3035

3136
public static void main(String[] args) {
3237

3338
int sceneWidth = 800;
3439
int sceneHeight = 800;
3540

36-
EventQueue.invokeLater(() -> {
37-
AlgoFrame frame = new AlgoFrame("Fractal Visualizer", sceneWidth,sceneHeight);
38-
39-
//CircleData data = new CircleData(sceneWidth/2, sceneHeight/2, sceneWidth/2-2, 10, 10);
40-
CircleData data = new CircleData(sceneWidth/2, sceneHeight/2, sceneWidth/2-2, 500, 2);
41-
42-
AlgoVisualizer vis = new AlgoVisualizer(frame, data);
43-
new Thread(() -> {
44-
vis.run();
45-
}).start();
46-
});
41+
AlgoVisualizer vis = new AlgoVisualizer(sceneWidth, sceneHeight);
4742
}
4843
}

‎09-Fractal-Drawing/03-Fractal-Basics/src/AlgoFrame.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public class AlgoFrame extends JFrame{
1313

1414
private int canvasWidth;
1515
private int canvasHeight;
16-
private JPanel canvas;
1716

1817
public AlgoFrame(String title, int canvasWidth, int canvasHeight){
1918

@@ -41,8 +40,8 @@ public AlgoFrame(String title){
4140
public int getCanvasHeight(){return canvasHeight;}
4241

4342
// data
44-
FractalData data;
45-
public void setData(FractalData data){
43+
privateFractalData data;
44+
public void render(FractalData data){
4645
this.data = data;
4746
repaint();
4847
}

‎09-Fractal-Drawing/03-Fractal-Basics/src/AlgoVisHelper.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import javax.swing.*;
22
import java.awt.*;
33
import java.awt.geom.Ellipse2D;
4-
54
import java.awt.geom.Rectangle2D;
65
import java.lang.InterruptedException;
76

8-
97
public class AlgoVisHelper {
108

119
private AlgoVisHelper(){}
@@ -33,35 +31,35 @@ private AlgoVisHelper(){}
3331
public static final Color White = new Color(0xFFFFFF);
3432

3533

36-
staticpublic void strokeCircle(Graphics2D g, int x, int y, int r){
34+
publicstatic void strokeCircle(Graphics2D g, int x, int y, int r){
3735

3836
Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
3937
g.draw(circle);
4038
}
4139

42-
staticpublic void fillCircle(Graphics2D g, int x, int y, int r){
40+
publicstatic void fillCircle(Graphics2D g, int x, int y, int r){
4341

4442
Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
4543
g.fill(circle);
4644
}
4745

48-
staticpublic void strokeRectangle(Graphics2D g, int x, int y, int w, int h){
46+
publicstatic void strokeRectangle(Graphics2D g, int x, int y, int w, int h){
4947

5048
Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
5149
g.draw(rectangle);
5250
}
5351

54-
staticpublic void fillRectangle(Graphics2D g, int x, int y, int w, int h){
52+
publicstatic void fillRectangle(Graphics2D g, int x, int y, int w, int h){
5553

5654
Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
5755
g.fill(rectangle);
5856
}
5957

60-
staticpublic void setColor(Graphics2D g, Color color){
58+
publicstatic void setColor(Graphics2D g, Color color){
6159
g.setColor(color);
6260
}
6361

64-
staticpublic void setStrokeWidth(Graphics2D g, int w){
62+
publicstatic void setStrokeWidth(Graphics2D g, int w){
6563
int strokeWidth = w;
6664
g.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
6765
}
Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import javafx.scene.input.MouseButton;
2-
31
import java.awt.*;
4-
import java.awt.event.*;
5-
import javax.swing.*;
62

73
public class AlgoVisualizer {
84

@@ -11,38 +7,35 @@ public class AlgoVisualizer {
117
private FractalData data;
128
private AlgoFrame frame;
139

14-
public AlgoVisualizer(AlgoFrame frame, FractalData data){
10+
public AlgoVisualizer(int depth){
11+
12+
data = new FractalData(depth);
13+
int sceneWidth = (int)Math.pow(3, depth);
14+
int sceneHeight = (int)Math.pow(3, depth);
15+
16+
EventQueue.invokeLater(() -> {
17+
frame = new AlgoFrame("Fractal Visualizer", sceneWidth,sceneHeight);
1518

16-
this.frame = frame;
17-
this.data = data;
18-
this.setData(data);
19+
new Thread(() -> {
20+
run();
21+
}).start();
22+
});
1923
}
2024

21-
public void run(){
25+
private void run(){
2226

23-
this.setData(data);
24-
AlgoVisHelper.pause(DELAY);
27+
setData();
2528
}
2629

27-
private void setData(FractalData data){
28-
frame.setData(data);
30+
private void setData(){
31+
frame.render(data);
32+
AlgoVisHelper.pause(DELAY);
2933
}
3034

3135
public static void main(String[] args) {
3236

3337
int depth = 6;
34-
int sceneWidth = (int)Math.pow(3, depth);
35-
int sceneHeight = (int)Math.pow(3, depth);
36-
37-
EventQueue.invokeLater(() -> {
38-
AlgoFrame frame = new AlgoFrame("Fractal Visualizer", sceneWidth,sceneHeight);
3938

40-
FractalData data = new FractalData(depth);
41-
42-
AlgoVisualizer vis = new AlgoVisualizer(frame, data);
43-
new Thread(() -> {
44-
vis.run();
45-
}).start();
46-
});
39+
AlgoVisualizer vis = new AlgoVisualizer(depth);
4740
}
4841
}

‎09-Fractal-Drawing/04-Fractal-with-Interaction/src/AlgoFrame.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,12 @@
33
import java.awt.Dimension;
44
import java.awt.Color;
55
import java.awt.RenderingHints;
6-
import java.util.ArrayList;
7-
import java.util.Map;
8-
import java.util.HashMap;
9-
106
import javax.swing.*;
117

128
public class AlgoFrame extends JFrame{
139

1410
private int canvasWidth;
1511
private int canvasHeight;
16-
private JPanel canvas;
1712

1813
public AlgoFrame(String title, int canvasWidth, int canvasHeight){
1914

@@ -41,8 +36,8 @@ public AlgoFrame(String title){
4136
public int getCanvasHeight(){return canvasHeight;}
4237

4338
// data
44-
FractalData data;
45-
public void setData(FractalData data){
39+
privateFractalData data;
40+
public void render(FractalData data){
4641
this.data = data;
4742
repaint();
4843
}
@@ -73,20 +68,21 @@ public void paintComponent(Graphics g) {
7368

7469
private void drawFractal(Graphics2D g, int x, int y, int w, int h, int depth){
7570

76-
// if( w <= 0 || h <= 0)
77-
// return;
71+
intw_3 = w/3;
72+
inth_3 = h/3;
7873

79-
if(w <= 0) w = 1;
80-
if(h <= 0) h = 1;
74+
if(w_3 <= 0 || h_3 <= 0){
75+
AlgoVisHelper.setColor(g, AlgoVisHelper.Indigo);
76+
AlgoVisHelper.fillRectangle(g, x, y, 1, 1);
77+
return;
78+
}
8179

8280
if( depth == data.depth ){
8381
AlgoVisHelper.setColor(g, AlgoVisHelper.Indigo);
8482
AlgoVisHelper.fillRectangle(g, x, y, w, h);
8583
return;
8684
}
8785

88-
int w_3 = w/3;
89-
int h_3 = h/3;
9086
drawFractal(g, x, y, w_3, h_3, depth+1);
9187
drawFractal(g, x+2*w_3, y, w_3, h_3, depth+1);
9288
drawFractal(g, x+w_3, y+w_3, w_3, h_3, depth+1);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /