Solved

Image Buffers...

Posted on 2001-07-16
2
402 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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
micro services spring boot application error 3 189
Java Timer (static) 9 43
java jdbc batch example 8 32
Which non-HTML GUI front end to use with Java? 3 22
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

770 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