Results 1 to 5 of 5
  1. #1
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default Method in a method?

    Is it possible to have a method in a method? I have this method that draws stuff and is automatically called when the applet runs, I can't call it because I cant pass parameters to it or call it as void without getting an error. So I want to have it in a method that will be called which will then run the draw method inside it. Is this possible?

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You cannot declare a method inside another method, no. Why can you not pass the parameters to it? What type of parameters does it accept, and what are you passing to it? Do you get any specific error messages? (If so, paste them here.)

  3. #3
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    Well the lines of code that I want to call at certain times look like this:

    Java Code:
         public void paint( Graphics g ) {
          g.setColor( Color.green );
          for ( int i = 0; i < 10; ++i ) {
    	  g.drawLine( width, height, i * width / 10 , 0 );
         }
    }
    And this automaticly runs and I want to call it on cue so I could have different drawings. Any ideas?

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    I'm not sure why you'd need to call that automatically to have different drawings. You can simple use an if statement in the paint() function, like so:

    Java Code:
    public void paint(Graphics g) {
        if (someVariable == true) {
            // code to draw a red box
        } else {
            // code to draw a green box
        }
    }
    Now that's a very simple code, but you can take it as far as you want. There's really no need to invoke the method manually--unless you want to start doing offscreen painting.

  5. #5
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    Omg, I dunno why I didn't think of that earlier, thanks!. You have been so much help!

Similar Threads

  1. Turning Recursion Method into Iterative method
    By mattakuevan in forum New To Java
    Replies: 9
    Last Post: 06-15-2010, 07:46 AM
  2. Replies: 2
    Last Post: 03-26-2010, 06:12 PM
  3. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 08:20 PM
  4. Replies: 29
    Last Post: 09-25-2008, 08:55 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 09:37 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
  •