Results 1 to 12 of 12
  1. #1
    nhmllr's Avatar
    nhmllr is offline Member
    Join Date
    Aug 2011
    Posts
    51
    Rep Power
    0

    Default Passing variables from one class to another

    I'm making a tetris game. Most of the hard work is done, in that the pieces fall, and the player can move them side to side, turn them, and drop them. Now I'd like to make a scoring system and other nice little bells and whistles.

    The "MainGrid" class is where the game is actually played. Now I made a new class called "MasterPanel" that makes a MainGrid and a new section called the "ScorePanel"

    The thing is, I need information to be fed from the MainGrid into the ScorePanel, even though neither one is contained inside the other. I hope that makes sense. How can I do it?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    You make use of callback methods.
    Java Code:
    class Foo {
        public static void main(String[] args) {
            new Foo().run();
        }
    
        public void print() {
            System.out.println("Hello World!");
        }
    
        public void run() {
            Bar b = new Bar(this);
            b.doStuff
        }
    }
    
    class Bar {
        Foo foo;
    
        Bar(Foo f) {
            foo = f;
        }
    
        public void doStuff() {
            foo.print();
        }
    }
    In the above code the Foo object is passed as a parameter to the Bar object when it is created. This allows to Bar object to callback to a method in the Foo object.

    In your case you would use the MasterPanel class to control things. When you want something to happen in the ScorePanel, the MainGrid class callsback to the MasterPanel class which then calls a method in the ScorePanel class, passing along the data you want.

  3. #3
    nhmllr's Avatar
    nhmllr is offline Member
    Join Date
    Aug 2011
    Posts
    51
    Rep Power
    0

    Default

    So I have to make TWO methods, one to pass it into the MasterPanel, and one to retrieve it? Huh.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    What I described above is one way and basically adheres to the MVC (model-view-controller) pattern. Alternatively you could pass a reference of the ScorePanel class to the MainGrid class so it can call the method in ScorePanel directly.

  5. #5
    nhmllr's Avatar
    nhmllr is offline Member
    Join Date
    Aug 2011
    Posts
    51
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    What I described above is one way and basically adheres to the MVC (model-view-controller) pattern. Alternatively you could pass a reference of the ScorePanel class to the MainGrid class so it can call the method in ScorePanel directly.
    What's a "reference?"

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Java Code:
    String str = "hello";
    In the above line str is not a String. It is a variable in particular a reference variable. What that means is the variable str holds a value which is a reference to the String object.

  7. #7
    nhmllr's Avatar
    nhmllr is offline Member
    Join Date
    Aug 2011
    Posts
    51
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Java Code:
    String str = "hello";
    In the above line str is not a String. It is a variable in particular a reference variable. What that means is the variable str holds a value which is a reference to the String object.
    Okay, but how do I refer to the specific object? In that, I need to constantly stream information from MainGrid to ScorePanel, so I don't want to make a new one inside of it.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Have you created a variable that references a ScorePanel object? The answer is probably yes so you have a reference to that object stored in the variable. So everytime you need to reference the ScorePanel you use the variable.

  9. #9
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    A little different take.

    Java Code:
    class Foo {
        public static void main( String[] args ) {
            Foo m = new Foo();
            m.run();
        }
    
        public void print( int data ) {
            System.out.println( "The value = " + data );
        }
    
        public void run() {
            Bar b = new Bar();
            b.doStuff();
        }
    }
    
    class Bar {
        public void doStuff() {
            Foo foo = new Foo();
            int j = 10;        
            foo.print( j );
        }
    }
    See how the doStuff method in Bar passes the int value back to Foo's print method?
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Hey, waddya know. I helped someone in this thread too. Looks like you incorrect assumptions are well incorrect.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    stchman

    Congrats, you are the first to be added to my ignore list. Don't bother replying

  12. #12
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Post deleted.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

Similar Threads

  1. Passing variables through classes?
    By EternalFacepalm in forum New To Java
    Replies: 4
    Last Post: 04-27-2011, 01:46 AM
  2. A bit of confusion with passing variables/infinite loop
    By kaizen9001 in forum New To Java
    Replies: 12
    Last Post: 03-27-2011, 10:05 PM
  3. please help with passing variables between class's
    By jasonwucinski in forum New To Java
    Replies: 4
    Last Post: 02-11-2011, 02:27 AM
  4. Passing dynamic variables between classes
    By Brekk in forum New To Java
    Replies: 1
    Last Post: 03-22-2010, 07:07 AM
  5. passing variables to another frame
    By Grom in forum New To Java
    Replies: 10
    Last Post: 11-09-2008, 06:55 PM

Posting Permissions

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