Hypnosis animation : Animation « 2D Graphics GUI « Java






Hypnosis animation

Hypnosis animation
     
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.GeneralPath;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class Hypnosis1 extends JComponent implements Runnable {
  private int[] coordinates;

  private int[] deltas;

  private Paint paint;

  public Hypnosis1(int numberOfSegments) {
    int numberOfCoordinates = numberOfSegments * 4 + 2;
    coordinates = new int[numberOfCoordinates];
    deltas = new int[numberOfCoordinates];
    for (int i = 0; i < numberOfCoordinates; i++) {
      coordinates[i] = (int) (Math.random() * 300);
      deltas[i] = (int) (Math.random() * 4 + 3);
      if (deltas[i] > 4)
        deltas[i] = -(deltas[i] - 3);
    }
    paint = new GradientPaint(0, 0, Color.blue, 20, 10, Color.red, true);

    Thread t = new Thread(this);
    t.start();
  }

  public void run() {
    try {
      while (true) {
        timeStep();
        repaint();
        Thread.sleep(1000 / 24);
      }
    } catch (InterruptedException ie) {
    }
  }

  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Shape s = createShape();
    g2.setPaint(paint);
    g2.fill(s);
    g2.setPaint(Color.white);
    g2.draw(s);
  }

  private void timeStep() {
    Dimension d = getSize();
    if (d.width == 0 || d.height == 0)
      return;
    for (int i = 0; i < coordinates.length; i++) {
      coordinates[i] += deltas[i];
      int limit = (i % 2 == 0) ? d.width : d.height;
      if (coordinates[i] < 0) {
        coordinates[i] = 0;
        deltas[i] = -deltas[i];
      } else if (coordinates[i] > limit) {
        coordinates[i] = limit - 1;
        deltas[i] = -deltas[i];
      }
    }
  }

  private Shape createShape() {
    GeneralPath path = new GeneralPath();
    path.moveTo(coordinates[0], coordinates[1]);
    for (int i = 2; i < coordinates.length; i += 4)
      path.quadTo(coordinates[i], coordinates[i + 1], coordinates[i + 2], coordinates[i + 3]);
    path.closePath();
    return path;
  }

  public static void main(String[] args) {
    JFrame f = new JFrame("Hypnosis");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new Hypnosis1(4));
    f.setSize(300, 300);
    f.setVisible(true);
  }
}

           
         
    
    
    
    
  








Related examples in the same category

1.Is Event Dispatcher ThreadIs Event Dispatcher Thread
2.Timer based animation
3.A rotating and scaling rectangle.
4.Fade out an image: image gradually get more transparent until it is completely invisible.
5.Font size animation
6.Noise ImageNoise Image
7.How to create Animation: Paint and threadHow to create Animation: Paint and thread
8.How to create animationHow to create animation
9.Animation: bounce
10.Image BouncerImage Bouncer
11.Text animationText animation
12.Buffered Animation DemoBuffered Animation Demo
13.Bouncing CircleBouncing Circle
14.Hypnosis SpiralHypnosis Spiral
15.Animator DemoAnimator Demo
16.Towers of Hanoi
17.Make your own animation from a series of images
18.Composition technique in this animation.
19.Animated Button
20.Animated Message Panel
21.Animated PasswordField
22.Animated TextField
23.A simple spring simulation in one dimension
24.Shows an animated bouncing ball
25.Shows animated bouncing ballsShows animated bouncing balls