How to use constructor from one class to draw image in another
Hi, I am having problems making a constructor for a image so that I can call it whenever I want from another class. The problem Im having is that I dont know what to put in g.drawImage(blah, blah, blah, THIS SECTION). I know normally you put "this" but I need to call it into another class..
public class Obstacle
public void drawUrn(Graphics g, String obstacle, int x, int y)
g.drawImage(obstacle, x, y, );
what would I need to do so I can call that in my Player.class?
"this" is a shorthand reference to the instance/object which is invoking the method. If your drawImage method needs to have an ImageObserver instance/object to act upon which is not the current instance/object, you can use the name of that ImageObserver instance/object you want the method to act on as an argument in place of "this".
So, if your Player class extends ImageObserver, and you have created an instance of Player - let's say, player1 - you would use
g.drawImage( obstacle, x, y, player1);
HOWEVER, you might want to directly invoke the player1 instance's drawImage method ...
Last edited by nspils; 08-22-2006 at 02:18 PM.
well, right now, player is the only other class. Its an applet right now. I just wanted a way to add an obstacle whenever I wanted but I wanted to make it a seperate class.. what Im trying to do is in Player, i want to be able to type like.. obstacle1.drawUrn(urn, 10, 20); and have it draw an urn at the coordinates provided.. but since the constructor is not in player, i am unsure of how to make that work
I'm not sure exactly what you're looking for, but you could just make a subclass of Obstacle called Urn and then give it a static final member which is an image. Then when you want to draw it, you would just say:
Or something like that.
g.drawImage(obstacle, x, y, Urn.IMAGE);
Hope this helps.
By Osiris43 in forum .NET
Last Post: 08-04-2006, 01:15 PM
By pentacle1 in forum Java
Last Post: 01-22-2006, 12:54 AM
Last Post: 01-15-2006, 08:10 PM
Last Post: 10-17-2002, 02:58 PM
Last Post: 08-30-2001, 12:45 PM
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL