Link to home
Start Free TrialLog in
Avatar of verzus
verzus

asked on

drawing image and imageObserver

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) {
  super.paintComponent(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.

cheers
/vs
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>The thing is a null value is not accepted as ImageObserver

Why not?
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

OR

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

OR

implement the ImageObserver interface in the class with the draw method.
Avatar of verzus
verzus

ASKER

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.

and:
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?
ASKER CERTIFIED SOLUTION
Avatar of Tommy Braas
Tommy Braas
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of verzus

ASKER

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