Recently, I'm making a simple engine using raw java and its default libraries. I want to make simple constructor methods to draw images using paintComponent();, but I encounter an error and don't really know how to fix it to make it work properly.
Here is the problem: I made a class called Texture, then made its constructor in the class, like this:
public Texture(String path, int scalex, int scaley) {
ImageIcon tex3 = new ImageIcon(getClass().getResource(path));
Image tex2 = tex3.getImage();
Image tex1 = tex2.getScaledInstance(scalex, scaley, java.awt.Image.SCALE_REPLICATE);
Image tex = tex1;
}
first, I instantiate a Texture:
Texture player = new Texture("res/player.png", 1, 1);
Then, I try to draw it using g.drawImage(); like this:
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(player, 100, 100, null);
}
But I encounter an error saying "The method drawImage(Image, int, int, ImageObserver) in the type Graphics is not applicable for the arguments (Texture, int, int, null)"
I really don't know how to fix this besides deleting the Texture constructor and just doing what the constructor does but in the class.
How can I make the Texture class usable for the Image class in the method g.drawImage()?
Thank you for anyone's help, I've been having trouble with this. It's probably an easy fix but mistakes make you learn!
~ Pale-Gray
What I have tried:
I've tried changing the constructor to "public Image Texture() {}" but that won't work,
I've tried casting it to an Image "Texture tex = (Image) new Texture();