//============================================================================= // UpperHand.java // Copyright(c) 1998 Satoshi Kobayashi, All rights reserved. //============================================================================= import java.applet.*; import java.awt.*; //============================================================================= // class UpperHand // Applet UpperHand を定義するクラス。 //============================================================================= public class UpperHand extends Applet { //----------------------------------------------------------------------------- // クラス変数定義 //----------------------------------------------------------------------------- // バージョン表示用文字列 private final static String version = "UpperHand Game 1.1a3"; // 著作権表示文字列 private final static String copyright = "Copyright(c) 1998 Satoshi Kobayashi, All rights reserved."; // 玉の大きさ private final static int size = 20; // 玉の描画エリア private static Rectangle area[]; // 使用する色のインデックス public final static int BG_COLOR = 0; // 背景色 public final static int NUTRAL_COLOR = 1; // 中立の玉の色 public final static int FIRST_COLOR = 2; // 先手の玉の色 public final static int SECOND_COLOR = 3; // 後手の玉の色 // 使用する色の定義 public final static Color color[] = { Color.lightGray, Color.green.darker(), Color.blue, Color.red }; //----------------------------------------------------------------------------- // クラス変数の初期化 //----------------------------------------------------------------------------- static { // 玉の描画エリアを初期化する。 // 位置pと、その(x,y,z)座標の対応をとり、sizeから位置pの描画エリアを // 計算している。 area = new Rectangle[55]; int p = 0; for (int z = 4; z >= 0; z--) { for (int y = 0; y <= z; y++) { for (int x = 0; x <= z; x++) { area[p] = new Rectangle( (4 - z) * size / 2 + size * x, (4 - z) * size / 2 + size * y, size, size); p++; } } } } //----------------------------------------------------------------------------- // インスタンス変数定義 //----------------------------------------------------------------------------- // ゲーム盤 private UpperHandGame game; //----------------------------------------------------------------------------- // メソッド定義 //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // public void init() // Appletの初期化処理を行う。 //----------------------------------------------------------------------------- public void init() { System.err.println("UpperHand.init() called."); // ゲーム盤を新規に作成し、初期化する。 game = new UpperHandGame(); } //----------------------------------------------------------------------------- // public void start() // Appletのページに移動したときの処理を行う。 //----------------------------------------------------------------------------- public void start() { System.err.println("UpperHand.start() called."); } //----------------------------------------------------------------------------- // public void stop() // Appletのページから離れるときの処理を行う。 //----------------------------------------------------------------------------- public void stop() { System.err.println("UpperHand.stop() called."); } //----------------------------------------------------------------------------- // public void destroy() // Appletを破棄するときの処理を行う。 //----------------------------------------------------------------------------- public void destroy() { System.err.println("UpperHand.destroy() called."); } //----------------------------------------------------------------------------- // public String getAppletInfo() // Appletのバージョン、著作件表示を返す。 //----------------------------------------------------------------------------- public String getAppletInfo() { return version + " " + copyright; } //----------------------------------------------------------------------------- // public void paint(Graphics g) // 現在のゲームの状態を描画する。 //----------------------------------------------------------------------------- public void paint(Graphics g) { System.err.println("UpperHand.paint() called."); paintBoard(g); // 盤を描画する。 // 玉を描画する。 for (int p = 0; p < area.length; p++) { paintBall(g, p); } } //----------------------------------------------------------------------------- // private void paintBoard(Graphics g) // ゲーム盤のみを描画する。 //----------------------------------------------------------------------------- private void paintBoard(Graphics g) { // 描画色を「背景色」とする。 g.setColor(color[BG_COLOR]); // 5×5の盤面に浮き上がった四角形を書く。 for (int y = 0; y < 5; y++) { for (int x = 0; x < 5; x++) { g.fill3DRect(x * size, y * size, size, size, true); } } } //----------------------------------------------------------------------------- // private void paintBall(Graphics g, int p) // pで指定された位置の玉を描画する。 //----------------------------------------------------------------------------- private void paintBall(Graphics g, int p) { Color c; // 玉の色を決定する。 switch (game.boardStatus(p)) { case UpperHandGame.FIRST: c = color[FIRST_COLOR]; break; case UpperHandGame.SECOND: c = color[SECOND_COLOR]; break; case UpperHandGame.NUTRAL: c = color[NUTRAL_COLOR]; break; default: return; } // 描画エリアより少し小さめに円を描く。 g.setColor(c); g.fillOval(area[p].x + 1, area[p].y + 1, area[p].width - 2, area[p].height - 2); } }