/Users/lyon/j4p/src/j2d/animation/AnimationPanel.java

1    package j2d.animation; 
2     
3    import gui.run.RunButton; 
4    import gui.run.RunSlider; 
5    import j2d.*; 
6     
7    import javax.swing.*; 
8    import java.awt.*; 
9     
10    
11   public class AnimationPanel extends JPanel { 
12    
13    
14       private int speedFps = 3; 
15       private boolean isRunning = false; 
16    
17       private Runnable r = null; 
18       private Image ia[] = null; 
19       private ImageSequenceProcessorDisplayController 
20               ispdc = null; 
21    
22       ImageProcessListener ipl = null; 
23    
24       public void openGifs() { 
25           ia = GifUtils.getGifs(); 
26           ispdc = new ImageSequenceProcessorDisplayController(ia); 
27       } 
28    
29       /**Construct the frame*/ 
30       public AnimationPanel(ImageProcessListener _ipl) { 
31           ipl = _ipl; 
32           initGuiElements(); 
33    
34       } 
35    
36       private JPanel getButtonPanel() { 
37           JPanel jp = new JPanel(); 
38           jp.setLayout(new FlowLayout()); 
39           jp.add(new RunButton("nextImage") { 
40               public void run() { 
41                   updateImage(); 
42               } 
43           }); 
44           jp.add(new RunButton("open gifs") { 
45               public void run() { 
46                   openGifs(); 
47               } 
48           }); 
49           jp.add(new RunButton("save gifs") { 
50               public void run() { 
51                   j2d.animation.GifUtils.writeGifStills(ia); 
52               } 
53           }); 
54           jp.add(new RunButton("Reset") { 
55               public void run() { 
56                   ipl.update(null); 
57               } 
58           }); 
59           jp.add(new RunButton("start/stop") { 
60               public void run() { 
61                   isRunning = !isRunning; 
62                   System.out.println("isRunning=" + isRunning); 
63                   if (isRunning) 
64                       r = new UpdateRunner(); 
65                   else 
66                       r = null; 
67               } 
68           }); 
69    
70           return jp; 
71       } 
72    
73    
74       /**Component initialization*/ 
75       private void initGuiElements() { 
76           RunSlider rs[] = { 
77               RunSlider.getLabeledSlider("speed", 
78                       new RunSlider(3, 40, 15) { 
79                           public void run() { 
80                               speedFps = getValue(); 
81                           } 
82                       })}; 
83    
84           JPanel labelPanel = new JPanel(); 
85           JPanel sliderPanel = new JPanel(); 
86           labelPanel.setLayout(new FlowLayout()); 
87           sliderPanel.setLayout(new FlowLayout()); 
88           for (int i = 0; i < rs.length; i++) { 
89               labelPanel.add(rs[i].getValueLabel()); 
90               sliderPanel.add(rs[i]); 
91           } 
92           setLayout(new BorderLayout()); 
93           add(labelPanel, BorderLayout.EAST); 
94           add(sliderPanel, BorderLayout.CENTER); 
95           add(getButtonPanel(), BorderLayout.SOUTH); 
96    
97       } 
98    
99       public JPanel getComparisonPanel(ImageProcessorFactory ipf) { 
100   
101          ImageBeanInterface origImagePanel = 
102                  (ImageBeanInterface) ipl; 
103          Image sourceImage = origImagePanel.getImage(); 
104          return ComparisonUtils.getComparisonPanel( 
105                  sourceImage, ipf); 
106   
107      } 
108   
109      public void updateImage(ImageProcessorInterface ip) { 
110          ipl.update(ip); 
111      } 
112   
113      public void updateImage() { 
114          if (ispdc == null) openGifs(); 
115          //ipl.update(ispdc); 
116          ipl.setImage(ispdc.process(null)); 
117      } 
118   
119      private class UpdateRunner implements Runnable { 
120          Thread t = new Thread(this); 
121   
122          public UpdateRunner() { 
123              t.start(); 
124          } 
125   
126          public void run() { 
127              while (isRunning) { 
128                  updateImage(); 
129                  try { 
130                      long l = (long) (1000.0 / speedFps); 
131                      Thread.sleep(l); 
132                      System.out.println("next image in:" + l); 
133                  } catch (InterruptedException e) { 
134                  } 
135              } 
136          } 
137      } 
138   
139   
140  } 
141