1: package siege.gui.views;
2:
3: import java.awt.Color;
4: import java.awt.Dimension;
5: import java.awt.Font;
6: import java.awt.Graphics;
7: import java.util.Observable;
8: import java.util.Observer;
9: import javax.swing.BorderFactory;
10: import javax.swing.Box;
11: import javax.swing.BoxLayout;
12: import javax.swing.JLabel;
13: import javax.swing.JPanel;
14: import siege.core.Game;
15: import siege.core.Playboard;
16: import siege.core.Player;
17: import siege.util.Log;
18:
19:
20: 21: 22: 23: 24:
25: public class StatusView extends JPanel implements Observer
26: {
27: 28: 29:
30: public static Dimension size = new Dimension(200, 150);
31: public static Dimension fieldSize = new Dimension(180, 20);
32:
33: 34: 35:
36: public StatusLabel playerInfo = new StatusLabel();
37: public StatusLabel capturedPointsTitle = new StatusLabel();
38: public StatusLabel capturedPointsMe = new StatusLabel();
39: public StatusLabel capturedPointsOpponent = new StatusLabel();
40: public StatusLabel status = new StatusLabel();
41:
42: 43: 44:
45: public StatusView()
46: {
47: super();
48:
49: 50: setPreferredSize(size);
51: setBackground(Color.white);
52: setBorder( BorderFactory.createCompoundBorder(
53: BorderFactory.createEmptyBorder(5,5,5,5),
54: null
55: ));
56: setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
57:
58: 59: playerInfo.setFont(new Font("SansSerif", Font.BOLD, 16));
60:
61: capturedPointsTitle.setFont(new Font("SansSerif", Font.BOLD, 13));
62:
63: 64: status.setFont(new Font("SansSerif", Font.BOLD, 16));
65: status.setBackground(new Color(176,211,225));
66: status.setBorder(BorderFactory.createCompoundBorder(
67: BorderFactory.createLineBorder(new Color(28,40,44)),
68: BorderFactory.createEmptyBorder(5,5,5,5)
69: ));
70:
71: 72: add(playerInfo);
73: add(Box.createRigidArea(new Dimension(0, 5)));
74: add(capturedPointsTitle);
75: add(Box.createRigidArea(new Dimension(0, 5)));
76: add(capturedPointsMe);
77: add(Box.createRigidArea(new Dimension(0, 5)));
78: add(capturedPointsOpponent);
79: add(Box.createVerticalGlue());
80: add(status);
81: }
82:
83: public void initialize()
84: {
85: playerInfo.setText("");
86: capturedPointsMe.setText("");
87: capturedPointsOpponent.setText("");
88: capturedPointsTitle.setText("");
89: status.setText("");
90:
91: 92: Game game = Game.getInstance();
93: game.getPlayboard().addObserver(this);
94: game.getLocalPlayer().addObserver(this);
95: game.addObserver(this);
96: updateGame(game);
97: updatePlayer(game.getLocalPlayer());
98:
99: Log.message(this, "initialize");
100: }
101:
102: public void update(Observable o, Object arg) {
103: Log.message(this, "update");
104: if(o instanceof Player) {
105: updatePlayer((Player)o);
106: } else if(o instanceof Playboard) {
107: updateScore();
108: } else if(o instanceof Game) {
109: updateGame((Game)o);
110: }
111: }
112:
113: private void updatePlayer(Player player)
114: {
115: String playerName = player.getName();
116: if(playerName != null) {
117: 118: playerInfo.setText(playerName + " (" + player.getAddress() + ")");
119: }
120: }
121:
122: private void updateScore()
123: {
124: Log.message(this, "update score");
125: String playerName = Game.getInstance().getLocalPlayer().getName();
126: if(playerName != null) {
127: 128: capturedPointsTitle.setText("Captured points");
129: capturedPointsMe.setText("me: " + Game.getInstance().getLocalPlayer().getScore().getCapturedPointCount());
130: capturedPointsOpponent.setText("opponent: " + Game.getInstance().getRemotePlayer().getScore().getCapturedPointCount());
131: }
132: }
133:
134: private void updateGame(Game game)
135: {
136: String statusText = "";
137: switch(game.getStatus()) {
138: case INITIALIZED:
139: statusText = "Welcome"; break;
140: case CONNECTING:
141: statusText = "Connecting"; break;
142: case CONNECTED:
143: statusText = "Connected"; break;
144: case START:
145: updateScore();
146: break;
147: case PLAYING:
148: statusText = "You are playing!"; break;
149: case WAITING:
150: statusText = "Wait for next turn"; break;
151: case END:
152: if(game.getWinner() == null) {
153: statusText = "Standoff";
154: } else if(game.getWinner().equals(game.getLocalPlayer())) {
155: statusText = "Winner";
156: } else {
157: statusText = "Loser";
158: }
159: break;
160: }
161: status.setText(statusText);
162: }
163:
164: @Override
165: public void paint(Graphics g)
166: {
167: super.paint(g);
168: g.setColor(new Color(131,176,178));
169: g.drawLine(getWidth()-1, 0, getWidth()-1, getHeight()-1);
170: }
171:
172: 173: 174:
175: public class StatusLabel extends JLabel
176: {
177: public StatusLabel() {
178: setOpaque(true);
179: setBackground(Color.white);
180: }
181: }
182: }
183: