can't create BufferedImage  
Author Message
Jay Tanner





PostPosted: 2006-5-17 19:45:00 Top

java-programmer, can't create BufferedImage 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





PostPosted: 2006-5-17 23:28:00 Top

java-programmer >> can't create BufferedImage >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?

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

Andrey

--
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


 
Oliver Wong





PostPosted: 2006-5-17 23:59:00 Top

java-programmer >> can't create BufferedImage
"Jay Tanner" <email***@***.com> wrote in message
news:41Eag.9714$UY6.2956@trnddc08...
>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?

[most of the code snipped]
>
> public class Test extends javax.swing.JFrame {
>
> public Test() {
> init();
> }
>
> void init() {
> java.awt.image.BufferedImage bh =
> (java.awt.image.BufferedImage)graphPanel.createImage(
> s.width, s.height);
> }
>
> public static final void main(String[] argv) {
> Test test = new Test();
> test.setVisible(true);
> }
> }
>

Your frame isn't visible when you call createImage.

- Oliver

 
 
Knute Johnson





PostPosted: 2006-5-18 0:34:00 Top

java-programmer >> can't create BufferedImage Jay Tanner wrote:
> 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);
> }
> }
>

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?

--

Knute Johnson
email s/nospam/knute/
 
 
Fred Kleinschmidt





PostPosted: 2006-5-18 0:40:00 Top

java-programmer >> can't create BufferedImage
"Jay Tanner" <email***@***.com> wrote in message
news:41Eag.9714$UY6.2956@trnddc08...
>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);
> }
> }
>

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) {}
}


 
 
Jay Tanner





PostPosted: 2006-5-18 10:16:00 Top

java-programmer >> can't create BufferedImage 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

Andrey Kuznetsov wrote:
>>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?
>
>
> because it is not yet shown.
> DoubleBufferPanel.java is a good example.
>
> Andrey
>

 
 
Thomas Weidenfeller





PostPosted: 2006-5-18 17:24:00 Top

java-programmer >> can't create BufferedImage Fred Kleinschmidt wrote:

> "Jay Tanner" <email***@***.com> wrote in message
> news:41Eag.9714$UY6.2956@trnddc08...
[...]

>> 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:
>
> public void getImage() {
> MediaTracker tracker = new MediaTracker (comp);
> java.awt.image.BufferedImage bh =
> (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
--
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/