KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > notification > whiteboard > PixelImage


1 package demo.notification.whiteboard;
2
3
4 import java.awt.Color JavaDoc;
5 import java.awt.image.MemoryImageSource JavaDoc;
6 import java.awt.image.ImageProducer JavaDoc;
7
8 /**
9  * PixelImage.java
10  * A pixel-based image.
11  *
12  *
13  * @author Nicolas Noffke, Torsten Fink
14  */

15
16 public class PixelImage {
17     private static final int ALPHA = 255 << 24;
18     protected int[] m_pixels;
19     private int width;
20     private int height;
21
22     /**
23       The constructor. Set up buffer.
24
25       @param height the images height.
26       @param width the images width.
27      */

28     public PixelImage(int width, int height) {
29         m_pixels = new int[width * height];
30         this.width=width;
31         this.height=height;
32         clearAll();
33     }
34
35     /**
36      * sets a pixel to a specific color.
37      *
38      * @param x the pixels x value.
39      * @param y the pixels y value.
40      * @param red the pixels red value.
41      * @param green the pixels green value.
42      * @param blue the pixels blue value.
43      */

44     public void setPixel(int x, int y, int red, int green, int blue) {
45         //ALPHA is needed, otherwise, if left 0, the pixel gets transparent.
46
m_pixels[width * y + x] = ALPHA | (red << 16) | (green << 8) | blue;
47     }
48
49     /**
50      * sets a pixel to a specific color.
51      *
52      * @param x the pixels x value.
53      * @param y the pixels y value.
54      * @param color the pixels color.
55      */

56     public void setPixel(int x, int y, Color JavaDoc color) {
57         m_pixels[width * y + x] = color.getRGB();
58     }
59
60
61     /**
62      * gets the ImageProducer for this image.
63      *
64      * @return the ImageProducer for this image.
65      */

66     public ImageProducer JavaDoc getProducer() {
67         return new MemoryImageSource JavaDoc(width, height, m_pixels, 0, width);
68     }
69
70     /**
71      * gets the pixel buffer of this image.
72      *
73      * @return the pixel buffer.
74      */

75     public int[] getPixelBuffer() {
76         return m_pixels;
77     }
78
79     public void setPixelBuffer(int[] data) {
80     m_pixels = data;
81     }
82
83     /**
84        draws a line in the image. The incremental line scan-conversion
85        algorithm is used (see "Computer Graphics"; Foley, vanDam,Feiner,Hughes).
86        (x0,y0) is the starting point, (x1,y1) the ending point.
87
88      */

89
90     public void drawLine(int x0,int y0, int x1, int y1,
91                          int red,int green, int blue) {
92         //System.out.println("Draw: ("+x0+","+y0+") ("+x1+","+y1+")");
93
// do some clipping
94
if ((x0<0)||(x1<0)||(y1<0)||(y0<0)||
95                 (x0>=width)||(x1>=width)||(y0>=height)||(y1>=height)) {
96             return;
97         }
98         // parameters are ok
99
if ( (x0==x1) && (y0==y1) ) {
100             setPixel(x0,y0,red,green,blue);
101         } else {
102             float grad = // cast is necessary
103
((float) (y1-y0))/
104                 ((float) (x1-x0));
105             if ((grad >= -1.0)&&(grad <= 1.0)) { // loop over x
106
if (x0>x1) { // change points
107
int change = x1;
108                     x1 = x0;
109                     x0 = change;
110                     change = y1;
111                     y1 = y0 ;
112                     y0=change;
113                 }
114                 for(float y= (float)y0;
115                         x0<=x1;
116                         x0++) {
117                     setPixel(x0,(int) (y+0.5),
118                              red,green,blue);
119                     y+= grad;
120                 }
121             } else { // loop over y
122
grad = ((float) 1.0)/grad;
123                 if (y0>y1) { // change points
124
int change = x1;
125                     x1 = x0;
126                     x0 = change;
127                     change = y1;
128                     y1 = y0 ;
129                     y0=change;
130                 }
131                 for(float x= (float)x0;
132                         y0<=y1;
133                         y0++,x+=grad) {
134                     setPixel((int) (x+0.5),y0,
135                              red,green,blue);
136                 }
137             }
138         }
139     }
140
141     public void clearAll() {
142         for(int x=0;x<width;x++)
143             for(int y=0;y<height;y++)
144                 setPixel(x,y,0,0,0);
145     }
146
147
148 } // PixelImage
149
Popular Tags