Tue Nov 11, 2008 5:43 pm
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.Random;
import javax.swing.*;
public class NoiseImage extends JComponent implements Runnable {
byte[] b;
BufferedImage bufferedImage;
Random ran;
public void init() {
int wid = getSize().width, ht = getSize().height;
int length = ((wid + 5) * ht) / 7;
b = new byte[length];
DataBuffer dataBuffer = new DataBufferByte(b, length);
WritableRaster raster = Raster.createPackedRaster(dataBuffer, wid, ht, 1, null);
ColorModel colorModel = new IndexColorModel(1, 2, new byte[] {
(byte) 0, (byte) 250} , new byte[] {(byte) 0, (byte) 250 }, new byte[] { (byte) 0, (byte) 250 });
bufferedImage = new BufferedImage(colorModel, raster, false, null);
ran = new Random();
new Thread(this).start();
}
public void run() {
while (true) {
ran.nextBytes(b);
repaint();
try {
Thread.sleep(80);
} catch (Exception e) {
}
}
}
public void paint(Graphics graphics) {
if (bufferedImage == null)
init();
graphics.drawImage(bufferedImage, 0, 0, this);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new NoiseImage());
frame.setSize(250, 250);
frame.setVisible(true);
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.