import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.util.*;public class JTImageCanvasTest extends Frame implements Runnable { JTImageCanvas canvas; final static int IMAGE_WIDTH = 128, IMAGE_HEIGHT = 128; Thread thread; public JTImageCanvasTest() { setLayout(new BorderLayout()); add("Center", canvas = new JTImageCanvas(IMAGE_WIDTH, IMAGE_HEIGHT)); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); pack(); setVisible(true); thread = new Thread(this); thread.setPriority(Thread.NORM_PRIORITY); thread.start(); } public static void main(String args[]) { new JTImageCanvasTest(); } public void run() { for (int y = 0; y < IMAGE_HEIGHT; y++) { for (int x = 0; x < IMAGE_WIDTH; x++) { int r = (x * 256) / IMAGE_WIDTH; int g = 0x80; int b = (y * 256) / IMAGE_HEIGHT; canvas.putPixel(x, y, r, g, b); } } } } class JTImageCanvas extends Canvas implements ImageProducer{ Dimension size; Image image; int pixels[]; DirectColorModel cm; ImageConsumer consumer; Hashtable properties; public JTImageCanvas(int w, int h) { size = new Dimension(w, h); pixels = new int[w * h]; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { pixels[y * w + x] = 0xffaabbcc; } } properties = new Hashtable(); cm = new DirectColorModel(32, 0xff0000, 0xff00, 0xff, 0xff000000); image = createImage(this); } public boolean imageUpdate(Image image, int flags, int x, int y, int w, int h) { // As far as i've learnt, this is the first method to // be overridden that's called from the ImageConsumer, // and thus being able to affect the clipping // rectangle by calling repaint() with the boundaries // of the subimage being delivered, and since I still // get the boundaries of the whole image, and it's // the ImageConsumer that's respnsible for "mapping" it // onto the original image, what to do? System.out.println(x + "," + y + "," + w + "," + h); if ((flags & FRAMEBITS) != 0) { repaint(); return true; } if ((flags & ALLBITS) != 0) { repaint(); return false; } return true; } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { g.drawImage(image, 0, 0, this); } public Dimension getPreferredSize() { return size; } public Dimension getMinimumSize() { return size; } public void putPixel(int x, int y, int r, int g, int b) { pixels[size.width * y + x] = (0xff000000 | (r << 16) | (g << 8) | b); newPixels(x, y, 1, 1); } // ImageProducer methods public synchronized void addConsumer(ImageConsumer ic) { consumer = ic; initConsumer(ic); sendPixels(0, 0, size.width, size.height); ic.imageComplete(ImageConsumer.SINGLEFRAMEDONE); } public synchronized boolean isConsumer(ImageConsumer ic) { return ic == consumer; } public void startProduction(ImageConsumer ic) { addConsumer(ic); } public synchronized void removeConsumer(ImageConsumer ic) { consumer = null; } public void requestTopDownLeftRightResend(ImageConsumer ic) {} public synchronized void newPixels(int x, int y, int w, int h) { sendPixels(x, y, w, h); consumer.imageComplete(ImageConsumer.SINGLEFRAMEDONE); } private void initConsumer(ImageConsumer ic) { ic.setDimensions(size.width, size.height); ic.setProperties(properties); ic.setColorModel(cm); ic.setHints(ImageConsumer.RANDOMPIXELORDER); } private void sendPixels(int x, int y, int w, int h) { consumer.setPixels(x, y, w, h, cm, pixels, y * size.width + x, w); } }
I need some help on ImageConsumers / ImageProducers. I create an ImageProducer
instance, that holds an array of ARGB values. When i only modify a specific
rectangular region of pixels in the pixel array, i only want to repaint
that area. So i send the new pixels to the ImageConsumer, which as far
as I've learnt, is responsible for "mapping" this subimage onto the original
image. But when i override the imageUpdate method in my Canvas class, the
consumer still tells me to repaint the entire image, even though i specified
the boundaries in the setPixels call. This is really annoying. I am currently
working on a raytracer, and I want to have full control over the image
data, and i want to be able to update only a certain pixel. Please help!
I've included some sourcecode that's compile- and runnable with the standard
JDK.
