Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / November 2004

Tip: Looking for answers? Try searching our database.

Getting an image object from a JPanel that is not visible

Thread view: 
Norbert - 26 Nov 2004 11:54 GMT
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
Thomas Weidenfeller - 26 Nov 2004 12:20 GMT
> 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

Signature

ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq

Norbert - 29 Nov 2004 06:03 GMT
> > because the Image returned by getImage() is null.
> > Has anybody some suggestions how to get an image object from a panel
[quoted text clipped - 4 lines]
>
> /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
Babu Kalakrishnan - 29 Nov 2004 07:39 GMT
>>>because the Image returned by getImage() is null.
>>>Has anybody some suggestions how to get an image object from a panel
[quoted text clipped - 19 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.