KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > instruments > TestImageSource


1 package JSci.instruments;
2
3 import java.awt.*;
4 import javax.swing.*;
5 import javax.swing.border.*;
6
7 /** An object that delivers sample frames */
8
9 public class TestImageSource implements ImageSource,Runnable JavaDoc {
10
11     Dimension dim = new Dimension(130,97);
12
13     public TestImageSource() {
14     Thread JavaDoc t = new Thread JavaDoc(this);
15     t.setDaemon(true);
16     t.start();
17     }
18
19     private ImageSink sink;
20     /** set the object that must read the frames through receive() method.
21      * @param fs the object that reads the frames
22      */

23     public void setSink(ImageSink fs) {
24     if (sink!=fs) {
25         sink=fs;
26         sink.setSource(this);
27     }
28     }
29
30     public void run() {
31     int num = 0;
32     while (true) {
33         final int n = num++;
34         final long t = System.currentTimeMillis();
35         final byte[] im = new byte[getWidth()*getHeight()];
36         final Dimension dim = new Dimension(getWidth(),getHeight());
37         for (int j=0;j<getWidth();j++)
38         for (int k=0;k<getHeight();k++)
39             im[j+k*getWidth()] = (byte) ((j+n)%256);
40         Image f = new Image() {
41             public byte[] getData() { return im; };
42             public Dimension getSize() { return dim; }
43             public long getTimeStamp() { return t; }
44         };
45         f.addOverlay(new Overlay() {
46             public void paint(Graphics g) {
47             g.setColor(Color.MAGENTA);
48             ((Graphics2D)g).draw(new Rectangle(30+n%10,34,40,40));
49             }
50         });
51         if (sink!=null) sink.receive(f);
52         try { Thread.sleep(100); }
53         catch (InterruptedException JavaDoc e) {}
54     }
55     }
56
57     /** @return the width of the image */
58     public int getWidth() { return dim.width; }
59     
60     /** @return the height of the image */
61     public int getHeight() { return dim.height; }
62     
63     /** @return the dimension of the image */
64     public Dimension getSize() { return dim; }
65
66     /** @return the component that controls the object that implements
67     this interface */

68     public Component getControlComponent() {
69     JPanel p = new JPanel();
70     p.add(new JLabel("Test"));
71     Border etched = BorderFactory.createEtchedBorder();
72     Border titled = BorderFactory.createTitledBorder(etched,"source");
73     p.setBorder(titled);
74     return p;
75     }
76
77 }
78
79
80
81
82
Popular Tags