summary refs log tree commit diff
path: root/src/BoardSquareButton.java
blob: 9c0a1e47eb52fe6583f9d02003c9a193ef4b5219 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


/*
* TODO: move the mouselistener to main so we can call restart from there + we can investigate surrounding buttons from there
*
* */

public class BoardSquareButton extends JButton {

    public static final int DEFAULT_WIDTH = 100;
    public static final int DEFAULT_HEIGHT = 100;
    public static final String DEFAULT_TEXT = "?";
    public static final String MINE_TEXT = "O";
    public static final Color DEFAULT_COLOR = Color.GRAY;
    public static final Color INVESTIGATED_COLOR = Color.GREEN;
    public static final Color MINE_COLOR = Color.BLACK;
    public static final Color POTENTIAL_COLOR = Color.RED;

    private int width;
    private int height;
    private Color color;
    private int x;
    private int y;
    private int surroundingMines;
    private boolean mine;
    private boolean investigated;
    private boolean potential;

    /*
        CONSTRUCTORS
    */

    public BoardSquareButton(int x, int y, int width, int height, Color color)
    {
        this.x = x;
        this.y = y;

        initialise();

        this.setDefaultDimension(width, height);
        this.setColor(color);

        this.addNewMouseListener();
    }

    public BoardSquareButton(int x, int y)
    {
        this.x = x;
        this.y = y;

        initialise();

        this.addNewMouseListener();
    }

    private void addNewMouseListener()
    {
        super.addMouseListener(new Psymp7Main());
    }

    public void initialise()
    {
        this.setDefaultDimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        super.setFont(new Font("Ariel", Font.PLAIN, 40));
        super.setText("?");
        super.setBackground(DEFAULT_COLOR);
        this.setColor(DEFAULT_COLOR);
        this.setMine(false);
        this.setInvestigated(false);
        this.setPotential(false);
    }

    /*
        GETTERS AND SETTERS
    */
    public BoardSquareButton getSelf()
    {
        return this;
    }

    public void setDefaultDimension(int x, int y)
    {
        this.setWidth(x);
        this.setHeight(y);
        super.setPreferredSize(new Dimension(x, y));
    }

    public int getWidth()
    {
        return width;
    }

    public void setWidth(int width)
    {
        this.width = width;
    }

    public int getHeight()
    {
        return height;
    }

    public void setHeight(int height)
    {
        this.height = height;
    }

    public int getSurroundingMines()
    {
        return surroundingMines;
    }

    public void setSurroundingMines(int mines)
    {
        this.surroundingMines = mines;
    }

    public Color getColor()
    {
        return color;
    }

    public void setColor(Color color)
    {
        this.color = color;
        super.setBackground(color);
    }

    public int getRow()
    {
        return x;
    }

    public void setRow(int x) { this.x = x; }

    public int getColumn()
    {
        return y;
    }

    public void setColumn(int y)
    {
        this.y = y;
    }

    public boolean isMine() {
        return mine;
    }

    public void setMine(boolean mine)
    {
        this.mine = mine;
    }

    public boolean isInvestigated()
    {
        return investigated;
    }

    public void setInvestigated(boolean investigated)
    {
        this.investigated = investigated;
        if (investigated)
        {
            this.setPotential(false);
            if (mine)
            {
                this.setColor(MINE_COLOR);
                this.setText(MINE_TEXT);
            } else
            {
                this.setColor(INVESTIGATED_COLOR);
                this.setText("" + this.getSurroundingMines());
            }
        }
        else
        {
            this.setColor(DEFAULT_COLOR);
            this.setText(DEFAULT_TEXT);
        }
    }
    public boolean isPotential()
    {
        return potential;
    }

    public void setPotential(boolean potential)
    {
        this.potential = potential;
        if(potential)
            this.setColor(POTENTIAL_COLOR);
        else
            this.setColor(DEFAULT_COLOR);
    }

}