Results 1 to 6 of 6
Thread: Problem calling a method
- 12-11-2009, 06:19 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
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); } }
- 12-11-2009, 06:58 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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
- 12-11-2009, 07:53 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
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()?
- 12-11-2009, 08:18 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 12-11-2009, 08:32 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
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? :/
-
The Opoly2 class has one constructor that takes an int as its parameter. Are you calling the constructor with the correct type of parameter?
Similar Threads
-
Calling a method
By mnki23 in forum New To JavaReplies: 21Last Post: 11-06-2009, 09:10 PM -
calling method from main method
By bob_bee in forum New To JavaReplies: 4Last Post: 10-02-2009, 05:30 PM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM -
method calling?
By frejon26 in forum New To JavaReplies: 4Last Post: 01-25-2008, 03:38 AM -
Help with Calling a method
By Albert in forum New To JavaReplies: 3Last Post: 07-10-2007, 03:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks