F:\java\knim-game\sources\ru\ifmo\knim\screens\StonePanel.java
|
1 /**
2 * @(#)StonePanel.java
3 *
4 * Copyright Anthony Yakovlev <yakovlev@rain.ifmo.ru> and Michail Lukin <michail@users.msn.com>
5 */
6
7 package ru.ifmo.knim.screens;
8
9 import javax.swing.ImageIcon;
10 import javax.swing.JLabel;
11 import javax.swing.JPanel;
12
13 /**
14 * Represents a panel with stones
15 * @author Anthony Yakovlev
16 */
17 public class StonePanel extends JPanel {
18
19 /**
20 * Array of labels for stones.
21 */
22 JLabel [] [] stones;
23
24 /**
25 * Constructs a new stone panel object.
26 * @param dimensions the array with stones amount per each line
27 * @param fishImage image to use as a picture of stone
28 * @param padding the spacing between stones
29 */
30 public StonePanel (int [] dimensions, ImageIcon fishImage, int padding) {
31 stones = new JLabel [dimensions.length] [];
32 int maxStones = -239;
33 setLayout(null);
34
35 int width = fishImage.getIconWidth();
36 int height = fishImage.getIconHeight();
37
38 for (int i = 0; i < dimensions.length; i++) {
39
40 if (dimensions [i] > maxStones) {
41 maxStones = dimensions [i];
42 }
43
44 stones [i] = new JLabel [dimensions [i] ];
45 for (int j = 0; j < dimensions [i]; j++) {
46 stones [i] [j] = new JLabel(fishImage);
47 stones [i] [j].setBounds(j * (width + padding), i * (height + padding), width, height);
48 add(stones [i] [j]);
49 }
50 }
51
52 setSize(maxStones * (width + padding), (height + padding) * dimensions.length);
53 }
54
55 /**
56 * Takes stones from given row.
57 * @param row the row to take stones
58 * @param num the amount of stones to take
59 */
60 public void takeStones(int row, int num){
61 int pos = stones [row].length - 1;
62 while (pos > -1 && num > 0) {
63 if (stones [row] [pos].isVisible()) {
64 stones [row] [pos].setVisible(false);
65 num--;
66 }
67 pos--;
68 }
69 }
70
71 /**
72 * Shows all hidden stones.
73 */
74 public void reset() {
75 for (int i = 0; i < stones.length; i++) {
76 for (int j = 0; j < stones [i].length; j++) {
77 stones [i] [j].setVisible(true);
78 }
79 }
80 }
81 }
82