Results 1 to 16 of 16

Thread: Calling methods

  1. #1
    Alkor is offline Member
    Join Date
    Feb 2012
    Posts
    39
    Rep Power
    0

    Default Calling methods

    So, im making a very basic game, that calls methods from another class and can easly be placed on the main class, the problem is when ever i make a object calling from another class nothing is display...

    Java Code:
    package pixel.advantures;
    
    import javax.swing.JFrame;
    
    public class PixelAdvantures extends JFrame{
        //Frame Set Up
        public PixelAdvantures(){
            setTitle("PixelAdvantures");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(800, 600);
            setResizable(false);
            setVisible(true);
        }
        public static void main(String[] args) {
            //Adding the Frame
            PixelAdvantures pixelAdvantures = new PixelAdvantures();
            //Adding a test
            Methods Graphics1 = new Methods();
        }
    }
    Class for holding methods
    Java Code:
    package pixel.advantures;
    
    import java.awt.Graphics;
    
    
    public class Methods extends PixelAdvantures{
    
        public void Graphics1(Graphics g){
            g.drawString("hi",100,100);
            
        }
    }

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: Calling methods

    when you overwrite the paint() method inside the class Methods, instead of Graphics1, the string "hi" will be displayed.

  3. #3
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Calling methods

    In your case, in main method, you are doing nothing else but instantiate PixelAdvantures pixelAdvanture, and after that, again instantiate Method Graphics1. Since Methods extends PixelAdvantures you will end up with two frames of PixelAdvantures. I think that was not your intention.
    Perhaps your intention was to call method Graphics1 from Methods class and to get hi text displayed on frame?
    If this is the case you must rewrite your Methods class...

  4. #4
    Alkor is offline Member
    Join Date
    Feb 2012
    Posts
    39
    Rep Power
    0

    Default Re: Calling methods

    Yes that was my intention, I have changed it...but still cant seem to get it to work

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default Re: Calling methods

    nothing is display..
    Where do you have any code that will be called that will display anything?

    BTW The coding standing for variable names and method names is that they start with a lower case letter.

  6. #6
    Alkor is offline Member
    Join Date
    Feb 2012
    Posts
    39
    Rep Power
    0

    Default Re: Calling methods

    Display meaning the text

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default Re: Calling methods

    Yes, display the text in the String in the Graphics1 method: hi.

  8. #8
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Calling methods

    Can you post your changed Methods class?

  9. #9
    Alkor is offline Member
    Join Date
    Feb 2012
    Posts
    39
    Rep Power
    0

    Default Re: Calling methods

    Okay, sorry I'm confused...
    I'm calling the text in this line
    Methods Graphics1 = new Methods();
    Graphics1 is the object.

  10. #10
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Calling methods

    I guess Norm will explain that you are not. Never the less, I still would like to see your modified class
    Last edited by milovan; 02-24-2012 at 11:56 PM.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default Re: Calling methods

    I'm calling the text in this line
    Methods Graphics1 = new Methods();
    That line creates a Methods object and assigns its address to the variable: Graphics1

    In the Methods class you have a method named: Graphics1.
    That method is not related to the the variable.


    To call the method you would code it this way:
    Graphics1.Graphics1(<args here>);

    BTW The coding standing for variable names and method names is that they start with a lower case letter.

    The above statement should be:

    graphics1.graphics1(<args here>);

  12. #12
    Alkor is offline Member
    Join Date
    Feb 2012
    Posts
    39
    Rep Power
    0

    Default Re: Calling methods

    Ok so i fixed everything but still no working here is my new code

    package pixel.advantures;

    import javax.swing.JFrame;

    public class PixelAdvantures extends JFrame{
    //Frame
    public PixelAdvantures(){
    setTitle("PixelAdvantures");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(800, 600);
    setResizable(false);
    setVisible(true);
    }

    public static void main(String[] args) {
    //Adding Objects
    PixelAdvantures pixelAdvantures = new PixelAdvantures();
    Objects g1 = new Objects();
    g1.g1();
    }

    }

    package pixel.advantures;

    import java.awt.Graphics;

    public class Objects{


    public void g1(Graphics g){
    g.drawString("hey", 25, 25);

    }

    }

  13. #13
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Calling methods

    You declared method g1() to take an argument of type Graphics, but you did not provide one - g1.g1(?).
    So you need to provide that argument.
    And in your case it is an graphics object of component you want to paint on, i.e. PixelAdvantures (because it extends JFrame).
    Try to figure out how to get Graphics of JFrame (yours PixelAdvantures) and then you can pass it to your g1() method...

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default Re: Calling methods

    What errors do you get when you try to compile your program?
    Please copy and paste them there.

  15. #15
    Alkor is offline Member
    Join Date
    Feb 2012
    Posts
    39
    Rep Power
    0

    Default Re: Calling methods

    Rewrote the whole program its working fine now, not sure what I did different.
    Thanks for your help.

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default Re: Calling methods

    Quote Originally Posted by milovan View Post
    Try to figure out how to get Graphics of JFrame (yours PixelAdvantures) and then you can pass it to your g1() method...
    Don't. Search for and go through the tutorial on Performing Custom Painting.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. calling methods in other classes
    By elite 98 in forum New To Java
    Replies: 5
    Last Post: 07-09-2011, 03:33 AM
  2. calling methods on an applet from cli
    By matpen in forum Java Applets
    Replies: 3
    Last Post: 02-20-2011, 07:52 AM
  3. Calling for methods
    By soccer_kid_6 in forum New To Java
    Replies: 3
    Last Post: 02-27-2010, 09:12 PM
  4. Calling Methods
    By bluegreen7hi in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 06:22 AM
  5. need help calling methods
    By lowpro in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 09:53 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
  •