001: package demo.notification.whiteboard;
002:
003: import java.awt.Color;
004: import java.awt.image.MemoryImageSource;
005: import java.awt.image.ImageProducer;
006:
007: /**
008: * PixelImage.java
009: * A pixel-based image.
010: *
011: *
012: * @author Nicolas Noffke, Torsten Fink
013: */
014:
015: public class PixelImage {
016: private static final int ALPHA = 255 << 24;
017: protected int[] m_pixels;
018: private int width;
019: private int height;
020:
021: /**
022: The constructor. Set up buffer.
023:
024: @param height the images height.
025: @param width the images width.
026: */
027: public PixelImage(int width, int height) {
028: m_pixels = new int[width * height];
029: this .width = width;
030: this .height = height;
031: clearAll();
032: }
033:
034: /**
035: * sets a pixel to a specific color.
036: *
037: * @param x the pixels x value.
038: * @param y the pixels y value.
039: * @param red the pixels red value.
040: * @param green the pixels green value.
041: * @param blue the pixels blue value.
042: */
043: public void setPixel(int x, int y, int red, int green, int blue) {
044: //ALPHA is needed, otherwise, if left 0, the pixel gets transparent.
045: m_pixels[width * y + x] = ALPHA | (red << 16) | (green << 8)
046: | blue;
047: }
048:
049: /**
050: * sets a pixel to a specific color.
051: *
052: * @param x the pixels x value.
053: * @param y the pixels y value.
054: * @param color the pixels color.
055: */
056: public void setPixel(int x, int y, Color color) {
057: m_pixels[width * y + x] = color.getRGB();
058: }
059:
060: /**
061: * gets the ImageProducer for this image.
062: *
063: * @return the ImageProducer for this image.
064: */
065: public ImageProducer getProducer() {
066: return new MemoryImageSource(width, height, m_pixels, 0, width);
067: }
068:
069: /**
070: * gets the pixel buffer of this image.
071: *
072: * @return the pixel buffer.
073: */
074: public int[] getPixelBuffer() {
075: return m_pixels;
076: }
077:
078: public void setPixelBuffer(int[] data) {
079: m_pixels = data;
080: }
081:
082: /**
083: draws a line in the image. The incremental line scan-conversion
084: algorithm is used (see "Computer Graphics"; Foley, vanDam,Feiner,Hughes).
085: (x0,y0) is the starting point, (x1,y1) the ending point.
086:
087: */
088:
089: public void drawLine(int x0, int y0, int x1, int y1, int red,
090: int green, int blue) {
091: //System.out.println("Draw: ("+x0+","+y0+") ("+x1+","+y1+")");
092: // do some clipping
093: if ((x0 < 0) || (x1 < 0) || (y1 < 0) || (y0 < 0)
094: || (x0 >= width) || (x1 >= width) || (y0 >= height)
095: || (y1 >= height)) {
096: return;
097: }
098: // parameters are ok
099: if ((x0 == x1) && (y0 == y1)) {
100: setPixel(x0, y0, red, green, blue);
101: } else {
102: float grad = // cast is necessary
103: ((float) (y1 - y0)) / ((float) (x1 - x0));
104: if ((grad >= -1.0) && (grad <= 1.0)) { // loop over x
105: if (x0 > x1) { // change points
106: int change = x1;
107: x1 = x0;
108: x0 = change;
109: change = y1;
110: y1 = y0;
111: y0 = change;
112: }
113: for (float y = (float) y0; x0 <= x1; x0++) {
114: setPixel(x0, (int) (y + 0.5), red, green, blue);
115: y += grad;
116: }
117: } else { // loop over y
118: grad = ((float) 1.0) / grad;
119: if (y0 > y1) { // change points
120: int change = x1;
121: x1 = x0;
122: x0 = change;
123: change = y1;
124: y1 = y0;
125: y0 = change;
126: }
127: for (float x = (float) x0; y0 <= y1; y0++, x += grad) {
128: setPixel((int) (x + 0.5), y0, red, green, blue);
129: }
130: }
131: }
132: }
133:
134: public void clearAll() {
135: for (int x = 0; x < width; x++)
136: for (int y = 0; y < height; y++)
137: setPixel(x, y, 0, 0, 0);
138: }
139:
140: } // PixelImage
|