This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
 
Rancher
Posts: 990
23
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 26914
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Bookmark Topic Watch Topic
  • New Topic