Win a copy of Raising Young Coders: A Parent’s Guide to Teaching Programming at Home this week in the General Computing forum!
Forums Login/signup

How to wrap a constructor in a default constructor. (Am I saying this wrong?)

+Pie Number of slices to send: Send
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:



first, I instantiate a Texture:



Then, I try to draw it using g.drawImage(); like this:



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();
+Pie Number of slices to send: Send
Pale Gray,

The problem you have is that your g.drawImage  wants player to be in image like a BufferedImage, which in my opinion is the easiest to use.  Your texture is not am Image.  If you want to make a factory or a method that returns an Image (BufferedImage is my recomendation), then you'll be able to get the drawImage to work.  

Les
+Pie Number of slices to send: Send
You're on your way to a solution, sort of. The primary issue is, you have a Texture and the other code wants an Image, and a Texture isn't an Image.

But your Texture has an Image... sort of. It needs two things fixed: first, it needs to store the scaled image in an Image variable. Not a local variable like you have there, which disappears as soon as the constructor is run, but an instance variable in the Texture class so that it's saved for future use.

Second, Texture needs a method which returns that Image to whoever needs it. The signature looks like this: public Image getImage().

Hope that helps you get moving again.

reply
reply
This thread has been viewed 288 times.
Similar Threads
drawing on the screen in response to user-initiated actions
How do I scale a shape?
2D graphic repaint() logic
JApplet overlaping png images.
Image won't appear
More...

All times above are in ranch (not your local) time.
The current ranch time is
Jun 27, 2025 07:16:10.