aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes help with actionPerformed method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "help with actionPerformed method." Watch "help with actionPerformed method." New topic
Author

help with actionPerformed method.

Justin Fenety
Greenhorn

Joined: Aug 11, 2008
Posts: 3
Hi, I am doing a assigment for a programming class, which involved setting up a client-server archtecture for a tic-tac-toe game. The problem I am having is implementing the GUI on top of this. It is based on a mvc design pattern.
The problem I am having is accessing the button's from the controller class which has a instace of the view. I need to be able to hit a button on a gui, and then send the information from the controller to the game model. But I can't access the buttons from the controller.
included GameController and GameGUI classes.
Thanks, Funkster9000

package gameUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GameGUI extends JFrame implements ActionListener
{
private JLabel title;
private JLabel userMessage;
private JLabel userPrompt;
private GameControllerSC theController;
public JButton TopLeft;
public JButton TopMiddle;
public JButton TopRight;
public JButton MiddleLeft;
public JButton MiddleMiddle;
public JButton MiddleRight;
public JButton BottomLeft;
public JButton BottomMiddle;
public JButton BottomRight;
public JButton help;
public GameGUI()
{
buildGUI();
setTitle("TICTACTOE");
pack();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void buildGUI()
{
GridLayout layout = new GridLayout(7, 1);
setLayout(layout);

JPanel row1 = new JPanel();
title = new JLabel("Let's Play Tic Tac Toe!");
JPanel row2 = new JPanel();
userPrompt = new JLabel("Connected to server");
JPanel row3 = new JPanel();
TopLeft = new JButton("?");
TopLeft.addActionListener(this);
TopMiddle = new JButton("?");
TopMiddle.addActionListener(this);
TopRight = new JButton("?");
TopRight.addActionListener(this);
JPanel row4 = new JPanel();
MiddleLeft = new JButton("?");
MiddleLeft.addActionListener(this);
MiddleMiddle = new JButton("?");
MiddleMiddle.addActionListener(this);
MiddleRight = new JButton("?");
MiddleRight.addActionListener(this);
JPanel row5 = new JPanel();
BottomLeft = new JButton("?");
BottomLeft.addActionListener(this);
BottomMiddle = new JButton("?");
BottomMiddle.addActionListener(this);
BottomRight = new JButton("?");
BottomRight.addActionListener(this);
JPanel row6 = new JPanel();
userMessage = new JLabel();
JPanel row7 = new JPanel();

FlowLayout layout2 = new FlowLayout(FlowLayout.CENTER, 5, 5);

row1.setLayout(layout2);
row1.add(title);
add(row1);

row2.setLayout(layout2);
row2.add(userPrompt);
add(row2);

row3.setLayout(layout2);
row3.add(TopLeft);
row3.add(TopMiddle);
row3.add(TopRight);
add(row3);

row4.setLayout(layout2);
row4.add(MiddleLeft);
row4.add(MiddleMiddle);
row4.add(MiddleRight);
add(row4);

row5.setLayout(layout2);
row5.add(BottomLeft);
row5.add(BottomMiddle);
row5.add(BottomRight);
add(row5);

row6.setLayout(layout2);
row6.add(userMessage);
add(row6);

row7.setLayout(layout2);
add(row7);
}
public void getController(GameControllerSC controller){
theController = controller;
}
public void displayMessage(String message){
String tempMessage = message;
userMessage.setText(message);

}
public void actionPerformed(ActionEvent e){
if(e.getSource() == MiddleLeft){

}
}
/*
* GameControllerSC.java
*
* Created on July 23, 2008, 2:06 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package gameUI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;

import javax.swing.JButton;

import gameModel.GameModel;

/**
*
*
*/
public class GameControllerSC {
private GameModel model;
private GameView view;
private boolean gameOver = false;
private int port = 4444;
private int col = 0;
private int row = 0;
private String [][]theBoard;
private String player = "X";
String hostname = "localhost";
Socket clientSocket1 = null;
Socket clientSocket2 = null;
Socket toServer = null;
BufferedReader in = null;
BufferedReader in1 = null;
BufferedReader in2 = null;
PrintWriter out1 = null;
PrintWriter out2 = null;
BufferedReader consoleIn = null;
PrintWriter out = null;
String message;

/** Creates a new instance of GameControllerSC */
public GameControllerSC(GameModel model) throws Exception {

this.model = model;

serverStart();
}
public GameControllerSC(GameView view){

this.view = view;
buildBoard();
clientStart();
}
private void displayBoard(){

for(int i = 0; i < 4; i++)
{
int j = 0;
view.displayMessage(theBoard[i][j] + theBoard[i][j+1] + theBoard[i][j+2] + theBoard[i][j+3]);


}
}
private void buildBoard(){
theBoard = new String[4][4];

for(int i = 0; i < 4; i++){


for(int j = 0; j < 4; j++){

if(i == 0 && j == 0)
theBoard[i][j] = " ";
if(i == 0 && j > 0)
theBoard[i][j] = "" + j;
if(i > 0 && j == 0)
theBoard[i][j] = "" + i;
if(i > 0 && j > 0)
theBoard[i][j] = "*";

}
}
}
private void serverStart() throws Exception{

ServerSocket serverSocket1 = new ServerSocket(port);
System.err.println("Started server on port " + port);

while(true){
Socket clientSocket1 = serverSocket1.accept();
System.out.println("Accepted connection from client1");

Socket clientSocket2 = serverSocket1.accept();
System.out.println("Accepted connection from client2");

BufferedReader in1 = new BufferedReader(new InputStreamReader(clientSocket1.getInputStream()));
PrintWriter out1 = new PrintWriter(clientSocket1.getOutputStream());

BufferedReader in2 = new BufferedReader(new InputStreamReader(clientSocket2.getInputStream()));
PrintWriter out2 = new PrintWriter(clientSocket2.getOutputStream());

sendMessage(out1, "X");
sendMessage(out2, "O");
int i = 2;

String gameMessage = null;

while(gameOver != true){

if(i%2 == 0){//player x's turn'
player = "X";
gameMessage = "" + row + col;
sendMessage(out1, gameMessage);
col = Integer.parseInt(in1.readLine());
row = Integer.parseInt(in1.readLine());

if(model.checkMove(row, col, player) == true){

gameMessage = model.checkGame();

if(gameMessage != "continue"){
gameOver = true;


if(gameMessage != player){
sendMessage(out1, "tie");


}

else{

sendMessage(out1, "win");

}

}
i = 1;
sendMessage(out1, "continue");

}

else
sendMessage(out1,"illegal");



}


else{
player = "O";
gameMessage = "" + row + col;
sendMessage(out2, gameMessage);
col = Integer.parseInt(in2.readLine());
row = Integer.parseInt(in2.readLine());

if(model.checkMove(row, col, player) == true){

gameMessage = model.checkGame();

if(gameMessage != "continue"){
gameOver = true;

if(gameMessage != player){

sendMessage(out2, "tie");//games ends in tie

}

else{

sendMessage(out2, "win");//game ends in win

}

}
i = 2;
sendMessage(out2, "continue");

}

else
sendMessage(out2,"illegal");


}
}
in1.close();
in2.close();
out1.close();
out2.close();
clientSocket1.close();
clientSocket2.close();
serverSocket1.close();
}


}
private void clientStart(){
try{
consoleIn = new BufferedReader(new InputStreamReader(System.in));
toServer = new Socket(hostname, 4444);
in = new BufferedReader(new InputStreamReader(toServer.getInputStream()));
out = new PrintWriter(toServer.getOutputStream());
view.displayMessage("Connected to server!");
view.displayMessage("Game is about to begin");
player = in.readLine();
view.displayMessage("you are player " + player);


while(gameOver!= true){
String gameMessage;
String row;
String col;
int parseRow;
int parseCol;

gameMessage = in.readLine();
view.displayMessage(gameMessage);
row = gameMessage.substring(0,1);
col = gameMessage.substring(1,2);
System.out.println(col);
System.out.println(row);
if(col.equals("0") && row.equals("0"))
displayBoard();
else{
parseRow = Integer.parseInt(row);
parseCol = Integer.parseInt(col);
if(player.equals("X"))
theBoard[parseRow][parseCol] = "O";
else
theBoard[parseRow][parseCol] = "X";
displayBoard();
}


view.displayMessage("enter column: ");
parseCol = Integer.parseInt(getInput());
gameMessage = "" + parseCol;
sendMessage(out, gameMessage);//send column to server
view.displayMessage("enter row: ");
parseRow = Integer.parseInt(getInput());
gameMessage = "" + parseRow;
sendMessage(out, gameMessage);//send row to server



gameMessage = in.readLine();

if(gameMessage.equals("illegal")){
view.displayMessage("Re-enter a valid position which is open");
}
if(gameMessage.equals("continue")){
view.displayMessage("valid move, please wait for your opponent to go");
theBoard[parseRow][parseCol] = player;
}
if(gameMessage.equals("tie")){
view.displayMessage("game is tie");
theBoard[parseRow][parseCol] = player;
gameOver = true;
}
if(gameMessage.equals("win")){
view.displayMessage("you win");
theBoard[parseRow][parseCol] = player;
gameOver = true;
}


}






}catch(IOException e){
System.err.println("error" + e.getMessage());
}finally{

try{
System.err.println("Closing connection with server");
in.close();
out.close();
toServer.close();
consoleIn.close();



}
catch(IOException e){
System.err.println("error: " + e.getMessage());
}
}
}





/**
* Method that is given a message and then sends that message to the chosen stream.
* @param PrintWriter x
* @param String s
*/
private void sendMessage(PrintWriter x, String s){
x.println(s);
x.flush();
}
/**
* Method that recieves input from the client, validates this input, and then returns it.
* @return String input
*/
private String getInput(){
String theLine = null;
int input = 0;

while(true){
try{
theLine = consoleIn.readLine();
input = Integer.parseInt(theLine);
if(input < 4 && input > 0){
return theLine;
}
else
view.displayMessage("Re-enter a valid number");

}catch (IOException ex) {
System.err.println("error: " + ex.getMessage());
}
}
}


}
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Consider posting less code, a lot less code, and using code tags to allow your posted code to be readable. I always find that in a situation like this, it helps to create a small sample program that has maybe 1 or 2 buttons in the small GUI class, and almost trivial model and controller classes just to allow me (or you) to demonstrate the concepts and problems. I recommend that you consider doing this and posting this code here. Good luck!
[ August 11, 2008: Message edited by: pete stein ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Yea, what Pete said. On top of that...

"But I can't access the buttons from the controller"

Nor would you really want to. Why do you think you need to access the buttons from the controller?


GenRocket - Experts at Building Test Data
Justin Fenety
Greenhorn

Joined: Aug 11, 2008
Posts: 3
Sorry for the code. I need to access the button's from the controller so that I can pass a message to the model telling it which position was clicked. I create this in the controller, the controller has a reference to the GUI called view. The buttons are public in the view. In my controller constrcutor I pass the ActionListener called al to the view. And then bind the view's actionListener to the button's. Will this let me trigger events from the controller.

//this is how i create the actionListener in the controller
ActionListener al = new ActionListener(){

public void actionPerformed(ActionEvent actionEvent) {
view.displayMessage("text");
if(actionEvent.getSource() == view.BottomLeft){
row = 3;
col = 1;
view.BottomLeft.setVisible(false);
try {
checkMove(row, col);
} catch (Exception e) {

//this is how I pass it in the constructor
view.ba = this.al;
//this is how i bind them in the view.
public ActionListener ba;
TopLeft.addActionListener(ba);

Thanks for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help with actionPerformed method.