//============================================================================= // UpperHandBallView.java // Copyright(c) 1998 Satoshi Kobayashi, All rights reserved. //============================================================================= import java.awt.*; //============================================================================= // class UpperHandBallView // プレーヤーの持ち玉を表示するクラス。 //============================================================================= public class UpperHandBallView extends Canvas { //----------------------------------------------------------------------------- // クラス変数定義 //----------------------------------------------------------------------------- // 玉の大きさ private final static int size = 14; // 縦横に並べる玉の数 private final static int width = 7; private final static int height = 4; //----------------------------------------------------------------------------- // インスタンス変数定義 //----------------------------------------------------------------------------- // 表示対象のゲーム private UpperHandGame game; // 表示対象のプレーヤー private int player; // 玉の色 private Color color; //----------------------------------------------------------------------------- // コンストラクタ定義 //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // public UpperHandBallView(UpperHandGame game, int player)) // UpperHandBallViewのインスタンスを生成し、初期化を行う。 // UpperHandGame game - 表示対象のゲーム // int player - 表示対象のプレーヤー //----------------------------------------------------------------------------- public UpperHandBallView(UpperHandGame game, int player) { this.game = game; this.player = player; // 玉の色を決める。 switch (player) { case UpperHandGame.FIRST: this.color = UpperHand.color[UpperHand.FIRST_COLOR]; break; case UpperHandGame.SECOND: this.color = UpperHand.color[UpperHand.SECOND_COLOR]; break; default: throw new Error("Bad player"); } // コンポーネントの背景色を設定する。 setBackground(UpperHand.color[UpperHand.BG_COLOR]); } //----------------------------------------------------------------------------- // メソッド定義 //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // public void paint(Graphics g) // 現在の持ち玉を描画する。 //----------------------------------------------------------------------------- public void paint(Graphics g) { Color c1; // 左と上の辺の色 Color c2; // 右と下の辺の色 // 背景を描く。 c1 = UpperHand.color[UpperHand.BG_COLOR].darker().darker(); c2 = UpperHand.color[UpperHand.BG_COLOR].brighter().brighter(); g.setColor(c1); g.drawLine(0, 0, 0, height * size - 1); g.drawLine(0, 0, width * size - 1, 0); g.setColor(c2); g.drawLine(width * size - 1, 0, width * size - 1, height * size - 1); g.drawLine(0, height * size - 1, width * size - 1, height * size - 1); // 玉を描く。 g.setColor(color); int p = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (p < game.ball(player)) { g.fillOval(size * x + 1, size * y + 1, size - 2, size - 2); } p++; } } } //----------------------------------------------------------------------------- // public Dimension minimumSize() // コンポーネントの最小サイズを返す。 //----------------------------------------------------------------------------- public Dimension minimumSize() { return new Dimension(size * width, size * height); } //----------------------------------------------------------------------------- // public Dimension preferredSize() // コンポーネントの推奨サイズを返す。 //----------------------------------------------------------------------------- public Dimension preferredSize() { return minimumSize(); } }