KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > instruments > Player


1 package JSci.instruments;
2
3 import java.awt.*;
4 import java.awt.image.*;
5 import javax.swing.*;
6 import javax.swing.border.*;
7 import java.text.*;
8
9 /** An object that creates an animated image from a ImageSource.
10 Create a new Player, register it with a ImageSource; then you can
11 add some ROIs. It displays a window with the image and a window with
12 all the controls of the chain.
13 */

14
15 public class Player extends JPanel implements ImageSink {
16
17     private MemoryImageSource imgsource = null;
18     private java.awt.Image JavaDoc img = null;
19     private Image currentImage = null;
20
21     /* source */
22     private ImageSource source = null;
23     public void setSource(ImageSource fs) {
24     if (source!=fs) {
25         source=fs;
26         source.setSink(this);
27     }
28     }
29
30     /* decimate frames */
31     private int decimationFrame = 0;
32     private int decimationNumber = 1;
33     /** The Player can decide to show only some frames.
34      * @param s only one frame over s will be displayed
35      */

36     public void setDecimationNumber(int s) { decimationNumber=s; }
37
38     /* statistics */
39     private long receivedFrames = 0;
40     private long displayedFrames = 0;
41     private long lastReceivedFrames = 0;
42     private long lastDisplayedFrames = 0;
43     private long last = System.currentTimeMillis();
44     private JLabel receivedFramesLabel;
45     private JLabel displayedFramesLabel;
46     private boolean newImagePresent=false;
47     private static NumberFormat formatter = NumberFormat.getNumberInstance();
48     { formatter.setMaximumFractionDigits(1);
49     formatter.setMinimumFractionDigits(1);
50     formatter.setMinimumIntegerDigits(1);
51     formatter.setMaximumIntegerDigits(3);
52     }
53
54     public void runStatisticsUpdate() {
55     long nowReceivedFrames;
56     long nowDisplayedFrames;
57     long now;
58     while (true) {
59         nowReceivedFrames = receivedFrames;
60         nowDisplayedFrames = displayedFrames;
61         now = System.currentTimeMillis();
62         
63         receivedFramesLabel.setText("received: "+
64                     formatter.format(
65                              (lastReceivedFrames - nowReceivedFrames)*
66                              1000.0/
67                              (last-now)
68                              )+
69                     " Hz"
70                     );
71         displayedFramesLabel.setText("displayed: "+
72                      formatter.format(
73                               (lastDisplayedFrames - nowDisplayedFrames)*
74                               1000.0/
75                               (last-now)
76                               )+
77                      " Hz"
78                      );
79
80         lastReceivedFrames = nowReceivedFrames;
81         lastDisplayedFrames = nowDisplayedFrames;
82         last = now;
83
84         try { Thread.sleep(1000); }
85         catch (InterruptedException JavaDoc e) {}
86     }
87     }
88
89
90     /* interfaces implementation */
91     public void receive(Image f) {
92     receivedFrames++;
93     currentImage=f;
94     decimationFrame++;
95     if (decimationFrame==decimationNumber) decimationFrame=0;
96     if (imgsource == null) {
97         setSize(f.getSize());
98         imgsource = new MemoryImageSource(f.getWidth(),f.getHeight(),f.getColorModel(),f.getData(),f.getOffset(),f.getScansize());
99         imgsource.setAnimated(true);
100         img = createImage(imgsource);
101     }
102     else if (decimationFrame==0) {
103         imgsource.newPixels(f.getData(),f.getColorModel(),f.getOffset(),f.getScansize());
104         newImagePresent=true;
105     }
106     }
107
108     public void start() {
109     while (imgsource == null) { //TODO: wait
110
try { Thread.sleep(100); }
111         catch (InterruptedException JavaDoc e) {}
112     }
113     JFrame f = new JFrame("Player");
114     f.setSize(getSize().width+10,getSize().height+30); //TODO: +10, +30
115
f.getContentPane().add(this);
116     f.setResizable(false);
117     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
118     f.setVisible(true);
119     JFrame c = new JFrame("Controls");
120     c.getContentPane().add(getControlComponent());
121     c.pack();
122     //c.setResizable(false);
123
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
124     c.setVisible(true);
125     }
126
127     ROI r = null; //TODO: use a list of ROIs
128
public void addROI(ROI r) {
129     this.r=r;
130     r.setComponent(this);
131     }
132
133     public void paintComponent(Graphics g) {
134     super.paintComponent(g);
135     if (img!=null) g.drawImage(img,0,0,this);
136     if (newImagePresent) {
137         displayedFrames++;
138         newImagePresent=false;
139     }
140     if (currentImage!=null)
141         currentImage.doOverlay(g);
142     if (r!=null) r.paint(g);
143     }
144     
145     /** @return the component that controls the object that implements
146     this interface */

147     public Component getControlComponent() {
148     JPanel t = new JPanel();
149     t.setLayout(new FlowLayout());
150     if (source.getControlComponent()!=null)
151         t.add(source.getControlComponent());
152     JPanel s = new JPanel();
153     s.setLayout(new BorderLayout());
154     receivedFramesLabel=new JLabel("received: 000.0 Hz");
155     displayedFramesLabel=new JLabel("displayed: 000.0 Hz");
156     Thread JavaDoc thrd = new Thread JavaDoc(new Runnable JavaDoc() {
157         public void run() { Player.this.runStatisticsUpdate(); }
158         });
159     thrd.setDaemon(true);
160     thrd.start();
161     s.add(BorderLayout.NORTH,receivedFramesLabel);
162     s.add(BorderLayout.SOUTH,displayedFramesLabel);
163     Border etched = BorderFactory.createEtchedBorder();
164     Border titled = BorderFactory.createTitledBorder(etched,"player");
165     s.setBorder(titled);
166     t.add(s);
167     return t;
168     }
169
170 }
171
172
173
174
175
176
177
178
Popular Tags