Código 1: (Blogspot_Fractal.java):
package blogspot_fractal;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Blogspot_Fractal {
public static void main(String[] args) {
int x = 300;
int y = 256;
int angulo = -90;
int depth = 9;
BufferedImage imagen = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB);
Dibujo.Dibujar((Graphics2D) imagen.getGraphics(), x / 2, y, angulo, depth);
try {
ImageIO.write(imagen, "png", new File("Arbol.png"));
} catch (IOException e) {
System.out.println("Error de escritura");
}
}
}
Código 2: (Dibujo.java):
package blogspot_fractal;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
class Dibujo {
public static void Dibujar(Graphics2D g, int x1, int y1, double angle, int depth) {
if (depth == 0) {
return;
}
g.setColor(Color.GREEN);
// grosor rama dependiendo de la profundidad
g.setStroke(new BasicStroke((float) depth));
//Filtro antialiasing
g.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 5.0);
int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 6.0);
g.drawLine(x1, y1, x2, y2);
Dibujar(g, x2, y2, angle - 30, depth - 1);
Dibujar(g, x2, y2, angle + 30, depth - 1);
}
}
Resultado:
Una forma sencilla y rápida de aprender JAVA, observando y deduciendo cómo se comporta el lenguaje a través de ejemplos prácticos.
Archivo del blog
Mostrando entradas con la etiqueta fractal. Mostrar todas las entradas
Mostrando entradas con la etiqueta fractal. Mostrar todas las entradas
miércoles, 3 de agosto de 2022
Gráficos 2D. Creación de un fractal 2 (árbol).
Suscribirse a:
Entradas (Atom)
Con la tecnología de Blogger.