Repository navigation
Expand file tree
/
Copy pathControlCircle.java
More file actions
54 lines (45 loc) · 1.53 KB
/
Copy pathControlCircle.java
File metadata and controls
54 lines (45 loc) · 1.53 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
47
48
49
50
51
52
53
54
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ControlCircle extends JFrame {
private JButton jbtEnlarge = new JButton("Enlarge");
private JButton jbtShrink = new JButton("Shrink");
private CirclePanel canvas = new CirclePanel();
public ControlCircle() {
JPanel panel = new JPanel(); // Use the panel to group buttons
panel.add(jbtEnlarge);
panel.add(jbtShrink);
this.add(canvas, BorderLayout.CENTER); // Add canvas to center
this.add(panel, BorderLayout.SOUTH); // Add buttons to the frame
jbtEnlarge.addActionListener(new EnlargeListener());
}
/** Main method */
public static void main(String[] args) {
JFrame frame = new ControlCircle();
frame.setTitle("ControlCircle");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
class EnlargeListener implements ActionListener { // Inner class
@Override
public void actionPerformed(ActionEvent e) {
canvas.enlarge();
}
}
class CirclePanel extends JPanel { // Inner class
private int radius = 5; // Default circle radius
/** Enlarge the circle */
public void enlarge() {
radius++;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(getWidth() / 2 - radius, getHeight() / 2 - radius,
2 * radius, 2 * radius);
}
}
}