-The rest of the game engine. I screwed up the commit last time.
This commit is contained in:
134
src/view/MainFrame.java
Normal file
134
src/view/MainFrame.java
Normal file
@@ -0,0 +1,134 @@
|
||||
package view;
|
||||
|
||||
import java.awt.GridBagConstraints;
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.Insets;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.security.auth.RefreshFailedException;
|
||||
import javax.security.auth.Refreshable;
|
||||
import javax.swing.JFrame;
|
||||
|
||||
import model.Referee;
|
||||
|
||||
public class MainFrame extends JFrame {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static void main(String[] args) {
|
||||
new MainFrame();
|
||||
}
|
||||
|
||||
private final ArrayList<Refreshable> elements = new ArrayList<Refreshable>();
|
||||
|
||||
private final Referee game;
|
||||
|
||||
public MainFrame() {
|
||||
game = new Referee();
|
||||
|
||||
init();
|
||||
|
||||
// Set up window.
|
||||
setDefaultCloseOperation(MainFrame.EXIT_ON_CLOSE);
|
||||
setResizable(false);
|
||||
|
||||
pack();
|
||||
setLocationRelativeTo(null);
|
||||
setVisible(true);
|
||||
|
||||
run();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
// Create objects.
|
||||
ScorePanel sp = new ScorePanel(game);
|
||||
BoardPanel bp = new BoardPanel(game);
|
||||
MessagePanel mp = new MessagePanel(game);
|
||||
TileSelectionPanel tp = new TileSelectionPanel(game);
|
||||
|
||||
// Add refreshables.
|
||||
elements.add(sp);
|
||||
elements.add(bp);
|
||||
elements.add(mp);
|
||||
elements.add(tp);
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
private void refresh() {
|
||||
boolean change = false;
|
||||
|
||||
for (Refreshable r : elements) {
|
||||
|
||||
if (!game.isCurrent() || !r.isCurrent()) {
|
||||
try {
|
||||
r.refresh();
|
||||
change = true;
|
||||
} catch (RefreshFailedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (change) {
|
||||
game.refresh();
|
||||
repaint();
|
||||
pack();
|
||||
}
|
||||
}
|
||||
|
||||
private void run() {
|
||||
while (!game.isOver()) {
|
||||
game.doSomething();
|
||||
|
||||
refresh();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user