Getting an image object from a JPanel that is not visible

  • Follow


Hi Newsgroup!

I need to create an image from a JPanel, that has not been displayed
on the screen. I.e. I recieve a JPanel and want to pass it as image to
a library without showing the JPanel to the user.
My idea is to do something like this:

public void doSomething(JPanel originalPanel) {
	testImage(getImage(originalPanel));
}


public Image getImage(JPanel aPanel) {
	Image image = aPanel.createImage(aPanel.getWidth(),
aPanel.getHeight());
	return image;
}

public void testImage(Image aImage) {
	JFrame frame = new JFrame("testImage");
	JLabel label = new JLabel(new ImageIcon(aImage));
	frame.getContentPane().add(label);
	frame.pack();
	frame.setVisible(true);
}


But this gives a:
java.lang.NullPointerException
	at javax.swing.ImageIcon.<init>(Unknown Source)

because the Image returned by getImage() is null.
Has anybody some suggestions how to get an image object from a panel
that hasn't been displayed?

Thx

Norbert
0
Reply norbert 11/26/2004 11:54:58 AM

Norbert wrote:
> because the Image returned by getImage() is null.
> Has anybody some suggestions how to get an image object from a panel
> that hasn't been displayed?

The behavior is in line with the documented behavior. So in short, you 
can't. Create a separate BufferedImage and use that one.

/Thomas

-- 
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
0
Reply Thomas 11/26/2004 12:20:00 PM


Thomas Weidenfeller <nobody@ericsson.invalid> wrote in message news:<co76qh$2sr$1@newstree.wise.edt.ericsson.se>...
> Norbert wrote:
> > because the Image returned by getImage() is null.
> > Has anybody some suggestions how to get an image object from a panel
> > that hasn't been displayed?
> 
> The behavior is in line with the documented behavior. So in short, you 
> can't. Create a separate BufferedImage and use that one.
> 
> /Thomas
Hi,

I think you are talking of something like this:

public Image getImage(JPanel aPanel) {
    BufferedImage image = new BufferedImage(500, 500,
        BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = image.createGraphics();
    aPanel.paint(g2d);
    return image;
}

But this gives me only a black image. Any further suggestions or do I
make a misstake?

Norbert
0
Reply norbert 11/29/2004 6:03:16 AM

Norbert wrote:
> Thomas Weidenfeller <nobody@ericsson.invalid> wrote in message news:<co76qh$2sr$1@newstree.wise.edt.ericsson.se>...
> 
>>Norbert wrote:
>>
>>>because the Image returned by getImage() is null.
>>>Has anybody some suggestions how to get an image object from a panel
>>>that hasn't been displayed?
>>
>>The behavior is in line with the documented behavior. So in short, you 
>>can't. Create a separate BufferedImage and use that one.
>>
>>/Thomas
> 
> Hi,
> 
> I think you are talking of something like this:
> 
> public Image getImage(JPanel aPanel) {
>     BufferedImage image = new BufferedImage(500, 500,
>         BufferedImage.TYPE_INT_RGB);
>     Graphics2D g2d = image.createGraphics();
>     aPanel.paint(g2d);
>     return image;
> }
> 
> But this gives me only a black image. Any further suggestions or do I
> make a misstake?
> 

Probably because the Panel hasn't been validated. Since the panel hasn't 
been added to anything, you will have to set its bounds manually and 
also call validate on it so that it will perform a layout of its child 
components. (In case the panel has child components, you need to 
experiment a bit to see if the validate call really works - There are a 
lot of places where AWT code will avoid performing validations if the 
component is not visible)

BK
0
Reply Babu 11/29/2004 7:39:07 AM

3 Replies
459 Views

(page loaded in 0.087 seconds)

Similiar Articles:













7/23/2012 4:38:56 PM


Reply: