import java.applet.*; import java.awt.*; public class Stones extends Applet implements Runnable { StoneView view; Choice redWho, blueWho; Label redResult, blueResult, statusLabel; Button newGame; Thread kicker; int speed = 700; public void init() { setBackground(Color.black); setLayout(new BorderLayout(10, 10)); Panel p1 = new Panel(); add("South", p1); p1.setLayout(new GridLayout(1, 0, 10, 0)); p1.add(redResult = makeLabel(StoneModel.RED)); p1.add(redWho = makeChoice(StoneModel.RED)); p1.add(newGame = makeNewGameButton()); p1.add(blueWho = makeChoice(StoneModel.BLUE)); p1.add(blueResult = makeLabel(StoneModel.BLUE)); add("North", statusLabel = makeLabel(StoneModel.FREE)); add("Center", view = new StoneView(Integer.parseInt(getParameter("size")))); view.init(); } public void start() { if (kicker == null) { kicker = new Thread(this); kicker.start(); }; } public void run() { while (true) { if (getPlayer() > 0 && !view.getModel().isGameOver()) view.clicked(null); try { Thread.sleep(speed); } catch (InterruptedException e) {}; }; } public void stop() { if (kicker != null) { kicker.stop(); kicker = null; }; } protected Choice makeChoice(int side) { Choice choice = new Choice(); for (int i = 0; i < StoneModel.playerNames.length; i++) choice.addItem(StoneModel.playerNames[i]); choice.setForeground(side == StoneModel.RED? Color.red : Color.blue); return choice; } protected Label makeLabel(int side) { Label label = new Label(); switch (side) { case StoneModel.RED: label.setAlignment(Label.LEFT); label.setForeground(Color.red); break; case StoneModel.FREE: label.setAlignment(Label.LEFT); label.setForeground(Color.white); break; case StoneModel.BLUE: label.setAlignment(Label.RIGHT); label.setForeground(Color.blue); break; }; label.setFont(new Font(getFont().getName(), Font.BOLD, 18)); return label; } protected Button makeNewGameButton() { Button button = new Button("New Game"); button.setForeground(Color.white); return button; } public boolean action(Event evt, Object what) { if (evt.target == newGame) { view.newGame(); return true; } return false; } public void status(String text, int who) { switch (who) { case StoneModel.RED: redResult.setText(text); break; case StoneModel.FREE: statusLabel.setText(text); break; case StoneModel.BLUE: blueResult.setText(text); break; }; } public int getPlayer() { return getPlayer(view.getSide()); } public int getPlayer(int side) { return (side == StoneModel.RED? redWho : blueWho).getSelectedIndex(); } }