Results 1 to 5 of 5
- 10-26-2014, 02:27 AM #1
Member
- Join Date
- Oct 2014
- Posts
- 4
- Rep Power
- 0
calling an object method from another object
Hi everyone, first post here :)
I've been trying to learn Java for the last 36 hours or so (after applying for a HTML/CSS job saying "Java knowledge preferred"), and decided to experiment a bit making a graphical tic-tac-toe game. I eventually managed to get that done and it's working. Working code below:
[Java] tic tac toe 1 - Pastebin.com (Sorry for using pastebin rather than the code function here, this site timed out on me twice trying to submit it).
So, it works to an extent, however, the way I am capturing which cell is selected seems very sloppy, and would not work if the cells weren't squares or rectangles. So I made a copy of the project and restructured it adding the mouse event to the cells, but now I can't get JComponent to repaint. New code below:
tic tac toe 2 - Pastebin.com
Apologies for this post being so long (if you include the code in the links :P), but I've been stuck on this for a couple of hours now and I'm out of ideas :(.
Curiously, clicking triggers the action for all 9 cells, but I presume it's because I haven't bounded them making it think I've clicked all 9 simultaneously.
What I've tried:
Make the Cell class extend the game class and call this.repaint()- causes stack overflow.
Calling Game.GameState() within the cell clicking event and making that function static - compiler doesn't like calling repaint() inside a static function.
Making another class to make a clone of the Game object and then refresh- was never going to work....
A couple of other things I can't remember...Last edited by manudude03; 10-26-2014 at 02:31 AM. Reason: fixed second link
- 10-26-2014, 02:32 AM #2
Re: calling an object method from another object
If you have code you have questions about, please post it here with your questions.
If you don't understand my response, don't ignore it, ask a question.
- 10-26-2014, 03:03 AM #3
Member
- Join Date
- Oct 2014
- Posts
- 4
- Rep Power
- 0
Re: calling an object method from another object
Let's try a shorter version (the full code is in the second link):
I have 2 classes in question, game.java (which is extended from another class which handles the gui) and cell.java. The game class contains an array of 9 cells.
In the game class, I have this method:
Java Code:public void GameState() { // standard iterator int i=0; // WinCombos is a 2D array showing the 8 possible ways of winning int[][] WinCombos= {{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,3,8},{0,4,8},{2,4,6}}; // iterate through the above array for (i=0; i<8; i++) { // these 2 if statements should be combined, but this way leads to shorter lines if (Game.cells[WinCombos[i][0]].getContent()==Game.cells[WinCombos[i][1]].getContent()) { if (Game.cells[WinCombos[i][0]].getContent()==Game.cells[WinCombos[i][2]].getContent()) { // if it's got in here, then one of the winning combos has happened, check who won if (Game.cells[WinCombos[i][0]].getContent()=="X") { Game.status="Player X wins"; Game.state=1; this.repaint(); return; } if (Game.cells[WinCombos[i][0]].getContent()=="O") { Game.status="Player O wins"; Game.state=2; this.repaint(); return; } } } } // if no-one has won, is the game still ongoing? for (i=0; i<9; i++) { if (Game.cells[i].getContent()=="") { Game.status="Player "+Game.move+"'s turn"; Game.state=0; // game still in progress this.repaint(); return; } } Game.status="Draw"; Game.state=3; this.repaint(); return; // game drawn }
Java Code:public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub double cx, cy; // the event x and y cx=e.getX(); cy=e.getY(); // check if click was inside perimeter of cell if (cx>this.getxlower()&&cx<this.getxupper()&&cy>this.getylower()&&cy<this.getyupper()) { System.out.println("Cell "+this.id+" clicked"); if (this.getContent()!="") { this.setContent(Game.move); Game.cells[this.id].setContent(Game.move); if (Game.move=="X") { Game.move="O"; } else { Game.move="X"; } // call to Game.GameState() needed here } } }
- 10-26-2014, 03:14 AM #4
Re: calling an object method from another object
Have a reference to the Game class at the class level so the code in the listener method can call its methods:
Java Code:refToGameClass.gameState(); // call method using reference to Game object
Last edited by Norm; 10-26-2014 at 03:17 AM.
If you don't understand my response, don't ignore it, ask a question.
- 10-26-2014, 03:53 AM #5
Member
- Join Date
- Oct 2014
- Posts
- 4
- Rep Power
- 0
Re: calling an object method from another object
My class names do start with a capital, I was just lazy when I was typing out the second post. The GameState method must have slipped through the cracks so to speak, but anyway it took a little tweaking to fix the gui, but your solution worked nicely, thanks :)
Similar Threads
-
Link objects and calling a variable object's method
By Wice in forum New To JavaReplies: 2Last Post: 06-14-2014, 05:53 PM -
[Paremeters] in a method(Object expA, Object expB){}
By Unsub in forum New To JavaReplies: 2Last Post: 01-29-2010, 03:01 AM -
Calling Object.method
By Fedor in forum New To JavaReplies: 1Last Post: 04-11-2009, 02:44 PM -
Calling a method for all instances of an object
By rattle in forum New To JavaReplies: 4Last Post: 04-30-2008, 03:10 PM -
Object from String (calling method dynamically)
By Java Tip in forum Java TipReplies: 0Last Post: 02-16-2008, 10:22 PM
Bookmarks