This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes bufferedImage and Graphics2D Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "bufferedImage and Graphics2D" Watch "bufferedImage and Graphics2D" New topic
Author

bufferedImage and Graphics2D

JC Denton
Greenhorn

Joined: Nov 21, 2004
Posts: 6
Im writing a class which intends to turn a bufferedimage objects into a graphics2d object so as to make use of the draw() method. Once that's done the method will return the graphics object to the class subclassing a JFrame. This class will position and paint the graphics object onto the JPanel. At least thats the intention but:

I'm stuck painting the graphics object onto the form. Any ideas? casting it back to a bufferedImage and using paintImage() in the paint() method of JPanel looks like an option but im unsure on how to use this method and graphics efficiency is imperative something which [prob] won't benefit of continual casting..

using getGraphics(), casting and passing the Graphics2d fails to fruit when the class calls Draw() on it. It is not a solution I'm interested in anyhow..

thanx all


<b>I know nothing</b> - Manuel Fawlty Towers
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I think you have a basic misunderstanding about the relationship between BufferedImage and Graphics. You can't cast one to another. A BufferedImage is an Image that holds its pixel data in memory. A Graphics object is a virtual surface that you can draw on -- it may or may not be backed by a BufferedImage.

One way to set pixels in on a BufferedImage is to use a graphics object. It sounds like you are already doing this:

Once you are done drawing, there is no need to hang onto the graphics object. The usual thing to do is call dispose, but that may be a noop for BufferedImages.

Now how do you show the results of that rendering? You typically do not subclass JFrame, because JFrame is the frame, not the painting, right? Instead, you might use a JLabel to display your BufferedImage:

JLabel label = new JLabel(new ImageIcon(bi));

Or if you are doing tricky things, you may subclass JComponent and override its paintComponent method:


There is no emoticon for what I am feeling!
JC Denton
Greenhorn

Joined: Nov 21, 2004
Posts: 6
Thank you for your help. It is very much appreciated in circumstances most despairing.

I'll confess. My hours are consumed attempting to create tetris, hence the drawing on the JFrame. I fear losing KeyEvent functionality when adding a label component to the form so I wish to restrain from doing so.

The KeyEvent listener is added to the JFrame subclass.

I am using another class to proxy the graphics rendering, allowing me to create multiple instances, easing the process of implementing [local] multiplayer.

As JPanel is a subclass of the Component class, I can override the paintComponent method there. [?]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42030
    
  64
Moved to the Swing/AWT/GUI forum, as this is not an applet-specific question.


Ping & DNS - my free Android networking tools app
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by JC Denton:
As JPanel is a subclass of the Component class, I can override the paintComponent method there?


Sure. When it comes to defining a custom component, there not much of a difference between subclassing JComponent and subclassing JPanel.
[ May 08, 2006: Message edited by: Jeff Albertson ]
JC Denton
Greenhorn

Joined: Nov 21, 2004
Posts: 6
I doubt this applies to graphics, rather than the language structure.

I have a class extending the JPanel with a member class of custom type Tetris.

Tetris has a method getImage() which returns a bufferedImage to be drawn onto the screen.

In this class it looks like this:



The above code works as desired.

For the 2 player version I'm subclassing this particular class and adding another Tetris member dubbed tetris2.

The same method in the subclass:


forgive the extra BI which is drawn on using g. The interface would otherwise start flickering.

The code compiles but when it is run there's a NPE

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

This is utterly discombobulating. Using JDK1.4.1 the code would work if tetris2 was a member of the parent class and not the subclass.

Is it something to do with the subclass not being able to draw on the panel of the superclass? The constructor of the subclass calls the parent constructor.

Thanks
 
GeeCON Prague 2014
 
subject: bufferedImage and Graphics2D