Solved

Image Buffers...

Posted on 2001-07-16
2
400 Views
Last Modified: 2008-02-01
Im trying to write a component that peoduces a nice graphics effect, and I want it to be compatable with IE 4...

The problem is making a buffered image to use, and getting it's handle.

In Java 2, I have been using:

private BufferedImage buffImage=new BufferedImage(360,370,BufferedImage.TYPE_INT_RGB);

which has been fine, but doesn't work in IE 4, so I can't use it.

So I wrote a small method thusly:

  private void createBufferedImage()
    {
    bufferedImage=createImage(sourceBitmapWidth,sourceBitmapHeight);
    buffer=bufferedImage.getGraphics();
    }

It's great but it stops every single time with a null pointer exception when the JVM gets to the getGraphics() method, DAMN!

I was reading a book, and it said put it in your paint(Graphics g) method, like so:

public void paint(Graphics g)
  {
  if(buffer==null)
    bufferedImage=createImage(sourceBitmapWidth,sourceBitmapHeight);
    buffer=bufferedImage.getGraphics();
    }

   buffer.blah blah blah............  

   g.drawImage(bufferedImage,0,0,this);
  }

This version works! But two things I dont understand...

Why does the very same createImage folowed by getGraphics commands work in the paint method, but not in my createBufferedImage method, which always throws that exception, and why do I have to create my buffer in the paint method, EVERY time checking to see if it's been produced already? I just want to create it, and refere to it FROM paint, not mess around checking if it exists in there!

If you really want, I can supply the full code...
0
Comment
Question by:Lab_Rat
2 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 100 total points
ID: 6287576
The issue, I believe, is one of timing. The createImage method depends on the Component being drawn on the screen. Since painting is completely asynchronous in Java, and since such createImage calls are frequently made early on, it is likely that the Component is not yet "shown" on the screen, in which case the createImage will fail, and so the getGraphics returns null.

Putting it in the paint method provides a reasonable place to make (a number of) tries at getting it.
0
 
LVL 1

Author Comment

by:Lab_Rat
ID: 6303730
lol!
Thanks for the input imladris! Although you didn't address the optimisation problem of having -mostly- redundant code in the paint method(an 'if' statement that only ever is true once...), you are the only person to comment!

Hence your A, and 100 points...

:o)

Thank you.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now