Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Mar 2010

    Accessing Canvas Class from Subclass


    I am making a simple game which involves moving a character around the screen. In my main class I have declared a member class which extends Canvas as follows:

     public class myCanvas extends Canvas {
     public void paint (Graphics g) 
             // Paint stuff
        }  // end paint method
      } // end member class myCanvas
    In my main class constructor I then create an instance of myCanvas as follows:

    keyCanvas = new myCanvas();

    This works fine and I can issue the command keyCanvas.repaint(); to repaint the screen.

    My question is this.. Is it possible to access my keyCanvas device from a subclass? For example I have created a subclass called food, which extends Thread. When this thread is started it generates a random number.

    When this random number is a certain value I would like to paint a food sprite to the main display for the games hero to collect! Is it possible to access my keyCanvas directly from the food class?

    Or would it be better to create a method in my food class which returns the random value to the main class and then have my main class draw the food sprite when the required random number is received?

    Thanks in advance for any help,


  2. #2
    Registered User
    Join Date
    Mar 2010

    Re: Accessing Canvas Class from Subclass

    I think I might have figured it out!

    In my food class I have defined a paint method as follows:

    public void paint(Graphics g)
        // draw food objects
    then in my main class I have defined a new food object as follows:

    private food thisfood = new food();

    then in the paint method in my main class I issue the following:


    This seems to work as required.

    Is this the correct way to go about painting subclass instances to the graphics object?

  3. #3
    Super Contributor
    Join Date
    Apr 2007

    Re: Accessing Canvas Class from Subclass

    thats not how I would implement it...
    I would just create a Canvas class (which has its own paint() method), and the only one who would access that method would be the Canvas class itself ! No subclasses or MIDlets would be able to call paint()...
    Then I might have a Food array within my Canvas, with a method to addFood() which will add a Food object (or Image) to my array...
    In my Canvas paint method I would simply paint every Food object which is in my array...

    If you try to solve it your way you will end up with maybe tens or hundres of food objects trying to call the paint method of the Canvas or trying to write into the Graphics g object...

    I'm not sure if this is a good method and to be honest I hope Graham sees this topic and reply's
    He's the gfx expert

  4. #4
    Nokia Developer Champion
    Join Date
    Feb 2009
    Noida, India

    Re: Accessing Canvas Class from Subclass

    You can ref to this article on FN Wiki - http://wiki.forum.nokia.com/index.ph...d_applications


Similar Threads

  1. Replies: 7
    Last Post: 2010-04-09, 08:49
  2. How to display menu screen with the help of canvas class
    By dineshdreamworld in forum Mobile Java Media (Graphics & Sounds)
    Replies: 2
    Last Post: 2010-02-08, 13:24
  3. Replies: 2
    Last Post: 2009-03-18, 08:44
  4. Replies: 0
    Last Post: 2005-02-03, 09:44

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts