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!
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()