Revampled MainFrame layout to improve appearance/usability when the number of tiles is very large or small.

Fixed instantiation where a new MouseListener was used to each tile - now only one is used for all Tiles.
Fixed issue where the MouseListener was being added to each tile twice.
This commit is contained in:
Woody Folsom
2012-04-14 12:02:17 -04:00
parent 9619f9a96d
commit 74b8eb4622
7 changed files with 154 additions and 222 deletions

View File

@@ -1,10 +1,11 @@
package view;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.BorderLayout;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import model.Referee;
@@ -12,90 +13,85 @@ public class MainFrame extends JFrame {
private static final long serialVersionUID = 1L;
private final Referee referee;
public static void main(String[] args) {
new MainFrame();
MainFrame mainFrame = new MainFrame();
mainFrame.playGame();
}
private final Referee game;
public MainFrame() {
super("Project 4");
game = new Referee();
super("CS8803 Project 4");
referee = new Referee();
init();
// Set up window.
setDefaultCloseOperation(MainFrame.EXIT_ON_CLOSE);
setResizable(false);
pack();
setLocationRelativeTo(null);
setVisible(true);
run();
pack();
}
private void init() {
// Create objects.
ScorePanel sp = new ScorePanel(game);
TileSelectionPanel tp = new TileSelectionPanel(game.getHumanPlayer());
BoardPanel bp = new BoardPanel(game,tp);
MessagePanel mp = new MessagePanel(game);
ScorePanel sp = new ScorePanel(referee);
TileSelectionPanel tp = new TileSelectionPanel(referee.getHumanPlayer());
// Add stuff to panel.
GridBagLayout layout = new GridBagLayout();
GridBagConstraints con;
con = new GridBagConstraints();
con.anchor = GridBagConstraints.WEST;
con.fill = GridBagConstraints.BOTH;
con.gridheight = 1;
con.gridwidth = 1;
con.gridx = 0;
con.gridy = 0;
con.weightx = 1;
layout.setConstraints(sp, con);
con = new GridBagConstraints();
con.anchor = GridBagConstraints.EAST;
con.fill = GridBagConstraints.BOTH;
con.gridheight = 1;
con.gridwidth = 1;
con.gridx = 1;
con.gridy = 0;
con.weightx = 1;
layout.setConstraints(mp, con);
con = new GridBagConstraints();
con.anchor = GridBagConstraints.NORTH;
con.fill = GridBagConstraints.BOTH;
con.gridwidth = 2;
con.gridx = 0;
con.gridy = 1;
layout.setConstraints(bp, con);
con = new GridBagConstraints();
con.anchor = GridBagConstraints.NORTH;
con.fill = GridBagConstraints.BOTH;
con.gridwidth = 2;
con.gridx = 0;
con.gridy = 2;
con.insets = new Insets(10, 0, 10, 0);
layout.setConstraints(tp, con);
setLayout(layout);
add(sp);
add(mp);
add(bp);
add(tp);
BoardPanel bp = new BoardPanel(referee,tp);
referee.setBoardPanel(bp);
MessagePanel mp = new MessagePanel(referee);
referee.setMessagePanel(mp);
//The outer wrapper allows the game board to be resized vertically.
JPanel vWrapper = new JPanel();
vWrapper.setLayout(new BoxLayout(vWrapper,BoxLayout.Y_AXIS));
//The inner wrappers allow each sub-panel to be independently resized horizontally.
JPanel hWrapperTop = new JPanel();
hWrapperTop.setLayout(new BoxLayout(hWrapperTop,BoxLayout.X_AXIS));
JPanel hWrapperMid = new JPanel();
hWrapperMid.setLayout(new BoxLayout(hWrapperMid,BoxLayout.X_AXIS));
JPanel hWrapperBottom = new JPanel();
hWrapperBottom.setLayout(new BorderLayout());
//Top horizontal-box wrapper contains message panel, score panel
hWrapperTop.add(Box.createHorizontalGlue());
hWrapperTop.add(mp);
hWrapperTop.add(sp);
hWrapperTop.add(Box.createHorizontalGlue());
//Mid horizontal box wrapper contains board panel
hWrapperMid.add(Box.createHorizontalGlue());
JPanel bpContainer = new JPanel();
bpContainer.add(bp);
hWrapperMid.add(bpContainer);
hWrapperMid.add(Box.createHorizontalGlue());
//Bottom horizontal box wrapper contains tile panel
hWrapperBottom.add(tp,BorderLayout.NORTH);
//the outter wrapper contains the 3 inner wrappers: top, mid, bottom
vWrapper.add(hWrapperTop);
vWrapper.add(hWrapperMid);
vWrapper.add(hWrapperBottom);
//The main JFrame contains a the outer (vertical) wrapper, in the center of a BorderLayout.
setLayout(new BorderLayout());
add(vWrapper,BorderLayout.CENTER);
//To ensure correct size, pre-populate the score and message panels with text.
sp.updateScore();
mp.updateMessage("Loading new game...");
pack();
//Finally, update the border to show the initially selected tile color.
tp.updateBorders();
}
private void run() {
while (!game.isOver()) {
game.doSomething();
}
private void playGame() {
new Thread(referee).start();
}
}