-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomework1.java
97 lines (83 loc) · 2.71 KB
/
Homework1.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Homework1 extends JFrame implements ActionListener {
private final JComboBox<String> comboBox;
public Homework1() {
setLayout(new FlowLayout());
add(new JLabel("Choose the Shape"));
String[] shapes = {"None", "Tree", "Hexagonal Polygon", "Circles"};
comboBox = new JComboBox<>(shapes);
comboBox.addActionListener(this);
add(comboBox);
setSize(500, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void paint(Graphics g) {
super.paint(g);
switch (comboBox.getSelectedIndex()) {
case 1 -> drawShape1(g);
case 2 -> drawShape2(g);
case 3 -> drawShape3(g);
default -> {}
}
}
private void drawShape1(Graphics g) {
g.setColor(new Color(150, 70, 0));
g.fillRect(250, 250, 20, 140);
g.setColor(Color.GREEN);
g.fillOval(222, 140, 75, 150);
}
private void drawShape2(Graphics g) {
g.setColor(Color.CYAN);
g.drawLine(200, 200, 300, 200);
g.drawLine(200, 200, 150, 300);
g.drawLine(300, 200, 350, 300);
g.drawLine(200, 400, 300, 400);
g.drawLine(350, 300, 300, 400);
g.drawLine(150, 300, 200, 400);
g.setColor(Color.MAGENTA);
g.drawLine(150, 300, 350, 300);
g.drawLine(300, 200, 200, 400);
g.drawLine(200, 200, 300, 400);
g.setColor(Color.RED);
drawOval(g, 195, 195);
drawOval(g, 295, 195);
drawOval(g, 145, 295);
drawOval(g, 345, 295);
drawOval(g, 195, 395);
drawOval(g, 295, 395);
drawOval(g, 245, 295);
}
private void drawOval(Graphics g, int x, int y) {
g.fillOval(x, y, 13, 13);
}
private void drawShape3(Graphics g) {
g.setColor(Color.BLUE);
g.drawRect(110, 210, 260, 200);
g.setColor(Color.RED);
drawOval(g, 240, 250, 90, 120);
drawOval(g, 240, 240, 100, 140);
drawOval(g, 240, 230, 110, 160);
drawOval(g, 240, 220, 120, 180);
drawOval(g, 240, 210, 130, 200);
drawOval(g, 150, 250, 90, 120);
drawOval(g, 140, 240, 100, 140);
drawOval(g, 130, 230, 110, 160);
drawOval(g, 120, 220, 120, 180);
drawOval(g, 110, 210, 130, 200);
}
private void drawOval(Graphics g, int x, int y, int width, int height) {
g.drawOval(x, y, width, height);
}
@Override
public void actionPerformed(ActionEvent event) {
repaint();
}
public static void main(String[] args) {
new Homework1();
}
}