-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAnimationDemo.java
executable file
·67 lines (58 loc) · 1.94 KB
/
AnimationDemo.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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AnimationDemo extends JFrame {
public AnimationDemo() {
// Create two MovingMessagePanel for displaying two moving messages
this.setLayout(new GridLayout(2, 1));
add(new MovingMessagePanel("message 1 moving?"));
add(new MovingMessagePanel("message 2 moving?"));
}
/** Main method */
public static void main(String[] args) {
AnimationDemo frame = new AnimationDemo();
frame.setTitle("AnimationDemo");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(280, 100);
frame.setVisible(true);
}
// Inner class: Displaying a moving message
static class MovingMessagePanel extends JPanel {
private String message = "Welcome to Java";
private int xCoordinate = 0;
private int yCoordinate = 20;
private Timer timer = new Timer(1000, new TimerListener());
public MovingMessagePanel(String message) {
this.message = message;
// Start timer for animation
timer.start();
// Control animation speed using mouse buttons
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int delay = timer.getDelay();
if (e.getButton() == MouseEvent.BUTTON1)
timer.setDelay(delay > 10 ? delay - 10 : 0);
else if (e.getButton() == MouseEvent.BUTTON3)
timer.setDelay(delay < 50000 ? delay + 10 : 50000);
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (xCoordinate > getWidth()) {
xCoordinate = -20;
}
xCoordinate += 5;
g.drawString(message, xCoordinate, yCoordinate);
}
class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
}
}
}