LoginSignup
11
10

More than 5 years have passed since last update.

「プログラムでシダを描画する」をJavaで描画する(AWT)

Last updated at Posted at 2014-06-03

空前のシダ描画ブーム到来!?(^^;)
あなたも得意なプログラミング言語でシダを描画してみよう!

Javaのプログラムがまだ出てないようなので、Emacs Lisp版に続いてJava (AWT)で書いてみました。
Javaアプレットが流行ってた頃を思い出すなぁw
(バグとかご意見ありましたら https://twitter.com/akmiyoshi までお願いします)

2014-0604-0422.png

Sida.java
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.Random;

public class Sida extends Frame {

    static final int SIDA_WIDTH = 500;
    static final int SIDA_HEIGHT = 500;
    static final Random rnd = new Random();
    static final BufferedImage buffImage
            = new BufferedImage(SIDA_WIDTH, SIDA_HEIGHT,
                    BufferedImage.TYPE_INT_BGR);
    static final Graphics2D graphics = buffImage.createGraphics();

    public static void main(String[] args) {
        //graphics.setBackground(Color.DARK_GRAY);
        graphics.setBackground(Color.BLACK);
        graphics.clearRect(0, 0, SIDA_WIDTH, SIDA_HEIGHT);
        f(20, 0, 0);
        new Sida();
    }

    public Sida() {
        super("Sida in Java");
        setSize(SIDA_WIDTH, SIDA_HEIGHT);
        addWindowListener(
                new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                }
        );
        this.setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        g.drawImage(buffImage, 0, 0, this);
    }

    static double W1x(double x, double y) {
        return 0.836 * x + 0.044 * y;
    }

    static double W1y(double x, double y) {
        return -0.044 * x + 0.836 * y + 0.169;
    }

    static double W2x(double x, double y) {
        return -0.141 * x + 0.302 * y;
    }

    static double W2y(double x, double y) {
        return 0.302 * x + 0.141 * y + 0.127;
    }

    static double W3x(double x, double y) {
        return 0.141 * x - 0.302 * y;
    }

    static double W3y(double x, double y) {
        return 0.302 * x + 0.141 * y + 0.169;
    }

    static double W4x(double x, double y) {
        return 0;
    }

    static double W4y(double x, double y) {
        return 0.175337 * y;
    }

    static void f(int k, double x, double y) {
        if (0 < k) {
            f(k - 1, W1x(x, y), W1y(x, y));
            if (rnd.nextDouble() < 0.3) {
                f(k - 1, W2x(x, y), W2y(x, y));
            }
            if (rnd.nextDouble() < 0.3) {
                f(k - 1, W3x(x, y), W3y(x, y));
            }
            if (rnd.nextDouble() < 0.3) {
                f(k - 1, W4x(x, y), W4y(x, y));
            }
        } else {
            plot((x * SIDA_WIDTH * 0.98) + (SIDA_WIDTH * 0.5),
                    SIDA_HEIGHT - (y * SIDA_HEIGHT * 0.98));
        }
    }

    static void plot(double xd, double yd) {
        int x = (int) xd;
        int y = (int) yd;
        graphics.setPaint(Color.green);
        graphics.drawLine(x, y, x, y);
    }
}
11
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
10