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 e279e87

Browse files
Vicsek Fractal Drawing codes updated.
1 parent b4aab49 commit e279e87

File tree

8 files changed

+263
-0
lines changed

8 files changed

+263
-0
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 javax.swing.*;
7+
8+
public class AlgoFrame extends JFrame{
9+
10+
private int canvasWidth;
11+
private int canvasHeight;
12+
13+
public AlgoFrame(String title, int canvasWidth, int canvasHeight){
14+
15+
super(title);
16+
17+
this.canvasWidth = canvasWidth;
18+
this.canvasHeight = canvasHeight;
19+
20+
AlgoCanvas canvas = new AlgoCanvas();
21+
setContentPane(canvas);
22+
pack();
23+
24+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
25+
setResizable(false);
26+
27+
setVisible(true);
28+
}
29+
30+
public AlgoFrame(String title){
31+
32+
this(title, 1024, 768);
33+
}
34+
35+
public int getCanvasWidth(){return canvasWidth;}
36+
public int getCanvasHeight(){return canvasHeight;}
37+
38+
// data
39+
private FractalData data;
40+
public void render(FractalData data){
41+
this.data = data;
42+
repaint();
43+
}
44+
45+
private class AlgoCanvas extends JPanel{
46+
47+
public AlgoCanvas(){
48+
// 双缓存
49+
super(true);
50+
}
51+
52+
@Override
53+
public void paintComponent(Graphics g) {
54+
super.paintComponent(g);
55+
56+
Graphics2D g2d = (Graphics2D)g;
57+
58+
// 抗锯齿
59+
RenderingHints hints = new RenderingHints(
60+
RenderingHints.KEY_ANTIALIASING,
61+
RenderingHints.VALUE_ANTIALIAS_ON);
62+
hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
63+
g2d.addRenderingHints(hints);
64+
65+
// 具体绘制
66+
drawFractal(g2d, 0, 0, canvasWidth, canvasHeight, 0);
67+
}
68+
69+
private void drawFractal(Graphics2D g, int x, int y, int w, int h, int depth){
70+
71+
if( depth == data.depth ){
72+
AlgoVisHelper.setColor(g, AlgoVisHelper.Indigo);
73+
AlgoVisHelper.fillRectangle(g, x, y, w, h);
74+
return;
75+
}
76+
77+
if(w <= 1 || h <= 1){
78+
AlgoVisHelper.setColor(g, AlgoVisHelper.Indigo);
79+
AlgoVisHelper.fillRectangle(g, x, y, Math.max(w,1), Math.max(h,1));
80+
return;
81+
}
82+
83+
int w_3 = w/3;
84+
int h_3 = h/3;
85+
drawFractal(g, x + w_3, y, w_3, h_3, depth + 1);
86+
drawFractal(g, x, y + h_3, w_3, h_3, depth + 1);
87+
drawFractal(g, x + w_3, y + h_3, w_3, h_3, depth + 1);
88+
drawFractal(g, x + 2 * w_3, y + h_3, w_3, h_3, depth + 1);
89+
drawFractal(g, x + w_3, y + 2 * h_3, w_3, h_3, depth + 1);
90+
91+
return;
92+
}
93+
94+
@Override
95+
public Dimension getPreferredSize(){
96+
return new Dimension(canvasWidth, canvasHeight);
97+
}
98+
}
99+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import javax.swing.*;
2+
import java.awt.*;
3+
import java.awt.geom.Ellipse2D;
4+
import java.awt.geom.Rectangle2D;
5+
import java.lang.InterruptedException;
6+
7+
public class AlgoVisHelper {
8+
9+
private AlgoVisHelper(){}
10+
11+
public static final Color Red = new Color(0xF44336);
12+
public static final Color Pink = new Color(0xE91E63);
13+
public static final Color Purple = new Color(0x9C27B0);
14+
public static final Color DeepPurple = new Color(0x673AB7);
15+
public static final Color Indigo = new Color(0x3F51B5);
16+
public static final Color Blue = new Color(0x2196F3);
17+
public static final Color LightBlue = new Color(0x03A9F4);
18+
public static final Color Cyan = new Color(0x00BCD4);
19+
public static final Color Teal = new Color(0x009688);
20+
public static final Color Green = new Color(0x4CAF50);
21+
public static final Color LightGreen = new Color(0x8BC34A);
22+
public static final Color Lime = new Color(0xCDDC39);
23+
public static final Color Yellow = new Color(0xFFEB3B);
24+
public static final Color Amber = new Color(0xFFC107);
25+
public static final Color Orange = new Color(0xFF9800);
26+
public static final Color DeepOrange = new Color(0xFF5722);
27+
public static final Color Brown = new Color(0x795548);
28+
public static final Color Grey = new Color(0x9E9E9E);
29+
public static final Color BlueGrey = new Color(0x607D8B);
30+
public static final Color Black = new Color(0x000000);
31+
public static final Color White = new Color(0xFFFFFF);
32+
33+
34+
public static void strokeCircle(Graphics2D g, int x, int y, int r){
35+
36+
Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
37+
g.draw(circle);
38+
}
39+
40+
public static void fillCircle(Graphics2D g, int x, int y, int r){
41+
42+
Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
43+
g.fill(circle);
44+
}
45+
46+
public static void strokeRectangle(Graphics2D g, int x, int y, int w, int h){
47+
48+
Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
49+
g.draw(rectangle);
50+
}
51+
52+
public static void fillRectangle(Graphics2D g, int x, int y, int w, int h){
53+
54+
Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
55+
g.fill(rectangle);
56+
}
57+
58+
public static void setColor(Graphics2D g, Color color){
59+
g.setColor(color);
60+
}
61+
62+
public static void setStrokeWidth(Graphics2D g, int w){
63+
int strokeWidth = w;
64+
g.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
65+
}
66+
67+
public static void pause(int t) {
68+
try {
69+
Thread.sleep(t);
70+
}
71+
catch (InterruptedException e) {
72+
System.out.println("Error sleeping");
73+
}
74+
}
75+
76+
public static void putImage(Graphics2D g, int x, int y, String imageURL){
77+
78+
ImageIcon icon = new ImageIcon(imageURL);
79+
Image image = icon.getImage();
80+
81+
g.drawImage(image, x, y, null);
82+
}
83+
84+
public static void drawText(Graphics2D g, String text, int centerx, int centery){
85+
86+
if(text == null)
87+
throw new IllegalArgumentException("Text is null in drawText function!");
88+
89+
FontMetrics metrics = g.getFontMetrics();
90+
int w = metrics.stringWidth(text);
91+
int h = metrics.getDescent();
92+
g.drawString(text, centerx - w/2, centery + h);
93+
}
94+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import java.awt.*;
2+
import java.awt.event.*;
3+
4+
public class AlgoVisualizer {
5+
6+
private static int DELAY = 40;
7+
8+
private FractalData data;
9+
private AlgoFrame frame;
10+
11+
public AlgoVisualizer(int maxDepth){
12+
13+
data = new FractalData(maxDepth);
14+
int sceneWidth = (int)Math.pow(3, maxDepth);
15+
int sceneHeight = (int)Math.pow(3, maxDepth);
16+
17+
EventQueue.invokeLater(() -> {
18+
frame = new AlgoFrame("Fractal Visualizer", sceneWidth,sceneHeight);
19+
frame.addKeyListener(new AlgoKeyListener());
20+
new Thread(() -> {
21+
run();
22+
}).start();
23+
});
24+
}
25+
26+
private void run(){
27+
28+
setData(data.depth);
29+
}
30+
31+
private void setData(int depth){
32+
data.depth = depth;
33+
34+
frame.render(data);
35+
AlgoVisHelper.pause(DELAY);
36+
}
37+
38+
public void addAlgoKeyListener(){
39+
frame.addKeyListener(new AlgoKeyListener());
40+
}
41+
42+
private class AlgoKeyListener extends KeyAdapter{
43+
44+
@Override
45+
public void keyReleased(KeyEvent event){
46+
//System.out.println("Key released:" + event);
47+
if(event.getKeyChar() >= '0' && event.getKeyChar() <= '9'){
48+
int depth = event.getKeyChar() - '0';
49+
setData(depth);
50+
}
51+
}
52+
}
53+
54+
public static void main(String[] args) {
55+
56+
int maxDepth = 6;
57+
58+
AlgoVisualizer vis = new AlgoVisualizer(maxDepth);
59+
}
60+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
public class FractalData {
3+
4+
public int depth;
5+
6+
public FractalData(int depth){
7+
this.depth = depth;
8+
}
9+
10+
}

0 commit comments

Comments
(0)

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