问题
I'm using this code to try and draw an image :
g.drawImage(Tile.background, (int)spritePositionX, (int)spritePositionY, null);
Here is the my tile class for Tile.background :
public class Tile {
public static int size = 40;
public static BufferedImage terrain, background, items, characters;
public Tile(){
try{
Tile.background = ImageIO.read(new File("res/bg.png"));
Tile.terrain = ImageIO.read(new File("res/terrain.png"));
Tile.items = ImageIO.read(new File("res/items.png"));
Tile.characters = ImageIO.read(new File("characters/bg.png"));
}catch(Exception e){
System.out.println("Error loading images.");
}
}
}
It gives me this error : The method drawImage(Image, float, float, Color) in the type Graphics is not applicable for the arguments (BufferedImage, int, int, null) Thanks in advance
These are my imports in the class that the g.drawImage is in :
import javax.swing.ImageIcon;
import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
import java.awt.Rectangle;
回答1:
You're trying to use Slicks implementation of the Graphics object which doesn't have a method with the arguments you're providing. Slick is dead anyway. Either switch to LIBGDX or just use Java's 2d API.
回答2:
I suspect you are trying to use the following method:
boolean Graphics.drawImage(Image img, int x, int y, ImageObserver observer)
The last argument has to be an object that implements the ImageObserver interface, and "null" does not.
One easy way to fix this is to modify your Tile class to implement ImageObserver (by adding a method with the following signature):
boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
The method can simply return true.
Then modify your drawImage call by changing the null argument to "this" (assuming the call is in your Tile class). If it is not in your Tile class, then change the null argument to a Tile object.
来源:https://stackoverflow.com/questions/17032625/cant-draw-a-buffered-image