-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractical_40.java
More file actions
46 lines (41 loc) · 1.29 KB
/
Practical_40.java
File metadata and controls
46 lines (41 loc) · 1.29 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 javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Practical_40 extends JPanel {
private Color color;
public Practical_40() {
color = Color.BLUE;
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
color = Color.RED;
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
color = Color.BLUE;
repaint();
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
int size = Math.min(width, height) - 200;
int x = (width - size) / 2;
int y = (height - size) / 2;
g.setColor(color);
g.fillOval(x, y, size, size);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Circle Color Changer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Practical_40 circle = new Practical_40();
frame.add(circle);
frame.setSize(400, 400);
frame.setVisible(true);
}
}