Tree Fractal example code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Color; | |
import java.awt.Graphics; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.SwingUtilities; | |
/** | |
* @author psychocoder | |
*/ | |
public class L_Systems_Tree extends JPanel { | |
private static final long serialVersionUID = 1L; | |
public L_Systems_Tree() { | |
} | |
private void drawTree(Graphics g, int x1, int y1, double angle, int depth) { | |
if (depth == 0) | |
return; | |
int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 12.0); | |
int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 12.0); | |
g.drawLine(x1, y1, x2, y2); | |
drawTree(g, x2, y2, angle - 20, depth - 1); | |
drawTree(g, x2, y2, angle + 20, depth - 1); | |
} | |
@Override | |
public void paintComponent(Graphics g) { | |
g.setColor(Color.DARK_GRAY); | |
drawTree(g, getWidth() / 2, getHeight(), -90, 10); | |
} | |
public static void main(String... args) { | |
final JFrame frame = new JFrame("L Systems - Tree Fractal"); | |
SwingUtilities.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
frame.setContentPane(new L_Systems_Tree()); | |
frame.setSize(1000, 700); | |
frame.setLocationRelativeTo(null); | |
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); | |
frame.setResizable(true); | |
frame.setVisible(true); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.BasicStroke; | |
import java.awt.Color; | |
import java.awt.GradientPaint; | |
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
import java.awt.Paint; | |
import java.awt.RenderingHints; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.SwingUtilities; | |
/** | |
* @author psychocoder | |
*/ | |
public class L_Systems_Tree extends JPanel { | |
private static final long serialVersionUID = 1L; | |
private void drawTree(Graphics2D g, int x1, int y1, double angle, int depth) { | |
if (depth == 0) { | |
return; | |
} | |
int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 12.0); | |
int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 12.0); | |
g.setStroke(new BasicStroke(2.0f, BasicStroke.JOIN_ROUND, BasicStroke.JOIN_MITER, 5.0f, new float[]{6.0f, 2.0f}, 1.0f)); | |
g.drawLine(x1, y1, x2, y2); | |
g.setColor(Color.green); | |
drawTree(g, x2, y2, angle - 25, depth - 1); | |
g.setColor(Color.red); | |
drawTree(g, x2, y2, angle - 15, depth - 1); | |
g.setColor(Color.WHITE); | |
drawTree(g, x2, y2, angle + 25, depth - 1); | |
} | |
@Override | |
public void paintComponent(Graphics g) { | |
Graphics2D g2 = (Graphics2D) g; | |
g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); | |
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); | |
g2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); | |
g2.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); | |
g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); | |
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); | |
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); | |
g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); | |
g2.setColor(new Color(25, 25, 25)); | |
Paint p = new GradientPaint(0, 110, Color.black, 10, getHeight(), new Color(25, 25, 25)); | |
g2.setPaint(p); | |
g2.fillRect(0, 0, getWidth(), getHeight()); | |
g2.setColor(Color.red); | |
for (int i = 1; i < 10; i++) { | |
drawTree(g2, getWidth() / 2, getHeight()+10, -100, i); | |
} | |
} | |
public static void main(String... args) { | |
final JFrame frame = new JFrame("L Systems - Tree Fractal"); | |
SwingUtilities.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
frame.setContentPane(new L_Systems_Tree()); | |
frame.setSize(1000, 700); | |
frame.setLocationRelativeTo(null); | |
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); | |
frame.setResizable(true); | |
frame.setVisible(true); | |
} | |
}); | |
} |
Share this post on your social networks if you like this post.
Thank you.