Reputation: 175
I am doing a project that consist on creating a Battleship Game. I have the following classes:
Battleship
(main class)Player
,Game
,Board
,Visualisation
(GUI class where I have a gridLayout
of JButtons
) where the user press the JButton
where want to insert the ship.First of all a create a new Game
with some parameters such as the size of the board. Then inside the class Game
I do a new Visualisation
. Inside this class I have done the actionListener
and the actionPerformed
.
My question is how I can pass the information, for example, of which JButton
I have pressed (to insert the ship in that cell of the gridLayout
) to the class Game
? This is what I have:
Class Game
private Player _user;
private Player _computer;
And then I want to check the board of the _user
if that positions are available to insert the ship. _user.MethodOfClassPlayer();
Class Player
private int id;
private String name;
private Board _boardPlayer
Class Board
private int size;
private int[][] _board = null ;
Function actionPerformed
public void actionPerformed(ActionEvent e) {
for ( int i = 0; i < tUsuariCPU.length; i++ ){
for ( int j = 0; j < tUsuariCPU[i].length; j++ ){
if ( e.getSource() == tUsuariCPU[i][j] ){
buttonPressedUser(i,j);
JButton temp = (JButton) e.getSource() ;
temp.setBackground(java.awt.Color.ORANGE);
}
I want to pass the information of the JButton pressed to Game class to know if for that Player that position is available or not for putting the ship. If it is available so I will paint the JButton I hope you understand me.
Upvotes: 0
Views: 2820
Reputation: 791
Taking account of the other answer, you may want to use Observers
and Observable
. An Observable is a class that can be observed by one or several Observers. And an Observer can observe several Observables.
Example:
public class Visualisation extends JFrame {
private Integer num;
private A myInnerClass;
public Visualisation(Observer o) {
num = 8;
myInnerClass = new A();
myInnerClass.setObserver(o);
}
public onButtonPressed(Event e) {
myInnerClass.notifyMyObservers();
}
public class A extends Observable {
public A() {
}
public void notifyMyObservers() {
this.setChanged();
this.notifyObservers(num); // the parameter can be any object
}
}
}
public class B implements Observer {
public B() {
}
public void update(Observable observed, Object arg) {
if (observed instance of A) {
if (arg instance of Integer) {
// ...
}
}
}
}
And here is how you hook them up within an example main function
public static main(String[] args) {
B observer = new B();
Visualisation v = new Visualisation(observer);
}
Upvotes: 3
Reputation: 368
you can use a dialog class like this:
public class Visualisation extends JDialog {
private Board board;
public Game(Frame owner, boolean modal) {
super(owner, modal);
// add a Listener to your button to dispose the dialog after you save your data in board
JButton yourButton = new JButton();
yourButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
board = ....;
dispose();
}
});
}
public Board show(){
setVisible(true);
return Board ;
}
}
and to call it in the game class use:
Visualisation visualisationDialog = new Visualisation(null, true);
Board board = visualisationDialog .show();
//do whatever you want with the board from here now
Upvotes: 0