Removed use of Refreshable and replaced with several methods which update the GUI using SwingUtilities.invokeLater().
While not as clean as using ChangeListener, it completely decouples the Model and View aspects, and also avoids the need to manually paint() a hierarchy of components (a task best handled by Swing/AWT).
This commit is contained in:
@@ -3,26 +3,19 @@ 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() {
|
||||
@@ -44,16 +37,10 @@ public class MainFrame extends JFrame {
|
||||
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.getPlayer());
|
||||
|
||||
// Add refreshables.
|
||||
elements.add(sp);
|
||||
elements.add(bp);
|
||||
elements.add(mp);
|
||||
elements.add(tp);
|
||||
|
||||
BoardPanel bp = new BoardPanel(game,tp);
|
||||
MessagePanel mp = new MessagePanel(game);
|
||||
|
||||
// Add stuff to panel.
|
||||
GridBagLayout layout = new GridBagLayout();
|
||||
GridBagConstraints con;
|
||||
@@ -100,35 +87,14 @@ public class MainFrame extends JFrame {
|
||||
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();
|
||||
}
|
||||
|
||||
pack();
|
||||
tp.updateBorders();
|
||||
}
|
||||
|
||||
private void run() {
|
||||
while (!game.isOver()) {
|
||||
game.doSomething();
|
||||
|
||||
refresh();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user