0

I am trying to move a function from the main class to a sub-class in a small game I'm making. I have copied and pasted the function from the main class into a sub-class and for some reason I am getting this error:

no suitable method found for drawImage(java.awt.Image,int,int,pony) method java.awt.Graphics2D.drawImage(java.awt.BufferedImage, java.awt.BufferedImageOp, int, int) is not applicable; (actual argument java.awt.Image cannot be converted to java.awt.BufferedImage by method invocation conversion); 

This is the function:

public void explode(Graphics g) { Graphics2D g2d = (Graphics2D) g; int i=0; float angle = 0.03f; float PI = 3.14159f; int x2,y2; int r=40; while(r<200) { while (angle < 2 * PI) { x2 = (int)this.x + (int) (Math.cos(angle)*r); y2 = (int)this.y + (int) (Math.sin(angle)*r); g2d.drawImage(sparkles[i].getImage(), x2, y2,this); angle+=0.1; i+=1; } i=0; angle=0.03f; r+=5; } } 

Here is the function call in the main class:

public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; for(int i=0;i<curPonies;i++) { if(ponies[i].colliding()) { ponies[i].explode(g); } } } 

If I move the explode function into the main class it works perfectly, but if I try to call it from another class I get the error above. If anyone can tell me why this happens, I would very much appreciate the help.

Thank you very much!

2
  • 2
    java.awt.Graphics2D.drawImage(java.awt.BufferedImage, java.awt.BufferedImageOp, int, int) Where it asks foe a BufferedImageOp parameter (second parameter), you put an int. For the last pareter, needing an int, you put put an object (this). It also states: actual argument java.awt.Image cannot be converted to java.awt.BufferedImage by method invocation conversion, meaning the Image your putting in cant be converted to BufferedImage. If you know it is a BufferedImage, cast it. (BufferedImage) getImage() Commented May 8, 2014 at 16:25
  • For better help sooner, post an MCVE (Minimal Complete and Verifiable Example). Commented May 9, 2014 at 9:19

1 Answer 1

1

Call in this way

g2d.drawImage(sparkles[i].getImage(), x2, y2, null); // if observer is not known 

Look at Graphics#drawImage(Image img,int x,int y,ImageObserver observer).

java.awt.Graphics2D is sub-class of java.awt.Graphics.


In your case last argument this must implement ImageObserver.

g2d.drawImage(sparkles[i].getImage(), x2, y2,this); //last argument this must be an ImageObserver 
Sign up to request clarification or add additional context in comments.

4 Comments

Hes using Graphics2d, which requires different parameters. You should point out that using null as the final parameter value really makes a difference, so he understands why he's comming across this error. This error is typically easier to find when not using Graphics2d, since theres not as much confusion with overloaded methods
java.awt.Graphics2D is sub-class of java.awt.Graphics.
Look at last line of my post. Now we don't know what is this here?
The last line of your post is good; as long as he understands what was causing the problem, so he doesnt end up back here with the same one :P

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.