1 package demo.notification.whiteboard; 2 3 4 import java.awt.Color ; 5 import java.awt.image.MemoryImageSource ; 6 import java.awt.image.ImageProducer ; 7 8 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 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 44 public void setPixel(int x, int y, int red, int green, int blue) { 45 m_pixels[width * y + x] = ALPHA | (red << 16) | (green << 8) | blue; 47 } 48 49 56 public void setPixel(int x, int y, Color color) { 57 m_pixels[width * y + x] = color.getRGB(); 58 } 59 60 61 66 public ImageProducer getProducer() { 67 return new MemoryImageSource (width, height, m_pixels, 0, width); 68 } 69 70 75 public int[] getPixelBuffer() { 76 return m_pixels; 77 } 78 79 public void setPixelBuffer(int[] data) { 80 m_pixels = data; 81 } 82 83 89 90 public void drawLine(int x0,int y0, int x1, int y1, 91 int red,int green, int blue) { 92 if ((x0<0)||(x1<0)||(y1<0)||(y0<0)|| 95 (x0>=width)||(x1>=width)||(y0>=height)||(y1>=height)) { 96 return; 97 } 98 if ( (x0==x1) && (y0==y1) ) { 100 setPixel(x0,y0,red,green,blue); 101 } else { 102 float grad = ((float) (y1-y0))/ 104 ((float) (x1-x0)); 105 if ((grad >= -1.0)&&(grad <= 1.0)) { if (x0>x1) { 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 { grad = ((float) 1.0)/grad; 123 if (y0>y1) { 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 } | Popular Tags |