-
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..
Obstacle 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 01: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:
Code:
g.drawImage(obstacle, x, y, Urn.IMAGE);
Or something like that.
Hope this helps.
~evlich
Similar Threads
-
By Osiris43 in forum .NET
Replies: 1
Last Post: 08-04-2006, 12:15 PM
-
By pentacle1 in forum Java
Replies: 0
Last Post: 01-21-2006, 11:54 PM
-
Replies: 5
Last Post: 01-15-2006, 07:10 PM
-
Replies: 5
Last Post: 10-17-2002, 01:58 PM
-
Replies: 3
Last Post: 08-30-2001, 11:45 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
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
|
Bookmarks