KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > graphics > Pattern


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
18  * Instances of this class represent patterns to use while drawing. Patterns
19  * can be specified either as bitmaps or gradients.
20  * <p>
21  * Application code must explicitly invoke the <code>Pattern.dispose()</code>
22  * method to release the operating system resources managed by each instance
23  * when those instances are no longer required.
24  * </p>
25  * <p>
26  * This class requires the operating system's advanced graphics subsystem
27  * which may not be available on some platforms.
28  * </p>
29  *
30  * @since 3.1
31  */

32 public class Pattern extends Resource {
33
34     /**
35      * the OS resource for the Pattern
36      * (Warning: This field is platform dependent)
37      * <p>
38      * <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT
39      * public API. It is marked public only so that it can be shared
40      * within the packages provided by SWT. It is not available on all
41      * platforms and should never be accessed from application code.
42      * </p>
43      */

44     public int handle;
45
46 /**
47  * Constructs a new Pattern given an image. Drawing with the resulting
48  * pattern will cause the image to be tiled over the resulting area.
49  * <p>
50  * This operation requires the operating system's advanced
51  * graphics subsystem which may not be available on some
52  * platforms.
53  * </p>
54  *
55  * @param device the device on which to allocate the pattern
56  * @param image the image that the pattern will draw
57  *
58  * @exception IllegalArgumentException <ul>
59  * <li>ERROR_NULL_ARGUMENT - if the device is null and there is no current device, or the image is null</li>
60  * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li>
61  * </ul>
62  * @exception SWTException <ul>
63  * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li>
64  * </ul>
65  * @exception SWTError <ul>
66  * <li>ERROR_NO_HANDLES if a handle for the pattern could not be obtained</li>
67  * </ul>
68  *
69  * @see #dispose()
70  */

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 /**
93  * Constructs a new Pattern that represents a linear, two color
94  * gradient. Drawing with the pattern will cause the resulting area to be
95  * tiled with the gradient specified by the arguments.
96  * <p>
97  * This operation requires the operating system's advanced
98  * graphics subsystem which may not be available on some
99  * platforms.
100  * </p>
101  *
102  * @param device the device on which to allocate the pattern
103  * @param x1 the x coordinate of the starting corner of the gradient
104  * @param y1 the y coordinate of the starting corner of the gradient
105  * @param x2 the x coordinate of the ending corner of the gradient
106  * @param y2 the y coordinate of the ending corner of the gradient
107  * @param color1 the starting color of the gradient
108  * @param color2 the ending color of the gradient
109  *
110  * @exception IllegalArgumentException <ul>
111  * <li>ERROR_NULL_ARGUMENT - if the device is null and there is no current device,
112  * or if either color1 or color2 is null</li>
113  * <li>ERROR_INVALID_ARGUMENT - if either color1 or color2 has been disposed</li>
114  * </ul>
115  * @exception SWTException <ul>
116  * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li>
117  * </ul>
118  * @exception SWTError <ul>
119  * <li>ERROR_NO_HANDLES if a handle for the pattern could not be obtained</li>
120  * </ul>
121  *
122  * @see #dispose()
123  */

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 /**
129  * Constructs a new Pattern that represents a linear, two color
130  * gradient. Drawing with the pattern will cause the resulting area to be
131  * tiled with the gradient specified by the arguments.
132  * <p>
133  * This operation requires the operating system's advanced
134  * graphics subsystem which may not be available on some
135  * platforms.
136  * </p>
137  *
138  * @param device the device on which to allocate the pattern
139  * @param x1 the x coordinate of the starting corner of the gradient
140  * @param y1 the y coordinate of the starting corner of the gradient
141  * @param x2 the x coordinate of the ending corner of the gradient
142  * @param y2 the y coordinate of the ending corner of the gradient
143  * @param color1 the starting color of the gradient
144  * @param alpha1 the starting alpha value of the gradient
145  * @param color2 the ending color of the gradient
146  * @param alpha2 the ending alpha value of the gradient
147  *
148  * @exception IllegalArgumentException <ul>
149  * <li>ERROR_NULL_ARGUMENT - if the device is null and there is no current device,
150  * or if either color1 or color2 is null</li>
151  * <li>ERROR_INVALID_ARGUMENT - if either color1 or color2 has been disposed</li>
152  * </ul>
153  * @exception SWTException <ul>
154  * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li>
155  * </ul>
156  * @exception SWTError <ul>
157  * <li>ERROR_NO_HANDLES if a handle for the pattern could not be obtained</li>
158  * </ul>
159  *
160  * @see #dispose()
161  *
162  * @since 3.2
163  */

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 /**
207  * Disposes of the operating system resources associated with
208  * the Pattern. Applications must dispose of all Patterns that
209  * they allocate.
210  */

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 /**
235  * Returns <code>true</code> if the Pattern has been disposed,
236  * and <code>false</code> otherwise.
237  * <p>
238  * This method gets the dispose state for the Pattern.
239  * When a Pattern has been disposed, it is an error to
240  * invoke any other method using the Pattern.
241  *
242  * @return <code>true</code> when the Pattern is disposed, and <code>false</code> otherwise
243  */

244 public boolean isDisposed() {
245     return handle == 0;
246 }
247
248 /**
249  * Returns a string containing a concise, human-readable
250  * description of the receiver.
251  *
252  * @return a string representation of the receiver
253  */

254 public String JavaDoc toString() {
255     if (isDisposed()) return "Pattern {*DISPOSED*}";
256     return "Pattern {" + handle + "}";
257 }
258     
259 }
260
Popular Tags