DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Hybrid View

  1. #1
    Join Date
    Jul 2006

    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?

  2. #2
    Join Date
    Dec 2004
    San Bernardino County, California
    "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.

  3. #3
    Join Date
    Jul 2006
    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

  4. #4
    Join Date
    Aug 2003
    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:
    g.drawImage(obstacle, x, y, Urn.IMAGE);
    Or something like that.

    Hope this helps.

Similar Threads

  1. Objects disposed incorrectly
    By Osiris43 in forum .NET
    Replies: 1
    Last Post: 08-04-2006, 12:15 PM
  2. Using a class within a class to draw
    By pentacle1 in forum Java
    Replies: 0
    Last Post: 01-21-2006, 11:54 PM
  3. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 07:10 PM
  4. Replies: 5
    Last Post: 10-17-2002, 01:58 PM
  5. Script for scrolling
    By Mark in forum Web
    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
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.