Jump to content

  • Log In with Google      Sign In   
  • Create Account

[java] getGraphics() & createImage()


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Stefpet   Members   -  Reputation: 122

Like
Likes
Like

Posted 29 May 2000 - 09:26 AM

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?

Sponsor:

#2 SteveMeister   Members   -  Reputation: 122

Like
Likes
Like

Posted 30 May 2000 - 01:13 AM

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()


#3 Stefpet   Members   -  Reputation: 122

Like
Likes
Like

Posted 30 May 2000 - 10:24 AM

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);




#4 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 31 May 2000 - 11:18 AM

jkjkj




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS