Ok, this is driving me nuts! I am programing the board game scrabble for a summer class. One of the requirements is to have multiple JFrames; one for each player. So what I decided to do is create one class named board and display it in two or more JFrames (one for each player). So there is one object board in 2 different JFrames. Now, when I add a tile to the board, it will show up on both frames.
The problem is, when I cover up the JFrames only one board will remain updated.
The board is a class that extends JPanel.
I hope this is clear enough. If not, I will explain it further.
6 Replies - 1560 Views - Last Post: 05 July 2008 - 04:52 AM
#1
PLEASE HELP! -- Using same object in multiple Jframes
Posted 04 July 2008 - 05:47 PM
Replies To: PLEASE HELP! -- Using same object in multiple Jframes
#2
Re: PLEASE HELP! -- Using same object in multiple Jframes
Posted 04 July 2008 - 05:51 PM
lwstory, on 4 Jul, 2008 - 05:47 PM, said:
Ok, this is driving me nuts! I am programing the board game scrabble for a summer class. One of the requirements is to have multiple JFrames; one for each player. So what I decided to do is create one class named board and display it in two or more JFrames (one for each player). So there is one object board in 2 different JFrames. Now, when I add a tile to the board, it will show up on both frames.
The problem is, when I cover up the JFrames only one board will remain updated.
The board is a class that extends JPanel.
I hope this is clear enough. If not, I will explain it further.
The problem is, when I cover up the JFrames only one board will remain updated.
The board is a class that extends JPanel.
I hope this is clear enough. If not, I will explain it further.
It would help if we could see the code
#3
Re: PLEASE HELP! -- Using same object in multiple Jframes
Posted 04 July 2008 - 05:59 PM
package editedTeamBCode;
import edu.buffalo.cse.TeamB.source.*;
public abstract class AbstractGame{
private int _numberPlaying;
public void setBoard(){
for(int i = 0; i <= _numberPlaying; i++){
gui.playerFrame pf = new gui.playerFrame(getBoard(),getPlayers().get(i));
pf.setLocation(new java.awt.Point(10*i,10*i));
} //This is where I make the multiple frames
}
public void setPlayers(){
_numberPlaying = getPlayers().size() - 1;
TurnManager manager = new TurnManager(getGame(),getBoard());
for(Player p:getPlayers()){
manager.setCurrent(p);
manager.rackReload();
}
}
public abstract Game getGame();
public abstract Board getBoard();
public abstract java.util.ArrayList<Player> getPlayers();
public abstract TurnManager getTurnManager();
}
package gui;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import listeners.TimerUpdateListener;
public class Board extends JPanel implements interfaces.IHaveSquares, IBoardConstants {
/**
*
*/
private static final long serialVersionUID = 1L;
private java.util.HashMap<Point,Square> _squareLocations = new java.util.HashMap<Point,Square>();
private edu.buffalo.cse.TeamB.source.Board _board;
private javax.swing.Timer updater = new javax.swing.Timer(10,new listeners.TimerUpdateListener(this));
private java.util.ArrayList<edu.buffalo.cse.TeamB.source.Tile> _tiles = new java.util.ArrayList<edu.buffalo.cse.TeamB.source.Tile>();
/**
* This sets up the graphical display of the board.
*/
public void setBoard(edu.buffalo.cse.TeamB.source.Board board) {
_board = board;
setLayout(null);
setSize(new Dimension((SQUARE_SIZE+SQUARE_PADDING*2)*20 + BOARD_PADDING * 2,(SQUARE_SIZE+SQUARE_PADDING*2)*20 + BOARD_PADDING * 2));
Square square;
for(int row = 1; row < 7; row++){
for(int column = 1; column < 15; column++){
square = new Square(this);
square.setLocation(row*(SQUARE_SIZE + SQUARE_PADDING + 2) + BOARD_PADDING, column*(SQUARE_SIZE + SQUARE_PADDING + 2) + BOARD_PADDING);
_squareLocations.put(new Point(row,column),square);
add(square);
}
}
for(int row = 7; row < 15; row++){
for(int column = 1; column < 21; column++){
square = new Square(this);
square.setLocation(row*(SQUARE_SIZE + SQUARE_PADDING + 2) + BOARD_PADDING, column*(SQUARE_SIZE + SQUARE_PADDING + 2) + BOARD_PADDING);
_squareLocations.put(new Point(row,column),square);
add(square);
}
}
for(int row = 15; row < 21; row++){
for(int column = 7; column < 21; column++){
square = new Square(this);
square.setLocation(row*(SQUARE_SIZE + SQUARE_PADDING + 2) + BOARD_PADDING, column*(SQUARE_SIZE + SQUARE_PADDING + 2) + BOARD_PADDING);
_squareLocations.put(new Point(row,column),square);
add(square);
}
}
_squareLocations.get(new Point(_board.homeSquareCol(),_board.homeSquareRow())).setHomeSquare();
this.repaint();
//timer.start();
}
/**
* @see interfaces.IHaveSquares#add(tileAndRack.Tile, gui.Square)
* @param tile
* @param square
*/
public void add(Tile tile, Point location) {
_squareLocations.get(location).add(tile);
}
/**
* @see interfaces.IHaveSquares#remove(gui.Square)
* @param square
*/
public Tile remove(Point location) {
return _squareLocations.get(location).remove();
}
public void add(Tile tile, Square square) {
}
public Tile remove(Square square) {
// TODO Auto-generated method stub
return null;
}
public void clearBoard(){
java.util.Iterator<Square> iter = _squareLocations.values().iterator();
while(iter.hasNext()){
Square square = iter.next();
square.remove();
}
if(_board.homeSquareCol() != 0){
_squareLocations.get(new Point(_board.homeSquareCol(),_board.homeSquareRow())).setHomeSquare();
}
_tiles.clear();
}
public void updateMe() {
java.util.ArrayList<edu.buffalo.cse.TeamB.source.Tile> tiles = _board._placements;
if(tiles != null && !tiles.isEmpty() && tiles.size() != _tiles.size()){
clearBoard();
try{
for(edu.buffalo.cse.TeamB.source.Tile t: tiles){
_tiles.add(t);
if(!t.isNew()) t.setNewPlacement();
}
}
catch(Exception ex){
ex.printStackTrace();
System.out.println("Board 115");
System.exit(1);
}
try{
for(edu.buffalo.cse.TeamB.source.Tile tile: tiles){
add(tile, new Point(tile.getCol(),tile.getRow()));
}
}
catch(Exception ex){
ex.printStackTrace();
System.out.println("Board 125");
System.exit(1);
}
this.repaint();
}
}
}
In other words, if you have two JFrames and add the same JPanel to them. Then you add an item to the JPanel, I want both of the frames to stay updated. In my case, the latest JFrame displays the information of the JPanel and the older frame will lose the graphics if you pass another window over it.
This post has been edited by lwstory: 04 July 2008 - 06:23 PM
#4
Re: PLEASE HELP! -- Using same object in multiple Jframes
Posted 04 July 2008 - 09:06 PM
lwstory, on 4 Jul, 2008 - 05:59 PM, said:
In other words, if you have two JFrames and add the same JPanel to them. Then you add an item to the JPanel, I want both of the frames to stay updated. In my case, the latest JFrame displays the information of the JPanel and the older frame will lose the graphics if you pass another window over it.
A JComponent (JPanel, JLabel, JButton,....) cannot be in 2 containers at the same time. It will end up into the last one you added it in. So you will need 2 copies (clone) of them
#5
Re: PLEASE HELP! -- Using same object in multiple Jframes
Posted 05 July 2008 - 04:11 AM
pbl, on 4 Jul, 2008 - 09:06 PM, said:
lwstory, on 4 Jul, 2008 - 05:59 PM, said:
In other words, if you have two JFrames and add the same JPanel to them. Then you add an item to the JPanel, I want both of the frames to stay updated. In my case, the latest JFrame displays the information of the JPanel and the older frame will lose the graphics if you pass another window over it.
A JComponent (JPanel, JLabel, JButton,....) cannot be in 2 containers at the same time. It will end up into the last one you added it in. So you will need 2 copies (clone) of them
Could someone give me an example of how to clone an object that extends JPanel or of how I could do it in my board class.
I know to add the Cloneable interface and then make the clone method, but I dont know what to return. Thanks in advanced.
#6
Re: PLEASE HELP! -- Using same object in multiple Jframes
Posted 05 July 2008 - 04:36 AM
lwstory, on 5 Jul, 2008 - 04:11 AM, said:
pbl, on 4 Jul, 2008 - 09:06 PM, said:
lwstory, on 4 Jul, 2008 - 05:59 PM, said:
In other words, if you have two JFrames and add the same JPanel to them. Then you add an item to the JPanel, I want both of the frames to stay updated. In my case, the latest JFrame displays the information of the JPanel and the older frame will lose the graphics if you pass another window over it.
A JComponent (JPanel, JLabel, JButton,....) cannot be in 2 containers at the same time. It will end up into the last one you added it in. So you will need 2 copies (clone) of them
Could someone give me an example of how to clone an object that extends JPanel or of how I could do it in my board class.
I know to add the Cloneable interface and then make the clone method, but I dont know what to return. Thanks in advanced.
Well to give a simple example:
public class Foo implements Cloneable {
private int ID;
private String bar;
public Foo() {
}
public int getID() {
return this.ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getBar() {
return this.bar;
}
public void setBar(String bar) {
this.bar = bar;
}
public Object clone() throws CloneNotSupportedException {
Foo retVal = new Foo();
retVal.setID(getID());
retVal.setBar(getBar());
return retVal;
}
}
Basically, you create a new object of the type you're cloning, and set all properties to be the same as the original. The API suggests you keep to the following conventions, though it depends on your definition of a copy:
x.clone() != x && x.clone().getClass() == x.getClass() && x.clone().equals(x)
#7
Re: PLEASE HELP! -- Using same object in multiple Jframes
Posted 05 July 2008 - 04:52 AM
JeroenFM, on 5 Jul, 2008 - 04:36 AM, said:
lwstory, on 5 Jul, 2008 - 04:11 AM, said:
pbl, on 4 Jul, 2008 - 09:06 PM, said:
lwstory, on 4 Jul, 2008 - 05:59 PM, said:
In other words, if you have two JFrames and add the same JPanel to them. Then you add an item to the JPanel, I want both of the frames to stay updated. In my case, the latest JFrame displays the information of the JPanel and the older frame will lose the graphics if you pass another window over it.
A JComponent (JPanel, JLabel, JButton,....) cannot be in 2 containers at the same time. It will end up into the last one you added it in. So you will need 2 copies (clone) of them
Could someone give me an example of how to clone an object that extends JPanel or of how I could do it in my board class.
I know to add the Cloneable interface and then make the clone method, but I dont know what to return. Thanks in advanced.
Well to give a simple example:
public class Foo implements Cloneable {
private int ID;
private String bar;
public Foo() {
}
public int getID() {
return this.ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getBar() {
return this.bar;
}
public void setBar(String bar) {
this.bar = bar;
}
public Object clone() throws CloneNotSupportedException {
Foo retVal = new Foo();
retVal.setID(getID());
retVal.setBar(getBar());
return retVal;
}
}
Basically, you create a new object of the type you're cloning, and set all properties to be the same as the original. The API suggests you keep to the following conventions, though it depends on your definition of a copy:
x.clone() != x && x.clone().getClass() == x.getClass() && x.clone().equals(x)
Thank you!
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|