Repository navigation
Expand file tree
/
Copy pathtask_8.java
More file actions
179 lines (158 loc) · 5.96 KB
/
Copy pathtask_8.java
File metadata and controls
179 lines (158 loc) · 5.96 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.File;
// App class
class MyApp extends JFrame {
int WINDOW_WIDTH = 600;
int WINDOW_HEIGHT = 600;
String background_image_path;
String animation_images_path = "Java_task_8/frames/";
int method;
// Init
MyApp() {
super("Some shapes");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
//setBackground(Color.BLACK);
setLocation(300, 300);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
this.method = 0;
// Btn "Start animation"
Button btn = new Button("Start animation");
btn.setSize(200, 100);
btn.setLocation(0, 0);
btn.addActionListener(
new ActionListener () {
public void actionPerformed(ActionEvent event) {
method = 1;
setTitle("Animation");
setSize(WINDOW_WIDTH + 1, WINDOW_HEIGHT);
setSize(WINDOW_WIDTH - 1, WINDOW_HEIGHT);
}
}
);
add(btn);
}
void set_background_image_path(String path) {
this.background_image_path = path;
}
void set_animation_images_path(String path) {
this.animation_images_path = path;
}
// Paint method
@Override
public void paint(Graphics g2) {
Graphics2D g = (Graphics2D) g2;
Image img = Toolkit.getDefaultToolkit().getImage(this.background_image_path);
g.drawImage(img, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, this);
switch (this.method) {
case 0:
random_shapes_paint(50, g);
break;
case 1:
animate(this.animation_images_path, g);
break;
default:
break;
}
}
// Random shapes paint methid
void random_shapes_paint(int shapes, Graphics2D g) {
for (int i = 0; i < shapes; i++) {
int choice = (int) (Math.random() * 7);
switch (choice) {
// Oval
case 1:
g.setColor(new Color((int) (Math.random() * 0x1000000)));
g.fillOval(
(int) (Math.random() * WINDOW_WIDTH),
(int) (Math.random() * WINDOW_HEIGHT),
(int) (Math.random() * 200),
(int) (Math.random() * 200)
);
break;
// Rect
case 2:
g.setColor(new Color((int) (Math.random() * 0x1000000)));
g.fillRect(
(int) (Math.random() * WINDOW_WIDTH),
(int) (Math.random() * WINDOW_HEIGHT),
(int) (Math.random() * 200),
(int) (Math.random() * 200)
);
break;
// Line
case 3:
g.setColor(new Color((int) (Math.random() * 0x1000000)));
g.drawLine(
(int) (Math.random() * WINDOW_WIDTH),
(int) (Math.random() * WINDOW_WIDTH),
(int) (Math.random() * WINDOW_HEIGHT),
(int) (Math.random() * WINDOW_HEIGHT)
);
break;
// Arc
case 4:
g.setColor(new Color((int) (Math.random() * 0x1000000)));
g.drawArc(
(int) (Math.random() * WINDOW_WIDTH),
(int) (Math.random() * WINDOW_HEIGHT),
(int) (Math.random() * WINDOW_WIDTH),
(int) (Math.random() * WINDOW_HEIGHT),
(int) (Math.random() * 360),
(int) (Math.random() * 360)
);
break;
// Rounded rect
case 5:
g.setColor(new Color((int) (Math.random() * 0x1000000)));
g.fillRoundRect(
(int) (Math.random() * WINDOW_WIDTH),
(int) (Math.random() * WINDOW_HEIGHT),
(int) (Math.random() * 200),
(int) (Math.random() * 200),
(int) (Math.random() * 200),
(int) (Math.random() * 200)
);
break;
// Circle
case 6:
g.setColor(new Color((int) (Math.random() * 0x1000000)));
g.fillOval(
(int) (Math.random() * WINDOW_WIDTH),
(int) (Math.random() * WINDOW_HEIGHT),
100,
100
);
break;
default:
break;
}
}
}
void animate(String frames_path, Graphics2D g) {
File dir = new File(frames_path);
for (File file : dir.listFiles()) {
Image frame = Toolkit.getDefaultToolkit().getImage(file.getPath());
g.drawImage(frame, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, this);
try { Thread.sleep(500); } catch (InterruptedException e) {}
}
}
// Start app
public static void main(String[] args) {
MyApp app = new MyApp();
if (args.length == 1) {
app.set_background_image_path(args[0]);
}
else if (args.length == 2) {
app.set_background_image_path(args[0]);
app.set_animation_images_path(args[1]);
}
else {
System.out.println("[!] Background image path is empty");
}
/* "C:/Users/moran/Desktop/MIREA/Mirea-Java-Tasks/Java_task_8/background.jpg" */
}
}