分类: Java
2009-08-25 19:58:32
Creating a Buffered Image from an Array of Color-Indexed Pixel Values
(出自:http://www.exampledepot.com/egs/java.awt.image/Mandelbrot2.html)
This example demonstrates how to convert a byte array of pixel values that are indices to a color table into a BufferedImage
. In particular, the example generates the Mandelbrot set in a byte buffer and combines this data with a SampleModel
, ColorModel
, and Raster
into a BufferedImage
. A 16-color index color model is used to represent the pixel colors.
import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.image.*; // Instantiate this class and then use the draw() method to draw the // generated on the graphics context. public class Mandelbrot2 { // Holds the generated image Image image; // 16-color model; this method is defined in // e660 Creating an Image from an Array of Color-Indexed Pixel Values ColorModel colorModel = generateColorModel(); public Mandelbrot2(int width, int height) { // Initialize with default location this(width, height, new Rectangle2D.Float(-2.0f, -1.2f, 3.2f, 2.4f)); } public Mandelbrot2(int width, int height, Rectangle2D.Float loc) { // Generate the pixel data; this method is defined in // e660 Creating an Image from an Array of Color-Indexed Pixel Values byte[] pixels = generatePixels(width, height, loc); // Create a data buffer using the byte buffer of pixel data. // The pixel data is not copied; the data buffer uses the byte buffer array. DataBuffer dbuf = new DataBufferByte(pixels, width*height, 0); // The number of banks should be 1 int numBanks = dbuf.getNumBanks(); // 1 // Prepare a sample model that specifies a storage 4-bits of // pixel datavd in an 8-bit data element int bitMasks[] = new int[]{(byte)0xf}; SampleModel sampleModel = new SinglePixelPackedSampleModel( DataBuffer.TYPE_BYTE, width, height, bitMasks); // Create a raster using the sample model and data buffer WritableRaster raster = Raster.createWritableRaster(sampleModel, dbuf, null); // Combine the color model and raster into a buffered image image = new BufferedImage(colorModel, raster, false, null);//new java.util.Hashtable()); } public void draw(Graphics g, int x, int y) { g.drawImage(image, x, y, null); } }
Here's some code that uses the Mandelbrot2
class:
class RunMandelbrot2 { static public void main(String[] args) { new RunMandelbrot2(); } RunMandelbrot2() { Frame frame = new Frame("Mandelbrot2 Set"); frame.add(new MyCanvas()); frame.setSize(300, 200) ; frame.setVisible(true); } class MyCanvas extends Canvas { Mandelbrot2 mandelbrot; MyCanvas() { // Add a listener for resize events addComponentListener(new ComponentAdapter() { // This method is called when the component's size changes public void componentResized(ComponentEvent evt) { Component c = (Component)evt.getSource(); // Get new size Dimension newSize = c.getSize(); // Regenerate the image mandelbrot = new Mandelbrot2(newSize.width, newSize.height); c.repaint(); } }); } public void paint(Graphics g) { if (mandelbrot != null) { mandelbrot.draw(g, 0, 0); } } } }
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);
}
}