1 11 package org.eclipse.swt.graphics; 12 13 import org.eclipse.swt.*; 14 import org.eclipse.swt.internal.gdip.*; 15 import org.eclipse.swt.internal.win32.*; 16 17 32 public class Pattern extends Resource { 33 34 44 public int handle; 45 46 71 public Pattern(Device device, Image image) { 72 if (device == null) device = Device.getDevice(); 73 if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 74 if (image == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 75 if (image.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 76 this.device = device; 77 device.checkGDIP(); 78 int[] gdipImage = image.createGdipImage(); 79 int img = gdipImage[0]; 80 int width = Gdip.Image_GetWidth(img); 81 int height = Gdip.Image_GetHeight(img); 82 handle = Gdip.TextureBrush_new(img, Gdip.WrapModeTile, 0, 0, width, height); 83 Gdip.Bitmap_delete(img); 84 if (gdipImage[1] != 0) { 85 int hHeap = OS.GetProcessHeap (); 86 OS.HeapFree(hHeap, 0, gdipImage[1]); 87 } 88 if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES); 89 if (device.tracking) device.new_Object(this); 90 } 91 92 124 public Pattern(Device device, float x1, float y1, float x2, float y2, Color color1, Color color2) { 125 this(device, x1, y1, x2, y2, color1, 0xFF, color2, 0xFF); 126 } 127 128 164 public Pattern(Device device, float x1, float y1, float x2, float y2, Color color1, int alpha1, Color color2, int alpha2) { 165 if (device == null) device = Device.getDevice(); 166 if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 167 if (color1 == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 168 if (color1.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 169 if (color2 == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 170 if (color2.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 171 this.device = device; 172 device.checkGDIP(); 173 int colorRef1 = color1.handle; 174 int rgb = ((colorRef1 >> 16) & 0xFF) | (colorRef1 & 0xFF00) | ((colorRef1 & 0xFF) << 16); 175 int foreColor = Gdip.Color_new((alpha1 & 0xFF) << 24 | rgb); 176 if (x1 == x2 && y1 == y2) { 177 handle = Gdip.SolidBrush_new(foreColor); 178 if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES); 179 } else { 180 int colorRef2 = color2.handle; 181 rgb = ((colorRef2 >> 16) & 0xFF) | (colorRef2 & 0xFF00) | ((colorRef2 & 0xFF) << 16); 182 int backColor = Gdip.Color_new((alpha2 & 0xFF) << 24 | rgb); 183 PointF p1 = new PointF(); 184 p1.X = x1; 185 p1.Y = y1; 186 PointF p2 = new PointF(); 187 p2.X = x2; 188 p2.Y = y2; 189 handle = Gdip.LinearGradientBrush_new(p1, p2, foreColor, backColor); 190 if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES); 191 if (alpha1 != 0xFF || alpha2 != 0xFF) { 192 int a = (int)((alpha1 & 0xFF) * 0.5f + (alpha2 & 0xFF) * 0.5f); 193 int r = (int)(((colorRef1 & 0xFF) >> 0) * 0.5f + ((colorRef2 & 0xFF) >> 0) * 0.5f); 194 int g = (int)(((colorRef1 & 0xFF00) >> 8) * 0.5f + ((colorRef2 & 0xFF00) >> 8) * 0.5f); 195 int b = (int)(((colorRef1 & 0xFF0000) >> 16) * 0.5f + ((colorRef2 & 0xFF0000) >> 16) * 0.5f); 196 int midColor = Gdip.Color_new(a << 24 | r << 16 | g << 8 | b); 197 Gdip.LinearGradientBrush_SetInterpolationColors(handle, new int[]{foreColor, midColor, backColor}, new float[]{0, 0.5f, 1}, 3); 198 Gdip.Color_delete(midColor); 199 } 200 Gdip.Color_delete(backColor); 201 } 202 Gdip.Color_delete(foreColor); 203 if (device.tracking) device.new_Object(this); 204 } 205 206 211 public void dispose() { 212 if (handle == 0) return; 213 if (device.isDisposed()) return; 214 int type = Gdip.Brush_GetType(handle); 215 switch (type) { 216 case Gdip.BrushTypeSolidColor: 217 Gdip.SolidBrush_delete(handle); 218 break; 219 case Gdip.BrushTypeHatchFill: 220 Gdip.HatchBrush_delete(handle); 221 break; 222 case Gdip.BrushTypeLinearGradient: 223 Gdip.LinearGradientBrush_delete(handle); 224 break; 225 case Gdip.BrushTypeTextureFill: 226 Gdip.TextureBrush_delete(handle); 227 break; 228 } 229 handle = 0; 230 if (device.tracking) device.dispose_Object(this); 231 device = null; 232 } 233 234 244 public boolean isDisposed() { 245 return handle == 0; 246 } 247 248 254 public String toString() { 255 if (isDisposed()) return "Pattern {*DISPOSED*}"; 256 return "Pattern {" + handle + "}"; 257 } 258 259 } 260 | Popular Tags |