-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDodgingButton.java
More file actions
46 lines (44 loc) · 1.52 KB
/
Copy pathDodgingButton.java
File metadata and controls
46 lines (44 loc) · 1.52 KB
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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DodgingButton extends JFrame {
private JButton closeButton;
private static final int FRAME_WIDTH = 400;
private static final int FRAME_HEIGHT = 400;
public DodgingButton() {
super("Dodging Close Button Game");
setUndecorated(true);
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
closeButton = new JButton("Close");
closeButton.setBounds(100, 100, 90, 30);
add(closeButton);
closeButton.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
moveButton();
}
});
closeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
setLocationRelativeTo(null);
setVisible(true);
}
private void moveButton() {
int newX = (int) (Math.random() * (getWidth() - closeButton.getWidth()));
int newY = (int) (Math.random() * (getHeight() - closeButton.getHeight()));
closeButton.setLocation(newX, newY);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new DodgingButton();
}
});
}
}