F:\java\knim-game\sources\ru\ifmo\knim\screens\FishControlPanel.java
|
1 /**
2 * @(#)FishControlPanel.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 java.awt.Color;
10 import java.awt.Graphics;
11
12 import javax.swing.ImageIcon;
13 import javax.swing.JPanel;
14
15 import ru.ifmo.knim.main.GameInfo;
16
17 /**
18 * Represents a game control panel.
19 * Contains a `stone view` ({@link #controlPanel}) and a control view ({@link #stonePanel})
20 * Contains resizing policy
21 * @author Anthony Yakovlev
22 */
23 public class FishControlPanel extends JPanel {
24 /**
25 * Control panel instance.
26 */
27 ControlPanel controlPanel;
28
29 /**
30 * Stone panel instance.
31 */
32 StonePanel stonePanel;
33
34 /**
35 * Image of stone.
36 */
37 ImageIcon iconStone;
38
39 /**
40 * Default spacing between neighboring cells.
41 */
42 int padding;
43
44 /**
45 * Fish control panel constructor
46 * @param iconMinus the image for minus sign in control panel
47 * @param iconPlus the image for plus sign in stone panel
48 * @param iconStone the stone image in stone panel
49 * @param gameInfo the game info instance
50 * @param padding space between neiboring controls
51 */
52 public FishControlPanel(ImageIcon iconStone, GameInfo gameInfo, int padding) {
53 // cache common info
54 this.iconStone = iconStone;
55 this.padding = padding;
56
57 // create objects
58 setLayout(null);
59 redoLayout(gameInfo);
60 }
61
62 /**
63 * Takes the given amount of stones from stone panel.
64 * @param row the row number, from which the stones will be taken
65 * @param num the amount of stones to take from given row
66 */
67 public void takeStones(int row, int num){
68 stonePanel.takeStones(row, num);
69 controlPanel.updateLimits();
70 }
71
72 /**
73 * Restarts the stone interface.
74 */
75 public void reset() {
76 stonePanel.reset();
77 controlPanel.updateLimits();
78 }
79
80 /**
81 * Returns the control panel state.
82 * @return mas
83 */
84 public int [] getMove () {
85 return controlPanel.getMove();
86 }
87
88 /**
89 * Perform layout with new game parameters.
90 * @param gameInfo the new game parameters
91 */
92 public void redoLayout(GameInfo gameInfo) {
93 // clean up
94 if (controlPanel != null) {
95 remove(controlPanel);
96 remove(stonePanel);
97 }
98
99 // create panel with new properties
100 stonePanel = new StonePanel(gameInfo.getLines(), iconStone, padding);
101 controlPanel = new ControlPanel(gameInfo, 45, stonePanel.getHeight());
102
103 // setup positions
104 stonePanel.setLocation(0, 0);
105 controlPanel.setLocation(stonePanel.getWidth() + 1, 0);
106
107 // adjust size
108 setSize(
109 stonePanel.getWidth() + controlPanel.getWidth(),
110 Math.max(stonePanel.getHeight(), controlPanel.getHeight())
111 );
112
113 // add to control list
114 add(controlPanel);
115 add(stonePanel);
116 }
117 }
118