Noise Image : Animation « 2D Graphics GUI « Java






Noise Image

Noise Image
     
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.IndexColorModel;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.util.Random;

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

public class StaticGenerator extends JComponent implements Runnable {
  byte[] data;

  BufferedImage image;

  Random random;

  public void initialize() {
    int w = getSize().width, h = getSize().height;
    int length = ((w + 7) * h) / 8;
    data = new byte[length];
    DataBuffer db = new DataBufferByte(data, length);
    WritableRaster wr = Raster.createPackedRaster(db, w, h, 1, null);
    ColorModel cm = new IndexColorModel(1, 2, new byte[] { (byte) 0, (byte) 255 }, new byte[] {
        (byte) 0, (byte) 255 }, new byte[] { (byte) 0, (byte) 255 });
    image = new BufferedImage(cm, wr, false, null);
    random = new Random();
    new Thread(this).start();
  }

  public void run() {
    while (true) {
      random.nextBytes(data);
      repaint();
      try {
        Thread.sleep(1000 / 24);
      } catch (InterruptedException e) { /* die */
      }
    }
  }

  public void paint(Graphics g) {
    if (image == null)
      initialize();
    g.drawImage(image, 0, 0, this);
  }

  public static void main(String[] args) {
    JFrame f = new JFrame();
    f.add(new StaticGenerator());
    f.setSize(300, 300);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    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.Hypnosis animationHypnosis animation
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