Total Pageviews

11 Feb 2013

Java2D Animation


import java.awt.BufferCapabilities;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

public class MainFrame extends JFrame {

    private boolean debug = true;
    private volatile int fps;
    private int frameCount;
    private long time;
    private static final Font DEBUG_FONT = new Font("Consolas", Font.BOLD, 15);
    private volatile double x, x1, y, y1, a, a1, b, b1;
    private volatile double t, t1, r, r1;
    private Color[] colors = new Color[25];

    public MainFrame () {
        for (int i = 10, j = 0; j < colors.length; i += 10, j++) {
            colors[j] = new Color(0, 50, 245, i / 4);
        }

        setCursor(Toolkit.getDefaultToolkit().createCustomCursor(Toolkit.getDefaultToolkit().createImage(new byte[0]), new Point(0, 0), "none"));
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setUndecorated(true);
        setIgnoreRepaint(true);
        setVisible(true);
        GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        device.setFullScreenWindow(this);
        createBufferStrategy(2);

        new Thread() {

            @Override
            public void run () {
                int i = 0;
                while (true) {
                    if (i == 0) {
                        x1 = x;
                        y1 = y;
                        a1 = a;
                        b1 = b;
                        t1 = t;
                        r1 = r;
                        x = -100 + Math.random() * 200;
                        y = 300 + Math.random() * 150;
                        a = 40 + Math.random() * 100;
                        b = 0.4 + Math.random() * 0.6;
                        t = Math.random() * Math.PI;
                        r = Math.random() * Math.PI;
                    } else {
                        x1 += (x - x1) * 0.1;
                        y1 += (y - y1) * 0.1;
                        a1 += (a - a1) * 0.1;
                        b1 += (b - b1) * 0.1;
                        t1 += (t - t1) * 0.1;
                        r1 += (r - r1) * 0.1;
                    }
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    i++;
                    if (i == 15) {
                        i = 0;
                    }
                }
            }
        }.start();

        mainLoop();
    }

    private void paintDebug (Graphics2D g) {
        Color c = g.getColor();
        Font f = g.getFont();
        g.setColor(Color.WHITE);
        g.setFont(DEBUG_FONT);

        BufferCapabilities cap = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().getBufferCapabilities();
        g.drawString("Multi-Buffer: " + cap.isMultiBufferAvailable(), 10, 20);
        g.drawString("   Page-Flip: " + cap.isPageFlipping(), 10, 40);
        g.drawString("         FPS: " + fps, 10, 60);

        g.setFont(f);
        g.setColor(c);
    }

    private void paintContent (Graphics2D g) {
        g.setBackground(Color.BLACK);
        g.clearRect(0, 0, getWidth(), getHeight());

        AffineTransform trans = g.getTransform();

        for (int k = 0; k < colors.length; k++) {
            g.setColor(colors[k]);
            double x = this.x - (double) k / colors.length * (this.x - x1);
            double y = this.y - (double) k / colors.length * (this.y - y1);
            double a = this.a - (double) k / colors.length * (this.a - a1);
            double b = this.b - (double) k / colors.length * (this.b - b1);
            double t = this.t - (double) k / colors.length * (this.t - t1);
            g.rotate(t, getWidth() / 2, getHeight() / 2);
            for (double i = 0; i < 3 * Math.PI / b; i += 0.1) {
                g.drawLine((int) (x + i * 120 * b), (int) (y + a * Math.sin(b * i)), (int) (x1 + (i + Math.random()) * 120 * b1), (int) (y1 + a1 * Math.sin(b1 * (i + Math.random()))));
            }
            g.setTransform(trans);
        }

        for (int k = 0; k < colors.length; k++) {
            g.setColor(colors[k]);
            double x = this.x - (double) k / colors.length * (this.x - x1);
            double y = this.y - (double) k / colors.length * (this.y - y1);
            double a = this.a - (double) k / colors.length * (this.a - a1);
            double b = this.b - (double) k / colors.length * (this.b - b1);
            double r = this.r - (double) k / colors.length * (this.r - r1);
            g.rotate(r, getWidth() / 2, getHeight() / 2);
            for (double i = 0; i < 3 * Math.PI / b; i += 0.1) {
                g.drawLine((int) (x + i * 120 * b), (int) (y + a * Math.sin(b * i)), (int) (x1 + (i + 1) * 120 * b1), (int) (y1 + a1 * Math.sin(b1 * (i + 1))));
            }
            g.setTransform(trans);
        }

    }

    private void mainLoop () {
        while (true) {
            BufferStrategy stg = getBufferStrategy();
            Graphics2D g = (Graphics2D) stg.getDrawGraphics();
            g.setRenderingHints(Configuration.getBestHints());
            paintContent(g);
            if (debug) {
                paintDebug(g);
            }

            g.dispose();
            if (!stg.contentsLost()) {
                stg.show();
                frameCount++;
                long now = System.currentTimeMillis();
                if (now - time >= 1000) {
                    fps = frameCount;
                    frameCount = 0;
                    time = now;
                }
            }
            Toolkit.getDefaultToolkit().sync();
        }
    }

    public static void main (String[] args) {
        // TODO Auto-generated method stub
        new MainFrame();
    }
}

No comments:

Post a Comment