Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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
  • 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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 …
Suggested Courses
Course of the Month12 days, 5 hours left to enroll

564 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