-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrawingPanel.java
More file actions
77 lines (64 loc) · 2.54 KB
/
DrawingPanel.java
File metadata and controls
77 lines (64 loc) · 2.54 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
Create a Swing application with a main window (JFrame).
Inside it, add a custom panel (JPanel) where you override paintComponent(Graphics g).
In the custom panel:
Draw at least three different shapes (rectangle, oval, line).
Draw a string of text in a custom color and font.
(Optional challenge) Draw an image from your computer inside the panel.
Add a button below the panel. When clicked, it should:
Change the color of shapes randomly.
Call repaint() to update the drawing.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class PaintingExercise extends JFrame {
private DrawingPanel drawingPanel;
public PaintingExercise() {
setTitle("Swing Painting Exercise");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
drawingPanel = new DrawingPanel();
add(drawingPanel, BorderLayout.CENTER);
JButton btnChangeColor = new JButton("Change Colors");
btnChangeColor.addActionListener(e -> {
drawingPanel.changeColors();
drawingPanel.repaint();
});
add(btnChangeColor, BorderLayout.SOUTH);
}
class DrawingPanel extends JPanel {
private Color rectColor = Color.BLUE;
private Color ovalColor = Color.RED;
private Color textColor = Color.MAGENTA;
private Random random = new Random();
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// Draw rectangle
g2d.setColor(rectColor);
g2d.fillRect(50, 50, 100, 80);
// Draw oval
g2d.setColor(ovalColor);
g2d.fillOval(200, 50, 120, 80);
// Draw line
g2d.setColor(Color.BLACK);
g2d.drawLine(50, 200, 300, 200);
// Draw text
g2d.setColor(textColor);
g2d.setFont(new Font("Serif", Font.BOLD, 20));
g2d.drawString("Hello Swing Painting!", 50, 250);
}
public void changeColors() {
rectColor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
ovalColor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
textColor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new PaintingExercise().setVisible(true));
}
}