-
Notifications
You must be signed in to change notification settings - Fork 0
/
myButton.java
142 lines (126 loc) · 3.43 KB
/
myButton.java
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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class myButton extends JButton implements ActionListener{
private int x;
private int y;
private int BOMB_VALUE = 99;
public static boolean bomb_trip;
private boolean toggled = false;
private boolean right_click_lock = false;
private MouseClickHandler mouseHandler = new MouseClickHandler();
private int rightClickCount = 0;
public myButton(int coordx, int coordy, String label){
super(label);
setBounds(20,20,300,100);
setLayout(new BorderLayout());
addActionListener(this);
this.addMouseListener(mouseHandler);
x = coordx;
y = coordy;
}
public int getCoordx(){
return x;
}
public int getCoordy(){
return y;
}
public void setToggle(boolean select){
toggled = select;
}
public boolean getToggle(){
return toggled;
}
public void resetRightClick(){
rightClickCount = 0;
}
public void setrightClickLock(boolean select){
right_click_lock = select;
}
private class MouseClickHandler extends MouseAdapter {
public void mouseClicked(MouseEvent event){
if(myBoard.time_init == false){
myBoard.timer.start();
myBoard.time_init = true;
}
if(right_click_lock == false){
if(event.getButton() == 3){
if(rightClickCount == 0){
setText("M");
toggled = true;
myBoard.updateMineCount(myBoard.NUM_OF_MINES - 1);
rightClickCount++;
}
else if(rightClickCount == 1){
setText("?");
myBoard.updateMineCount(myBoard.NUM_OF_MINES + 1);
rightClickCount++;
}
else if(rightClickCount == 2){
right_click_lock = false;
setText("");
rightClickCount = 0;
toggled = false;
}
}
else
System.out.println("Something else");
}
}
}
public void actionPerformed(ActionEvent event){
System.out.println(bomb_trip);
System.out.println("Toggle: " + toggled);
if(myBoard.time_init == false){
myBoard.timer.start();
myBoard.time_init = true;
}
if(bomb_trip == false){
if(toggled == false){
System.out.println("Coords: " + getCoordx() + ", " + getCoordy());
System.out.println("Minecheck: " + myBoard.minefield[getCoordx()][getCoordy()]);
int field_value = myBoard.minefield[getCoordx()][getCoordy()];
if(field_value >= BOMB_VALUE){
bomb_trip = true;
Icon mine = new ImageIcon("mine.png");
//this.setText("B");
setIcon(mine);
setBackground(Color.GRAY);
myBoard.timer.stop();
toggled = true;
JOptionPane.showMessageDialog(this, "There was a bomb!");
myBoard.showBoard();
}
else{
//JOptionPane.showMessageDialog(this, "Number of bombs around: " + field_value);
setBackground(Color.GRAY);
if(field_value == 0){
myBoard.cell_depthsearch(getCoordx(), getCoordy());
return;
}
else if(field_value == 1)
setForeground(Color.BLUE);
else if(field_value == 2)
setForeground(Color.GREEN);
else if(field_value == 3)
setForeground(Color.YELLOW);
else if(field_value == 4)
setForeground(Color.ORANGE);
else if(field_value == 5)
setForeground(Color.RED);
else if(field_value == 6)
setForeground(Color.CYAN);
else if(field_value == 7)
setForeground(Color.PINK);
else if(field_value == 8)
setForeground(Color.WHITE);
this.setText(field_value + "");
toggled = true;
myBoard.emptyCellsDecr();
right_click_lock = true;
}
}
}
}
}