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 / May 2006

Tip: Looking for answers? Try searching our database.

can't create BufferedImage

Thread view: 
Jay Tanner - 17 May 2006 12:45 GMT
I am trying to create a BifferedImage and Graphics2D associated with a
JPanel.  I understand that the JPanel must be displayable else the
createImage method may return null.  However in my stub program below I
always get null returned from createGraphics.  I obviously am missing
something but I can't figure out what.  If the program below is run
without a runline parameter it does not attempt to create a
BufferedImage and it runs OK.  If it is run with a runline parameter it
throws an exception because bh is null.  Why?

Jay

public class Test extends javax.swing.JFrame {
   
   private static String[] opt = null;

   public Test() {
      init();
   }

   void init() {
      javax.swing.JPanel jContentPane = new javax.swing.JPanel();
      setContentPane(jContentPane);
      setSize(450, 350);
      javax.swing.JPanel graphPanel = new javax.swing.JPanel();
      java.awt.Dimension s = new java.awt.Dimension(400, 300);
      graphPanel.setPreferredSize(s);
      getContentPane().add(graphPanel, null);
      graphPanel.addNotify();
      s = graphPanel.getPreferredSize();
      System.err.println("Is JPanel=" +
         (graphPanel instanceof javax.swing.JPanel) +
         " " + s.width + "x" + s.height + " Displayable=" +
         graphPanel.isDisplayable() + " Headless= " +
         java.awt.GraphicsEnvironment.isHeadless());
      if(opt!=null && opt.length>0) {
         java.awt.image.BufferedImage bh =
            (java.awt.image.BufferedImage)graphPanel.createImage(
            s.width, s.height);
         java.awt.Graphics2D bufh = bh.createGraphics();
      }
   }

   public static final void main(String[] argv) {
      opt = argv;
      Test test = new Test();
      test.setVisible(true);
   }
}
Andrey Kuznetsov - 17 May 2006 16:27 GMT
>I am trying to create a BifferedImage and Graphics2D associated with a
>JPanel.  I understand that the JPanel must be displayable else the
[quoted text clipped - 4 lines]
>runs OK.  If it is run with a runline parameter it throws an exception
>because bh is null.  Why?

because it is not yet shown.
DoubleBufferPanel.java is a good example.

Andrey

Signature

http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

Jay Tanner - 18 May 2006 03:15 GMT
Thanks.  I think the javadoc for Component.createImage() is misleading
on this point.  It says:

The return value may be null if the component is not displayable.  This
will always happen if GraphicsEnvironment.isHeadless() returns true.
See Also:
    isDisplayable(),GraphicsEnvironment.isHeadless()

So it seemed to me that if isDiplayable() was true and isHeadless()
was false, as they were shown to be in my example, then createImage()
should not have returned null.

JT

>>I am trying to create a BifferedImage and Graphics2D associated with a
>>JPanel.  I understand that the JPanel must be displayable else the
[quoted text clipped - 9 lines]
>
> Andrey
Oliver Wong - 17 May 2006 16:59 GMT
>I am trying to create a BifferedImage and Graphics2D associated with a
>JPanel.  I understand that the JPanel must be displayable else the
[quoted text clipped - 4 lines]
>runs OK.  If it is run with a runline parameter it throws an exception
>because bh is null.  Why?

[most of the code snipped]

> public class Test extends javax.swing.JFrame {
>
[quoted text clipped - 13 lines]
>    }
> }

   Your frame isn't visible when you call createImage.

   - Oliver
Knute Johnson - 17 May 2006 17:33 GMT
> I am trying to create a BifferedImage and Graphics2D associated with a
> JPanel.  I understand that the JPanel must be displayable else the
[quoted text clipped - 44 lines]
>    }
> }

Just for my curiosity, why are you going through all of those
machinations to create a BufferedImage?  What is it you are really
trying to do?

Signature

Knute Johnson
email s/nospam/knute/

Fred Kleinschmidt - 17 May 2006 17:39 GMT
>I am trying to create a BifferedImage and Graphics2D associated with a
>JPanel.  I understand that the JPanel must be displayable else the
[quoted text clipped - 44 lines]
>    }
> }

Try using a MediaTracker to wait for the image to be created,
and call this method after you make the panel visible:

public void getImage() {
      MediaTracker tracker = new MediaTracker (comp);
     java.awt.image.BufferedImage bh =
            (java.awt.image.BufferedImage)graphPanel.createImage(
            s.width, s.height);
     java.awt.Graphics2D bufh = bh.createGraphics();

      tracker.addImage (bh, 1);
      try {
          tracker.waitForAll();
      } catch (InterruptedException e) {}
}
Thomas Weidenfeller - 18 May 2006 10:23 GMT
[...]

>> bh is null.

[...]

>>          java.awt.image.BufferedImage bh =
>>             (java.awt.image.BufferedImage)graphPanel.createImage(
>>             s.width, s.height);

> Try using a MediaTracker to wait for the image to be created,
> and call this method after you make the panel visible:
[quoted text clipped - 4 lines]
>              (java.awt.image.BufferedImage)graphPanel.createImage(
>              s.width, s.height);
[...]

>        tracker.addImage (bh, 1);

Tracking on a null reference will not magically change that null
reference to an image instance.

/Thomas
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/



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.