1 package JSci.instruments; 2 3 import java.awt.*; 4 import javax.swing.*; 5 import javax.swing.border.*; 6 7 8 9 public class TestImageSource implements ImageSource,Runnable { 10 11 Dimension dim = new Dimension(130,97); 12 13 public TestImageSource() { 14 Thread t = new Thread (this); 15 t.setDaemon(true); 16 t.start(); 17 } 18 19 private ImageSink sink; 20 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 e) {} 54 } 55 } 56 57 58 public int getWidth() { return dim.width; } 59 60 61 public int getHeight() { return dim.height; } 62 63 64 public Dimension getSize() { return dim; } 65 66 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 |