Skip to content

Commit a55f41a

Browse files
committed
fix(hint): change logic hint feat and new design
1 parent f2decf9 commit a55f41a

3 files changed

Lines changed: 38 additions & 37 deletions

File tree

asset/hint.png

44.9 KB
Loading

asset/hints.png

-2.49 KB
Binary file not shown.

src/main/GamePanel.java

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
import java.awt.Font;
66
import java.awt.GridLayout;
77
import java.awt.Image;
8-
import java.awt.Toolkit;
9-
import java.awt.datatransfer.Clipboard;
10-
import java.awt.datatransfer.StringSelection;
118
import java.awt.event.ActionEvent;
129
import java.awt.event.ActionListener;
1310
import java.awt.event.MouseAdapter;
@@ -154,15 +151,20 @@ private void Render() {
154151
score_label.setAlignmentX(CENTER_ALIGNMENT);
155152
add(score_label);
156153

157-
ImageIcon hintsIcon = new ImageIcon("asset/hints.png");
158-
Image image = hintsIcon.getImage();
159-
Image scaledImage = image.getScaledInstance(25, 25, Image.SCALE_SMOOTH);
160-
ImageIcon scaledImageIcon = new ImageIcon(scaledImage);
161-
JLabel hints_button = new JLabel(scaledImageIcon);
162-
hints_button.setBorder(new EmptyBorder(20, 0, 0, 10));
163-
hints_button.setCursor(new Cursor(Cursor.HAND_CURSOR));
164-
LinkHints(hints_button);
165-
add(hints_button);
154+
JLabel hint_button = new JLabel();
155+
ImageIcon hintImage = new ImageIcon(getClass().getClassLoader().getResource("asset/hint.png"));
156+
hint_button.setIcon(
157+
new ImageIcon(hintImage.getImage().getScaledInstance(25, 25, Image.SCALE_SMOOTH))
158+
);
159+
hint_button.setHorizontalTextPosition(JLabel.CENTER);
160+
hint_button.setBorder(new EmptyBorder(20, 0, 0, 0));
161+
hint_button.setForeground(ColorScheme.white);
162+
hint_button.setFont(new Font(Font.DIALOG, Font.BOLD, 20));
163+
hint_button.setAlignmentX(CENTER_ALIGNMENT);
164+
hint_button.setCursor(new Cursor(Cursor.HAND_CURSOR));
165+
hint_button.setVisible(true);
166+
hint(hint_button, status_value);
167+
add(hint_button);
166168

167169
submit_button.addMouseListener(
168170
new MouseAdapter() {
@@ -176,6 +178,7 @@ public void mouseClicked(MouseEvent e) {
176178
submit_button,
177179
continue_button,
178180
back_button,
181+
hint_button,
179182
score_label
180183
);
181184
}
@@ -194,27 +197,35 @@ public void actionPerformed(ActionEvent e) {
194197
submit_button,
195198
continue_button,
196199
back_button,
200+
hint_button,
197201
score_label
198202
);
199203
}
200204
}
201205
);
202206
}
203207

204-
private void LinkHints(JLabel hintsFrame) {
205-
hintsFrame.addMouseListener(
208+
private void hint(JLabel hint_button, JLabel status_value) {
209+
hint_button.addMouseListener(
206210
new MouseAdapter() {
211+
String hint = String.valueOf(Integer.toBinaryString(UNKNOW_NUMBER));
207212
@Override
208213
public void mouseClicked(MouseEvent e) {
209-
String hintString = String.valueOf(
210-
Integer.toBinaryString(UNKNOW_NUMBER)
211-
);
212-
JOptionPane.showConfirmDialog(
214+
int dialogResult = JOptionPane.showConfirmDialog(
213215
null,
214-
hintString,
215-
"Hints",
216-
JOptionPane.CLOSED_OPTION
216+
"Are you sure you want to use a hint?\nYou will lose 1 score.",
217+
"Warning",
218+
JOptionPane.YES_NO_OPTION
217219
);
220+
if (dialogResult == JOptionPane.YES_OPTION) {
221+
if (SCORE >= 1) {
222+
SCORE -= 1;
223+
status_value.setText("I'll give you a hint: " + hint);
224+
status_value.setFont(new Font(Font.DIALOG, Font.BOLD, 15));
225+
} else {
226+
status_value.setText("Not enough score!");
227+
}
228+
}
218229
}
219230
}
220231
);
@@ -258,6 +269,7 @@ private void changeStatusValue(
258269
JLabel submit_button,
259270
JLabel continue_button,
260271
JLabel back_button,
272+
JLabel hint_button,
261273
JLabel score_label
262274
) {
263275
if (guess_field.getText().equals("")) {
@@ -269,13 +281,7 @@ private void changeStatusValue(
269281

270282
if (guess_field.getText().equals("cheat")) {
271283
status_value.setText("The answer is: " + UNKNOW_NUMBER);
272-
// Copy to clipboard
273-
StringSelection stringSelection = new StringSelection(
274-
String.valueOf(UNKNOW_NUMBER)
275-
);
276-
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
277-
clipboard.setContents(stringSelection, null);
278-
guess_field.setText("");
284+
guess_field.setText(String.valueOf(UNKNOW_NUMBER));
279285
return;
280286
}
281287

@@ -285,21 +291,15 @@ private void changeStatusValue(
285291
getClass().getClassLoader().getResource("asset/correct.wav")
286292
);
287293
unknown_number.setText(String.valueOf(""));
288-
Image originalImg = new ImageIcon(
294+
ImageIcon suntanaImage = new ImageIcon(
289295
getClass().getClassLoader().getResource("asset/suntana.png")
290-
)
291-
.getImage();
292-
Image resizedImg = originalImg.getScaledInstance(
293-
200,
294-
225,
295-
Image.SCALE_SMOOTH
296296
);
297-
unknown_number.setIcon(new ImageIcon(resizedImg));
298-
unknown_number.setForeground(ColorScheme.gold);
297+
unknown_number.setIcon(new ImageIcon(suntanaImage.getImage().getScaledInstance(200, 205, Image.SCALE_SMOOTH)));
299298
gridPanel.setVisible(false);
300299
submit_button.setVisible(false);
301300
continue_button.setVisible(true);
302301
back_button.setVisible(true);
302+
hint_button.setVisible(false);
303303
SCORE++;
304304
score_label.setText(
305305
"Score: " + SCORE + " 🏆 High Score: " + HIGH_SCORE
@@ -346,6 +346,7 @@ private void changeStatusValue(
346346
submit_button.setVisible(false);
347347
continue_button.setVisible(true);
348348
back_button.setVisible(true);
349+
hint_button.setVisible(false);
349350
SCORE++;
350351
score_label.setText(
351352
"Score: " + SCORE + " 🏆 High Score: " + HIGH_SCORE

0 commit comments

Comments
 (0)