-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAnimatedIconDemo.java
More file actions
174 lines (142 loc) · 4.17 KB
/
AnimatedIconDemo.java
File metadata and controls
174 lines (142 loc) · 4.17 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class AnimatedIconDemo extends JPanel
{
public AnimatedIconDemo()
{
setLayout( new BorderLayout() );
JPanel panel = new JPanel( );
JLabel label1 = new JLabel();
AnimatedIcon icon1 = new AnimatedIcon(label1, 250, 3);
Icon anIcon = new ColorIcon(Color.BLUE, 70, 20 );
// Use image for better demo
// Icon anIcon = new ImageIcon( "???" );
icon1.addIcon( anIcon );
for (int angle = 45; angle < 360; angle += 45)
{
icon1.addIcon( new RotatedIcon(anIcon, angle) );
}
icon1.setShowFirstIcon( true );
label1.setIcon( icon1 );
panel.add(createPanel(label1, "Three Cycles"), BorderLayout.WEST);
JLabel label2 = new JLabel("Processing ");
label2.setHorizontalTextPosition( JLabel.LEADING );
AnimatedIcon icon2 = new AnimatedIcon( label2 );
icon2.setAlignmentX( AnimatedIcon.LEFT );
icon2.addIcon( new TextIcon(label2, ".") );
icon2.addIcon( new TextIcon(label2, "..") );
icon2.addIcon( new TextIcon(label2, "...") );
icon2.addIcon( new TextIcon(label2, "....") );
icon2.addIcon( new TextIcon(label2, ".....") );
label2.setIcon( icon2 );
Border title2 = new TitledBorder("Continuous Animation");
label2.setBorder( new CompoundBorder(title2, new EmptyBorder(0, 25, 0, 25)) );
JLabel label3 = new JLabel();
CircularAnimatedIcon icon3 = new CircularAnimatedIcon( label3 );
int j = 1;
for (int i = 255; i > 10; i -= 30)
{
icon3.addIcon( new ColorIcon( new Color(255-i, i, i), j*5, j*5) );
j++;
}
label3.setIcon( icon3 );
Border title3 = new TitledBorder("Circular Animation");
label3.setBorder( new CompoundBorder(title3, new EmptyBorder(0, 25, 0, 25)) );
add(panel, BorderLayout.WEST);
add(label2, BorderLayout.CENTER);
add(label3, BorderLayout.EAST);
icon1.start();
icon2.start();
icon3.start();
}
private JPanel createPanel(JLabel label, String title)
{
final AnimatedIcon icon = (AnimatedIcon)label.getIcon();
JPanel east = new JPanel( new GridLayout(0, 1) );
JButton start = new JButton("start");
start.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
icon.start();
}
});
east.add(start);
JButton restart = new JButton("restart");
restart.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
icon.restart();
}
});
east.add(restart);
JButton pause = new JButton("pause");
pause.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
icon.pause();
}
});
east.add(pause);
JButton stop = new JButton("stop");
stop.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
icon.stop();
}
});
east.add(stop);
JPanel panel = new JPanel( new BorderLayout(10, 0) );
panel.setBorder( new TitledBorder( title ) );
panel.add(east, BorderLayout.EAST);
panel.add(label);
return panel;
}
static class ColorIcon implements Icon
{
private Color color;
private int width;
private int height;
public ColorIcon(Color color, int width, int height)
{
this.color = color;
this.width = width;
this.height = height;
}
public int getIconWidth()
{
return width;
}
public int getIconHeight()
{
return height;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
g.setColor(color);
g.fillRect(x, y, width, height);
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI()
{
JFrame frame = new JFrame("Animated Icon");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new AnimatedIconDemo() );
frame.setSize(450, 180);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}