• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using createImage() on Swing Objects

 
Arin Reese
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I am currently using double buffering to create a paint program and I want to allow the user to place text on the screen. I want to use JLabel because it allows the user to edit the font and the size of the text that they are going to place, something that drawString from Graphics does not offer. This is currently how I am trying to achieve this:


I am currently trying to get a image of the jLabel and then I want to draw that image onto my background buffer for my double buffering. But for some reason the image always returns null and is never drawn onto the back buffer. I was wondering if anyone had any clues on how to get this to work or if there was a easier way to achieve what I am currently working on.
 
Andrea Binello
Ranch Hand
Posts: 32
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arin Reese wrote:I want to use JLabel because it allows the user to edit the font and the size of the text that they are going to place, something that drawString from Graphics does not offer.

Your point of view is wrong. drawString of Graphics/Graphics2D draws a text using font, color, etc... from the graphics context. That is, other properties of Graphics, configurable by setFont, setColor, etc...

Arin Reese wrote:I am currently trying to get a image of the jLabel and then I want to draw that image onto my background buffer for my double buffering. But for some reason the image always returns null and is never drawn onto the back buffer.

This createImage is from java.awt.Component. From javadoc documentation:

Creates an off-screen drawable image to be used for double buffering.

This method does not create an Image with the representation of the JLabel!! It creates a blank image that is "compatible" (same color model, etc...) with JLabel and that is typically usable for double-buffering or other off-screen uses.

And there is also another question: when you create a new component and don't make it "displayable" (see isDisplayable() for the meaning of displayable), its physical size is 0x0 and a createImage(0,0) always return null.

EDIT: sorry, I made a little mistake due I didn't remember: calling createImage with width=0 and/or height=0 causes an IllegalArgumentException. createImage returns null if the component is not displayable (or in exceptional case if the system is "headless").
 
Sam Gooding
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's what Andrea said. createImage doesn't mean "take a selfie of the Component that called it" (although it could have meant that, if the authors had intended it that way!).

To take a "picture" of a JLabel in a Swing application, you'd have to do something like:
1) go into the JLabel's paint() method, override it to do nothing new except capture (or perhaps copy the buffer paint() itself creates) the pixels which are about to otherwise be blitted to the screen into a buffer.

paiint() is where the image of the JLabel is created in the first place.

2) Then you would save that buffer to a file.

3) Now you have a file of bytes which if interpreted as an Image object by loading them as such, will render the JLabel.

Never done it myself, but that is where I would start tinkering
 
Piet Souris
Rancher
Posts: 1630
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JLabel is oke, I think. It has indeed easy optons to change the font, the color, whether it is transparant, et cetera.

My idea is to use some List for all your paintings. You would have a BufferedImage for your basic drawings in the top,
shapes like a circle, labels for text, et cetera.
In your paintComponent() of the panel you would simply draw all the figures from the list, from the first to the last.

And when the user presses some 'oke' button, write all to your basic BufferedImage, creating the final picture.

That way you can always edit all the different elements of your painting. This is just like the layering that Photoshop does
(I think, although their techniques may be a little bit more refined )

Greetings,
Piet
 
Piet Souris
Rancher
Posts: 1630
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On second thoughts, it is not so easy to draw a JLabel in some paintComponent() routine!
So, another idea is to create some special text object, that has a String and all the font
information needed, and a draw(Graphics g) method would draw it in the BufferedImage. Such an object
could be created where you now create a JLabel.
Editing would be a bit more difficult. You could either inspect a click on the panel, or you
could have a pop-up list of textobjects.

Poeh, I guess there are a lot of ways...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!