KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > instruments > Image


1 package JSci.instruments;
2
3 import java.awt.*;
4 import java.awt.image.*;
5 import java.util.*;
6
7 /** Describes a frame, that holds the informations to access an image */
8
9 public abstract class Image implements Dimensions {
10
11     /** @return the width of the image */
12     public int getWidth() { return getSize().width; }
13
14     /** @return the height of the image */
15     public int getHeight() { return getSize().height; }
16
17     /** @return the dimension of the image */
18     public abstract Dimension getSize();
19
20     /** @return the scansize of the image in the getData() */
21     public int getScansize() { return getSize().width; }
22
23     /** @return the offset of the image in the getData() */
24     public int getOffset() { return 0; }
25
26     private static ColorModel cm;
27     static {
28     byte [] r = new byte[256];
29     byte [] g = new byte[256];
30     byte [] b = new byte[256];
31     for (int j=0;j<256;j++) r[j]=g[j]=b[j]=(byte)j;
32     cm = new IndexColorModel(8,256,r,g,b);
33     }
34     /** @return the ColorModel */
35     public ColorModel getColorModel() { return cm; }
36
37     /** @return the data */
38     public abstract byte[] getData();
39
40     /** @return the time in milliseconds */
41     public long getTimeStamp() { return 0; }
42
43     /* Overlay */
44     private ArrayList ovrl = new ArrayList();
45     
46     /** Images (poligons and text) can be overlaid onto the images.
47      * This method is called to actually paint the overlays.
48      * @param g the graphical environment on which we want to paint
49      */

50     public void doOverlay(Graphics g) {
51     for (int j=0;j<ovrl.size();j++) ((Overlay)(ovrl.get(j))).paint(g) ;
52     }
53
54     /** Images (poligons and text) can be overlaid onto the images
55      * @param o the objects that must be paint over the image
56      */

57     public void addOverlay(Overlay o) { ovrl.add(o); }
58
59     /** Creates a new image, from a part of this.
60      * @param r the part that we want to extract
61      */

62     public Image getSubImage(Rectangle r) {
63     final int w=r.width;
64     final int h=r.height;
65     final byte[] b=new byte[w*h];
66     for (int j=0;j<w;j++) for (int k=0;k<h;k++)
67         b[j+k*w]=getData()[getOffset()+(r.x+j)+(r.y+k)*getScansize()];
68     return new Image() {
69         public int getWidth() { return w; }
70         public int getHeight() { return h; }
71         public Dimension getSize() { return new Dimension(w,h); }
72         public int getScansize() { return w; }
73         public int getOffset() { return 0; }
74         public byte[] getData() { return b; }
75         };
76     }
77    
78
79 }
80
81
82
Popular Tags