/Users/lyon/j4p/src/ip/gui/frames/AnimateFrame.java
|
1 package ip.gui.frames;
2
3 import futils.Ls;
4 import ip.gui.ImageSequence;
5
6 import java.awt.*;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.io.File;
10 import java.net.MalformedURLException;
11 import java.net.URL;
12
13 public class AnimateFrame
14 extends ShortCutFrame
15 implements ActionListener {
16 private String homeDir =
17 "http://www.docjava.com/java/images/";
18 private String roadImgs =
19 homeDir + "road.imgs";
20
21 private MenuBar menuBar = new MenuBar();
22 Menu fileMenu = getMenu("File");
23 Menu openMenu = getMenu("Open");
24 MenuItem save_mi
25 = addMenuItem(fileMenu, "save *.imgs...");
26 MenuItem open_mi
27 = addMenuItem(openMenu, "*.imgs...");
28 MenuItem openGifs_mi
29 = addMenuItem(openMenu, "*.gifs...");
30 MenuItem openRoadImgs_mi
31 = addMenuItem(openMenu, roadImgs);
32 MenuItem start_mi
33 = addMenuItem(fileMenu, "start");
34 MenuItem stop_mi
35 = addMenuItem(fileMenu, "stop");
36 MenuItem fhaar_mi
37 = addMenuItem(fileMenu, "forward haar");
38 MenuItem bhaar_mi
39 = addMenuItem(fileMenu, "backward haar");
40
41
42 public boolean running = true;
43 private ImageSequence imgSeq
44 = new ImageSequence();
45
46
47 public void setRunning(boolean t) {
48 running = t;
49 }
50
51
52
53 public void actionPerformed(ActionEvent e) {
54 Object c = e.getSource();
55
56 if (c == fhaar_mi) {
57 fhaar();
58 return;
59 }
60 if (c == bhaar_mi) {
61 bhaar();
62 return;
63 }
64 if (c == openRoadImgs_mi) {
65 openRoadImgs();
66 return;
67 }
68 if (c == save_mi) {
69 saveImages();
70 return;
71 }
72 if (c == open_mi) {
73 openImages();
74 return;
75 }
76 if (c == openGifs_mi) {
77 openGifs();
78 return;
79 }
80 if (c == start_mi) {
81 setRunning(true);
82 return;
83 }
84 if (c == stop_mi) {
85 setRunning(false);
86 return;
87 }
88
89 }
90
91 public AnimateFrame() {
92 super("AnimateFrame");
93 fileMenu.add(openMenu);
94 menuBar.add(fileMenu);
95 setMenuBar(menuBar);
96 setSize(64, 64);
97 }
98
99 public void fhaar() {
100 setRunning(false);
101 imgSeq.forwardHaar();
102 setRunning(true);
103 }
104
105 public void bhaar() {
106 imgSeq.backwardHaar();
107 }
108
109 public void saveImages() {
110 imgSeq.save();
111 }
112
113 public void openImages() {
114 imgSeq.open();
115 setVisible(true);
116 }
117
118 public void openRoadImgs() {
119 try {
120 imgSeq.open(
121 new URL(roadImgs));
122 } catch (MalformedURLException e) {
123 e.printStackTrace();
124 }
125 setSize(imgSeq.getWidth(), imgSeq.getHeight());
126 setVisible(true);
127 setRunning(true);
128 }
129
130 public void addImage(Image img) {
131 imgSeq.add(img);
132 imgSeq.setWidth(img.getWidth(this));
133 imgSeq.setHeight(img.getHeight(this));
134 }
135
136 public void update(Graphics g) {
137 paint(g);
138 }
139
140
141 public void paint(Graphics g) {
142 if (imgSeq.getSize() == 0) return;
143 if (!running) return;
144 Rectangle r = getBounds();
145 g.drawImage(imgSeq.next(), 0, 0, r.width, r.height,
146 this);
147 repaint(33);
148 }
149
150 void waitForImage(Component component,
151 Image image) {
152 MediaTracker tracker = new MediaTracker(component);
153 try {
154 tracker.addImage(image, 0);
155 tracker.waitForID(0);
156 if (!tracker.checkID(0))
157 System.out.println("Load failure!");
158 } catch (InterruptedException e) {
159 }
160 }
161
162 public void openGifs(File fileNames[]) {
163 Toolkit tk = Toolkit.getDefaultToolkit();
164 for (int i = 0; i < fileNames.length; i++) {
165 Image img = tk.getImage(fileNames[i].toString());
166 waitForImage(this, img);
167 addImage(img);
168 }
169 setVisible(true);
170 }
171
172 public void openGifs() {
173 File files[] = Ls.getWildFiles("gif");
174 openGifs(files);
175 setSize(imgSeq.getWidth(),
176 imgSeq.getHeight());
177 }
178
179 }
180