drawing image and imageObserver

Posted on 2003-02-20
Medium Priority
Last Modified: 2011-09-20
I'm working on an application where the paintComponent method leaves it to other methods (in other classes) to do the actual drawing. Something like:

public void paintComponent(Graphics g) {
  Graphics2D g2 = (Graphics2D) g;
  otherClass1.draw(g2, someArea);
etc etc

In one of those classes I want to draw an image:

public void draw(Graphics2D g2, Rectangle2D myArea){
  g2.drawImage(myImage, myArea.getX(), myArea.getY(), null);

The thing is a null value is not accepted as ImageObserver. Nor did sending a (this) reference from the JPanel with the paint method help. It's a small image and I don't care about Tracker.

The reason i don't want to paint in different Panels is I want to print the contents easily.

Question by:verzus
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
  • 2
LVL 86

Expert Comment

ID: 7988114
>>The thing is a null value is not accepted as ImageObserver

Why not?
LVL 14

Expert Comment

by:Tommy Braas
ID: 7988151
Does "One of those classes" inherit from Component (way up the hierarchy)?

If not, they will not implement the ImageObserver interface.
If that is the case you can either;
supply a reference to the class with the overridden paintComponent method


just create a new Component in your draw method and supply that as the ImageObserver reference


implement the ImageObserver interface in the class with the draw method.

Author Comment

ID: 7989334
I am not getting any wizer...

The image drawing class does not inherit anything. I tried sending a reference from the class with paintComponent, the result was the same, only:

cannot resolve symbol :
method drawImage(java.awt.Image,double,double,<nulltype>)

instead of

method drawImage(java.awt.Image,double,double,myClassName)

even tried implementing ImageObservera and passing the this-reference.

ImageObserver imageObserver;
g2.drawImage(legendMapImage, myArea.getX(), myArea.getY(), imageObserver);

gave me:
cannot resolve...  method
drawImage (java.awt.Image,double,double,java.awt.image.ImageObserver)

What do you mean by creating a new component? Any component? A new JLabel an pass that as reference?
LVL 14

Accepted Solution

Tommy Braas earned 200 total points
ID: 7989902
>> The image drawing class does not inherit anything.

Actually, all classes in Java inherit from java.lang.Object, that's immaterial though.

From the describtion, it would appear that the following might solve your problem (it will at least compile);

public class OtherClass

   Component dummyImageObserver = new Canvas();

   public void draw(Graphics2D g2, Rectangle2D myArea)
      g2.drawImage(myImage, (int)myArea.getX(), (int)myArea.getY(), dummyImageObserver);

It should be noted that the cast to integer from the double values retrieved from the Rectangle2D object, might need to be translated to the current coordinate system.

I hope this was a bit clearer!

Author Comment

ID: 7989991
Thanks, it wasn't the imageObserver, it was the double values...

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
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.
Suggested Courses
Course of the Month9 days, 1 hour left to enroll

765 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