F:\java\knim-game\sources\ru\ifmo\knim\screens\ControlPanel.java
|
1 /**
2 * @(#)ControlPanel.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.JPanel;
10 import javax.swing.JSpinner;
11 import javax.swing.SpinnerNumberModel;
12
13 import ru.ifmo.knim.main.GameInfo;
14
15 /**
16 * Represents a panel with plus buttons, minus buttons & edit boxes.
17 *
18 * @author Anthony Yakovlev
19 */
20 public class ControlPanel extends JPanel {
21
22 /**
23 * Spinners that are used to select moves
24 */
25 JSpinner spinners [];
26
27 /**
28 * Game information. Assumed that is passed by pointer.
29 */
30 GameInfo gameInfo;
31
32 /**
33 * Constructs a new control panel object.
34 * @param gameInfo the game information to use.
35 * @param width width of control
36 * @param height height of control
37 */
38 public ControlPanel (GameInfo gameInfo, int width, int height) {
39 spinners = new JSpinner [gameInfo.getLineAmount()];
40
41 setLayout(null);
42 setSize(width, height);
43
44 this.gameInfo = gameInfo;
45 height = height / gameInfo.getLineAmount();
46
47 for (int i = 0; i < gameInfo.getLineAmount(); i++) {
48 spinners [i] = new JSpinner();
49 spinners [i].setBounds(0, i * height, width, Math.min(height, 32));
50 spinners [i].setModel( new SpinnerNumberModel(0, 0, gameInfo.getFishInLine(i), 1) );
51
52 add( spinners [i] );
53 }
54
55 }
56
57 /**
58 * Returns the move, parsing it from the text data
59 * @return fantomas
60 */
61 public int [] getMove () {
62 int [] mas = new int [spinners.length];
63 for (int i = 0; i < spinners.length; i++) {
64 mas [i] = ((Integer)spinners [i].getValue()).intValue();
65 }
66
67 return mas;
68 }
69
70 /**
71 * Updates limits in spinners
72 */
73 public void updateLimits() {
74 for (int i = 0; i < gameInfo.getLineAmount(); i++) {
75 spinners [i].setModel( new SpinnerNumberModel(0, 0, gameInfo.getFishInLine(i), 1) );
76 }
77 }
78 }
79