Solved

Image Buffers...

Posted on 2001-07-16
2
406 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

617 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