Menu

[r70]: / trunk / src / org / gcs / robot / RCInfo.java  Maximize  Restore  History

Download this file

159 lines (138 with data), 5.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package org.gcs.robot;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.MessageFormat;
import java.util.Observable;
import java.util.Observer;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* The RCInfo class is a view of the game's highlights.
* It observes the game model and examines the current score & level,
* displaying the highest ones obtained. It also displays random kudos
* after a new high score. As a convenience, mouse clicks in the panel
* allow the player to jump out of harm's way.
*
* @see RCModel
* @serial exclude
* @author John B. Matthews
*/
public class RCInfo extends JPanel implements MouseListener, Observer {
private RCModel game;
private JLabel score = new JLabel("", JLabel.CENTER);
private JLabel message = new JLabel("", JLabel.CENTER);
private Random random = new Random(System.currentTimeMillis());
private int highScore = RCPrefs.getHighScore();
private int highLevel = RCPrefs.getHighLevel();
private int highJumps = RCPrefs.getHighJumps();
private Cursor moveCursor = new Cursor(Cursor.MOVE_CURSOR);
private Cursor sysCursor = new Cursor(Cursor.DEFAULT_CURSOR);
private MessageFormat scoreForm = new MessageFormat(
"High score: {0} on level {1} with {2} jumps");
private static final int height = 24;
private static final int textHeight = 12;
private static final String helpMessage = "Press H for help.";
private static final String resetMessage = "High score reset.";
private static final Color c = RCImage.borderColor;
private static final String [] messageArray = new String [] {
"New high score!",
"Well done!",
"A new personal best!",
"Good show!",
"A stunning achievment!"
};
/**
* Construct an informative view of the specified game.
*
* @param game a game board
*/
public RCInfo(RCModel game) {
this.game = game;
this.setLayout(new GridLayout(1, 2));
score.setForeground(Color.LIGHT_GRAY);
score.setBackground(Color.DARK_GRAY);
score.setOpaque(true);
score.setFont(new Font("SansSerif", Font.BOLD, textHeight));
score.setBorder(BorderFactory.createMatteBorder(1, 1, 2, 2, c));
score.setText(scoreMessage());
message.setForeground(Color.LIGHT_GRAY);
message.setBackground(Color.DARK_GRAY);
message.setOpaque(true);
message.setFont(new Font("SansSerif", Font.BOLD, textHeight));
message.setBorder(BorderFactory.createMatteBorder(1, 2, 2, 1, c));
this.showSize();
this.setBackground(Color.DARK_GRAY);
this.add(message);
this.add(score);
this.addMouseListener(this);
this.setCursor(moveCursor);
this.game.addObserver(this);
}
/** Return this panel's preferred size. */
@Override
public Dimension getPreferredSize() {
return new Dimension(0, height);
}
/** Update the game's highlights. */
public void update(Observable model, Object arg) {
int currentScore = game.getDeadRobots();
int currentLevel = game.getLevel();
int currentJumps = game.getSafeJumps();
if ((currentScore > highScore) ||
(currentLevel > highLevel) ||
(currentJumps > highJumps)) {
highScore = currentScore;
highLevel = currentLevel;
highJumps = currentJumps;
message.setText(randomMessage());
score.setText(scoreMessage());
RCPrefs.putHighScore(highScore);
RCPrefs.putHighLevel(highLevel);
RCPrefs.putHighJumps(highJumps);
}
}
/** Clear the user's previous high score & level; reset the game. */
public void resetHilights() {
highScore = 0;
highLevel = 0;
highJumps = 0;
game.resetGame();
message.setText(resetMessage);
}
/** Show the current board size. */
public void showSize() {
message.setText(helpMessage + " [" +
game.getWidth() + "x" + game.getHeight() + "]");
}
/** Show the board size while resizing. */
public void showSize(int w, int h) {
message.setText(helpMessage + " [" + w + "x" + h + "]");
}
private String scoreMessage() {
Object [] scoreArgs = {highScore, highLevel, highJumps};
return scoreForm.format(scoreArgs);
}
private String randomMessage() {
int randomIndex = random.nextInt(messageArray.length);
return messageArray[randomIndex];
}
/** Handle mouseClicked events. */
public void mouseClicked(MouseEvent e) {
game.move();
}
/** Handle mouseEntered events to set the cursor. */
public void mouseEntered(MouseEvent e) { setCursor(moveCursor); }
/** Handle mouseExited events to restore the cursor. */
public void mouseExited(MouseEvent e) { setCursor(sysCursor); }
/** Handle mouseReleased events. */
public void mouseReleased(MouseEvent e) { }
/** Handle mousePressed events. */
public void mousePressed(MouseEvent e) {}
}