-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graphics1.java
50 lines (42 loc) · 1.12 KB
/
Graphics1.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
import javax.swing.*;
import java.awt.*;
public class Graphics1 extends JFrame {
Graphics1(){
setSize(800,600);
setTitle("Graphical Animation");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
MyAnimation panel = new MyAnimation(780,560);
panel.setBackground(Color.PINK);
add(panel);
}
public static void main(String[] args) { new Graphics1(); }
}
class MyAnimation extends JPanel{
int x ;
int y ;
int width = 100;
int height = 50;
int stepSize = 10;
int max_X,max_y;
MyAnimation(int w , int h){
this.max_X = w;
max_y = h;
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.WHITE);
g.fillRect(x,y,width,height);
if(x + width > max_X || x<0)
stepSize=-stepSize;
x += stepSize;
try {
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
}
}