Reputation: 201
Im creating a tetris game, and want the user to be able to choose their board size, from 3 different sizes. I have a Board backend that handles the right, left, down, drop movements, and I have a BoardGUI that creates the shapes and animates it. So when updating the size I need to both update the backend and my GUI for the board. My size keeps getting initialized back to 10 x 20 which is the default size.
here is my BoadGUI snippet (I deleted all unnecessary code, i may have missed something, sorry)
public class BoardGUI extends JPanel implements Observer {
private static final int BLOCK_WIDTH = 20;
private Board myBoard;
private MainGUI myFrame;
private int myWidth;
private int myHeight;
public BoardGUI(MainGUI theFrame, int theWidth, int theHeight){
myWidth = theWidth;
myHeight = theHeight;
this.myBoard = new Board(myWidth, myHeight);
this.myFrame = theFrame;
setupComponents();
myBoard.newGame();
}
public Board getBoard() {
return myBoard;
}
public void setPanelSize(int theWidth, int theHeight){ // gets correct values
this.myWidth = theWidth;
this.myHeight = theHeight;
}
private void setupComponents() {
setLayout(new BorderLayout());
this.setPreferredSize(new Dimension(myWidth * BLOCK_WIDTH, myHeight * BLOCK_WIDTH)); // setting this panel size
System.out.println(myWidth + " " + myHeight); // DOES NOT PRINT CORRECT SIZE
myFrame.pack();
}
here is my MainGUI where I set the size
private int myWidth;
private int myHeight;
public MainGUI() {
super();
myWidth = 10;
myHeight = 20;
}
/**
*
*/
private static final long serialVersionUID = -5056050661369885282L;
private JMenuBar myMenuBar;
public void start() {
setTitle("Tetris");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myEastPanel = new JPanel(new BorderLayout());
NextPiece nextPanel = new NextPiece();
myBoardGUI = new BoardGUI(this, myWidth, myHeight);
myMainBoard = myBoardGUI.getBoard();
setResizable(false);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public JMenuBar createMenuBar() {
myMenuBar = new JMenuBar();
myWindow = new JMenu("Window");
final JFrame frame = this;
myMenuBar.add(myWindow);
setWindowSize();
myMenuBar.setVisible(true);
return myMenuBar;
}
private void setWindowSize() {
ButtonGroup group = new ButtonGroup();
JCheckBox def = new JCheckBox("Default");
JCheckBox size1 = new JCheckBox("150 x 250");
myWindow.add(def);
myWindow.addSeparator();
myWindow.add(size1);
group.add(def);
group.add(size1);
JFrame frame = this;
def.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
myWidth = 10;
myHeight = 20;
myBoardGUI.setPanelSize(myWidth,myHeight); //setting here
}
});
size1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// myBoardGUI.setPanelSize(15, 25);
myWidth = 15;
myHeight = 25;
myBoardGUI.setPanelSize(myWidth,myHeight); // Setting here
}
});
}
Upvotes: 0
Views: 49
Reputation: 324147
You save the variables, but you never reset the preferred size.
this.setPreferredSize(new Dimension(myWidth * BLOCK_WIDTH, myHeight * BLOCK_WIDTH));
The problem is that you should NOT be using the setPreferredSize() method. This makes the size fixed NOT dynamic.
Instead you SHOULD be overriding the getPreferredSize()
method of your panel. Something like:
@Override
public Dimension getPreferredSize()
{
return new Dimension(myWidth * BLOCK_WIDTH, myHeight * BLOCK_WIDTH));
}
This will allow the size to be dynamically calculated whenever Swing needs to do so.
Now in your panel.setSize()
method after you update the variables you need to invoke:
revalidate();
repaint();
So the layout manager can be invoked and the new size of the panel will be considered.
Upvotes: 3