#### Archived

This topic is now archived and is closed to further replies.

# [java] getGraphics() & createImage()

This topic is 6441 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''ve got a little problem... let''s see if someone out there might have a nice solution or may enlighten me a little bit. I create an image (fieldImg) and then get a graphic buffer to it (fieldBuffer). I then draw on the buffer (fieldBuffer) and finally draw the whole whole image (fieldImg) on the real backbuffer which is then drawn to screen in paint().
fieldImg = createImage(WIDTH, HEIGHT);
fieldBuffer = fieldImg.getGraphics();
fieldBuffer.drawString("test", 120, 30);

bufferg.drawImage(fieldImg, 0, 0, this);

This work. However, what I''m trying to do but fail is to, in various places, change the whole fieldImg from a MemoryImageSource, but then continue as usual with drawing the string and then putting it on the bufferg.
fieldImg = createImage(new MemoryImageSource(WIDTH, HEIGHT, fieldimage, 0, WIDTH));
fieldBuffer = fieldImg.getGraphics();
fieldBuffer.drawString("test", 120, 30);

bufferg.drawImage(fieldImg, 0, 0, this);

However, after the createImage(new MemoryImageSource()) fieldBuffer seems completely dead. Nothing happens when using fieldBuffer.drawString and when using bufferg.drawImage it''s exactly the MemoryImageSource that gets drawn but the drawString doesn''t affect at all. So... How may I get the MemoryImageSource into an Image, but still draw to that Image afterwards?

##### Share on other sites
MemoryImageSource is an ImageProducer, and Images created based on them behave a little differently from normal images. It maintains an array of pixels which represent the image. In order to change the image contents, you actually have to manipulate the array itself, using one of the MemoryImageSource.newPixels methods. You''ll also want to call MemoryImageSource.setAnimated & set that to true.
Anyway, you''re basically not going to be able to do what you want to do very easily if you use a MemoryImageSource, so I''d recommend reverting back to the way you were originally doing it.
Also, don''t forget that Swing components have built-in double buffering, so if you call super.paint() within a Swing component''s paint method, and you''ve set double buffering on on the component, you''ll get double buffering behavior for free.

JComponent.setDoubleBuffered(boolean flag)
boolean JComponent.isDoubleBuffered()

##### Share on other sites
Thanks for your comment, that explains the strange behaviour.

I did a little work around that seems pretty sufficent for what I had in mind. Instead of trying to manipulate the image directly, I draw it to another buffer ("real" image graphic object) and then work with that one.

tripleImg = createImage(WIDTH, HEIGHT);tripleBuffer = tripleImg.getGraphics();fieldImg = createImage(new MemoryImageSource(WIDTH, HEIGHT, fieldimage, 0, WIDTH));tripleBuffer.drawImage(fieldImg, 0, 0, this)tripleBuffer.drawString("test", 120, 30);bufferg.drawImage(tripleImg, OFFSETX, OFFSETY, this);