Results 1 to 6 of 6
  1. #1
    ToeJam is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default Problem calling a method

    So basically I am trying to use an Opoly2 method (reset()) in OpolyPanel, but I cannot figure out how to implement it. I bolded the line of confusion. Honestly I just have no idea what to do with it any help is appreciated, thanks.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    
    public class GopolyDriver{
      public static void main(String[] args){
        DisplayWindow d = new DisplayWindow();
        JMenuBar menuBar = new JMenuBar();
        d.setJMenuBar(menuBar);
        OpolyPanel p = new OpolyPanel(menuBar);
        d.addPanel(p);
        d.showFrame();
      }
    }
    Java Code:
    public class Opoly2{
     private int boardSize;
     private int pos = 0;
    private int steps = 0;
    private int reward = 10;
    
     public Opoly2(int boardSize){
       this.boardSize=boardSize;
     }
     
     public int getPos(){return pos;}
     public int getSteps(){return steps;}
     public int getReward(){return reward;}
     public int getBoardSize(){return boardSize;}
     
     public void reset(){
       // resets game for another start
       pos = 0;
       steps = 0;
       reward = 10;
     }
    
     public int spin(){
       // gives a random spin 1-5
       int p=(1+(int)(Math.random()*5));
       return p;
     }
    
     public void move(int p){
       // rules for advancing board piece
       steps++;
       if  (pos + p > boardSize) return;
       if(pos+p == boardSize-1){
         reward=reward/3;
         pos=0;
       }
       else if (pos + p == boardSize)
       {pos = pos + p; return;}
       else{
         pos = pos + p;
         if(pos%4==0) reward = 2*reward;
       }
     }
     
     public boolean isGameOver(){
       // game over when piece equals board size exactly
       return (pos==boardSize);
     }
    
     public void spinAndMove(){
       // does a spin, then move
       int p=spin();
       move(p);
     }
    
     public void drawBoard(){
       // a crude drawing of the board; shows current reward
       for(int j=0; j<boardSize; j++){
         if(j!=pos){
           System.out.print("*");}
         else{
           System.out.print("o");}
       }
       if (pos == boardSize)System.out.print("o");
       System.out.println(" " + reward);
     }
    
     public void playGame(){
       // top level play of game; play continues until game over (duh!)
       System.out.println("Board Size: " + boardSize);
         drawBoard();
       while (isGameOver()==false){
        spinAndMove();
        drawBoard();
       }
       System.out.println("game over");
       System.out.println("rounds of play: " + steps);
       System.out.println("final reward: " + reward);
              }
    }
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class OpolyPanel extends JPanel implements ActionListener{
      JMenuBar b;
      JMenu menu = new JMenu("Actions");
      JMenuItem newGame = new JMenuItem("NewGame");
      JMenuItem quit = new JMenuItem("Quit");
      
      public OpolyPanel(JMenuBar bar){
        this.b = bar;
        b.add(menu);
        menu.add(newGame);
        menu.add(quit);
        newGame.addActionListener(this);
        quit.addActionListener(this);
        setBackground(Color.white);
      }
      public void actionPerformed(ActionEvent e){
     [SIZE="4"][B]   if (e.getSource() == newGame) this.reset();[/B][/SIZE]
        if (e.getSource() == quit) System.exit(0);
      }
    }
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    public  class DisplayWindow extends JFrame{
    
      private Container c;
    
      public DisplayWindow(){
        super("Display");
        c = this.getContentPane();
      }
    
      public void addPanel(JPanel p){
        p.setPreferredSize(new Dimension(500,400));
        c.add(p);
      }
    
      public void showFrame(){
        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Methods are called 'on' objects, i.e. you have to call the reset() method 'on' an Opoly2 object. Your panel needs a reference to such an object to call that method on. Your panel should 'have' such a reference which it doesn't.

    kind regards,

    Jos

  3. #3
    ToeJam is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default

    What do you mean by reference? Like create an object in Opoly2 and use thatObject.reset()? or do I have to do something like set an object in Opoly2 = to one in Panel. In this case being Opoly2.reset()?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ToeJam View Post
    What do you mean by reference? Like create an object in Opoly2 and use thatObject.reset()? or do I have to do something like set an object in Opoly2 = to one in Panel. In this case being Opoly2.reset()?
    I mean your panel needs to have an Opology2 object somewhere to call the reset() method on.

    kind regards,

    Jos

  5. #5
    ToeJam is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default

    ah yes that makes sense thanks for clearing that up. I would love to test this, but I cannot seem to create the Opoly2 board = new Opoly2(); correctly.

    cannot find symbol
    symbol : constructor Opoly2()
    location: class Opoly2

    From

    public Opoly2(int boardSize){
    this.boardSize=boardSize;
    }

    Any idea on that one? :/

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Calling a method
    By mnki23 in forum New To Java
    Replies: 21
    Last Post: 11-06-2009, 09:10 PM
  2. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 05:30 PM
  3. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  4. method calling?
    By frejon26 in forum New To Java
    Replies: 4
    Last Post: 01-25-2008, 03:38 AM
  5. Help with Calling a method
    By Albert in forum New To Java
    Replies: 3
    Last Post: 07-10-2007, 03:27 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
  •