Solved

Image Buffers...

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Bot application - advice 3 81
Java 8 to Java 6 8 44
jsp error 6 72
Java Eclipse Loop 3 50
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…
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 …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

710 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