-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCirclePanel.java
73 lines (61 loc) · 1.92 KB
/
CirclePanel.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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CirclePanel extends JPanel {
private int x, y;
private int diameter = 50;
public CirclePanel() {
x = 50;
y = 50;
JButton upButton = new JButton("Up");
upButton.addActionListener(new UpListener());
add(upButton);
JButton downButton = new JButton("Down");
downButton.addActionListener(new DownListener());
add(downButton);
JButton leftButton = new JButton("Left");
leftButton.addActionListener(new LeftListener());
add(leftButton);
JButton rightButton = new JButton("Right");
rightButton.addActionListener(new RightListener());
add(rightButton);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillOval(x, y, diameter, diameter);
}
private class UpListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
y -= 10;
repaint();
}
}
private class DownListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
y += 10;
repaint();
}
}
private class LeftListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
x -= 10;
repaint();
}
}
private class RightListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
x += 10;
repaint();
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Circle Movement");
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CirclePanel panel = new CirclePanel();
frame.add(panel);
frame.setVisible(true);
}
}