?? image.java
字號:
/******************************************************************************* * Copyright (c) 2000, 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.swt.graphics;import org.eclipse.swt.internal.win32.*;import org.eclipse.swt.*;import java.io.*;/** * Instances of this class are graphics which have been prepared * for display on a specific device. That is, they are ready * to paint using methods such as <code>GC.drawImage()</code> * and display on widgets with, for example, <code>Button.setImage()</code>. * <p> * If loaded from a file format that supports it, an * <code>Image</code> may have transparency, meaning that certain * pixels are specified as being transparent when drawn. Examples * of file formats that support transparency are GIF and PNG. * </p><p> * There are two primary ways to use <code>Images</code>. * The first is to load a graphic file from disk and create an * <code>Image</code> from it. This is done using an <code>Image</code> * constructor, for example: * <pre> * Image i = new Image(device, "C:\\graphic.bmp"); * </pre> * A graphic file may contain a color table specifying which * colors the image was intended to possess. In the above example, * these colors will be mapped to the closest available color in * SWT. It is possible to get more control over the mapping of * colors as the image is being created, using code of the form: * <pre> * ImageData data = new ImageData("C:\\graphic.bmp"); * RGB[] rgbs = data.getRGBs(); * // At this point, rgbs contains specifications of all * // the colors contained within this image. You may * // allocate as many of these colors as you wish by * // using the Color constructor Color(RGB), then * // create the image: * Image i = new Image(device, data); * </pre> * <p> * Applications which require even greater control over the image * loading process should use the support provided in class * <code>ImageLoader</code>. * </p><p> * Application code must explicitely invoke the <code>Image.dispose()</code> * method to release the operating system resources managed by each instance * when those instances are no longer required. * </p> * * @see Color * @see ImageData * @see ImageLoader */public final class Image implements Drawable { /** * specifies whether the receiver is a bitmap or an icon * (one of <code>SWT.BITMAP</code>, <code>SWT.ICON</code>) */ public int type; /** * the handle to the OS image resource * (Warning: This field is platform dependent) */ public int handle; /** * the device where this image was created */ Device device; /** * specifies the transparent pixel * (Warning: This field is platform dependent) */ int transparentPixel = -1; /** * the GC which is drawing on the image * (Warning: This field is platform dependent) */ GC memGC; /** * the alpha data for the image * (Warning: This field is platform dependent) */ byte[] alphaData; /** * the global alpha value to be used for every pixel * (Warning: This field is platform dependent) */ int alpha = -1; /** * the image data used to create this image if it is a * icon. Used only in WinCE * (Warning: This field is platform dependent) */ ImageData data; /** * specifies the default scanline padding * (Warning: This field is platform dependent) */ static final int DEFAULT_SCANLINE_PAD = 4;/** * Prevents uninitialized instances from being created outside the package. */Image () {}/** * Constructs an empty instance of this class with the * specified width and height. The result may be drawn upon * by creating a GC and using any of its drawing operations, * as shown in the following example: * <pre> * Image i = new Image(device, width, height); * GC gc = new GC(i); * gc.drawRectangle(0, 0, 50, 50); * gc.dispose(); * </pre> * <p> * Note: Some platforms may have a limitation on the size * of image that can be created (size depends on width, height, * and depth). For example, Windows 95, 98, and ME do not allow * images larger than 16M. * </p> * * @param device the device on which to create the image * @param width the width of the new image * @param height the height of the new image * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> * <li>ERROR_INVALID_ARGUMENT - if either the width or height is negative or zero</li> * </ul> * @exception SWTError <ul> * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li> * </ul> */public Image(Device device, int width, int height) { if (device == null) device = Device.getDevice(); if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); init(device, width, height); if (device.tracking) device.new_Object(this); }/** * Constructs a new instance of this class based on the * provided image, with an appearance that varies depending * on the value of the flag. The possible flag values are: * <dl> * <dt><b>IMAGE_COPY</b></dt> * <dd>the result is an identical copy of srcImage</dd> * <dt><b>IMAGE_DISABLE</b></dt> * <dd>the result is a copy of srcImage which has a <em>disabled</em> look</dd> * <dt><b>IMAGE_GRAY</b></dt> * <dd>the result is a copy of srcImage which has a <em>gray scale</em> look</dd> * </dl> * * @param device the device on which to create the image * @param srcImage the image to use as the source * @param flag the style, either <code>IMAGE_COPY</code>, <code>IMAGE_DISABLE</code> or <code>IMAGE_GRAY</code> * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> * <li>ERROR_NULL_ARGUMENT - if srcImage is null</li> * <li>ERROR_INVALID_ARGUMENT - if the flag is not one of <code>IMAGE_COPY</code>, <code>IMAGE_DISABLE</code> or <code>IMAGE_GRAY</code></li> * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li> * </ul> * @exception SWTException <ul> * <li>ERROR_INVALID_IMAGE - if the image is not a bitmap or an icon, or * is otherwise in an invalid state</li> * <li>ERROR_UNSUPPORTED_DEPTH - if the depth of the Image is not supported</li> * </ul> * @exception SWTError <ul> * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li> * </ul> */public Image(Device device, Image srcImage, int flag) { if (device == null) device = Device.getDevice(); if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); this.device = device; if (srcImage == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if (srcImage.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); switch (flag) { case SWT.IMAGE_COPY: { Rectangle r = srcImage.getBounds(); this.type = srcImage.type; switch (type) { case SWT.BITMAP: /* Get the HDC for the device */ int hDC = device.internal_new_GC(null); /* Copy the bitmap */ int hdcSource = OS.CreateCompatibleDC(hDC); int hdcDest = OS.CreateCompatibleDC(hDC); int hOldSrc = OS.SelectObject(hdcSource, srcImage.handle); handle = OS.CreateCompatibleBitmap(hdcSource, r.width, r.height); if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES); int hOldDest = OS.SelectObject(hdcDest, handle); OS.BitBlt(hdcDest, 0, 0, r.width, r.height, hdcSource, 0, 0, OS.SRCCOPY); OS.SelectObject(hdcSource, hOldSrc); OS.SelectObject(hdcDest, hOldDest); OS.DeleteDC(hdcSource); OS.DeleteDC(hdcDest); /* Release the HDC for the device */ device.internal_dispose_GC(hDC, null); transparentPixel = srcImage.transparentPixel; alpha = srcImage.alpha; if (srcImage.alphaData != null) { alphaData = new byte[srcImage.alphaData.length]; System.arraycopy(srcImage.alphaData, 0, alphaData, 0, alphaData.length); } break; case SWT.ICON: if (OS.IsWinCE) { init(device, srcImage.data); } else { handle = OS.CopyImage(srcImage.handle, OS.IMAGE_ICON, r.width, r.height, 0); if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES); } break; default: SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT); } if (device.tracking) device.new_Object(this); return; } case SWT.IMAGE_DISABLE: { Rectangle r = srcImage.getBounds(); this.type = srcImage.type; byte[] rgbBwBitmapInfo = { 40,0,0,0, /* biSize */ (byte)(r.width & 0xFF), /* biWidth */ (byte)((r.width & 0xFF00) >> 8), (byte)((r.width & 0xFF0000) >> 16), (byte)((r.width & 0xFF000000) >> 24), (byte)(r.height & 0xFF), /* biHeight */ (byte)((r.height & 0xFF00) >> 8), (byte)((r.height & 0xFF0000) >> 16), (byte)((r.height & 0xFF000000) >> 24), 1,0, /* biPlanes */ 1,0, /* biBitCount */ 0,0,0,0, /* biCompression */ 0,0,0,0, /* biSizeImage */ 0,0,0,0, /* biXPelsPerMeter */ 0,0,0,0, /* biYPelsPerMeter */ 0,0,0,0, /* biClrUsed */ 0,0,0,0, /* biClrImportant */ 0,0,0,0, /* First color: black */ (byte)0xFF,(byte)0xFF,(byte)0xFF,0 /* Second color: white */ }; /* Get the HDC for the device */ int hDC = device.internal_new_GC(null); /* Source DC */ int hdcSource = OS.CreateCompatibleDC(hDC); if (hdcSource == 0) SWT.error(SWT.ERROR_NO_HANDLES); /* Monochrome (Intermediate) DC */ int bwDC = OS.CreateCompatibleDC(hdcSource); if (bwDC == 0) SWT.error(SWT.ERROR_NO_HANDLES); /* Destination DC */ int hdcBmp = OS.CreateCompatibleDC(hDC); if (hdcBmp == 0) SWT.error(SWT.ERROR_NO_HANDLES); /* Monochrome (Intermediate) DIB section */ int[] pbitsBW = new int[1]; int hbmBW = OS.CreateDIBSection(bwDC, rgbBwBitmapInfo, OS.DIB_RGB_COLORS, pbitsBW, 0, 0); if (hbmBW == 0) SWT.error(SWT.ERROR_NO_HANDLES); switch (type) { case SWT.BITMAP: /* Attach the bitmap to the source DC */ int hOldSrc = OS.SelectObject(hdcSource, srcImage.handle); /* Create the destination bitmap */ handle = OS.CreateCompatibleBitmap(hDC, r.width, r.height); if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES); /* Attach the DIB section and the new bitmap to the DCs */ int hOldBw = OS.SelectObject(bwDC, hbmBW); int hOldBmp = OS.SelectObject(hdcBmp, handle); /* BitBlt the bitmap into the monochrome DIB section */ OS.BitBlt(bwDC, 0, 0, r.width, r.height, hdcSource, 0, 0, OS.SRCCOPY); /* Paint the destination rectangle in gray */ RECT rect = new RECT(); rect.left = 0; rect.top = 0; rect.right = r.width; rect.bottom = r.height; OS.FillRect(hdcBmp, rect, OS.GetSysColorBrush(OS.COLOR_3DFACE)); /* * BitBlt the black bits in the monochrome bitmap into * COLOR_3DHILIGHT bits in the destination DC. * The magic ROP comes from Charles Petzold's book */ int hb = OS.CreateSolidBrush(OS.GetSysColor(OS.COLOR_3DHILIGHT)); int oldBrush = OS.SelectObject(hdcBmp, hb); OS.BitBlt(hdcBmp, 1, 1, r.width, r.height, bwDC, 0, 0, 0xB8074A); /* * BitBlt the black bits in the monochrome bitmap into * COLOR_3DSHADOW bits in the destination DC. */ hb = OS.CreateSolidBrush(OS.GetSysColor(OS.COLOR_3DSHADOW)); OS.DeleteObject(OS.SelectObject(hdcBmp, hb)); OS.BitBlt(hdcBmp, 0, 0, r.width, r.height, bwDC, 0, 0, 0xB8074A); OS.DeleteObject(OS.SelectObject(hdcBmp, oldBrush)); /* Free resources */ OS.SelectObject(hdcSource, hOldSrc); OS.SelectObject(hdcBmp, hOldBmp); OS.SelectObject(bwDC, hOldBw); OS.DeleteDC(hdcSource); OS.DeleteDC(bwDC); OS.DeleteDC(hdcBmp); OS.DeleteObject(hbmBW); /* Release the HDC for the device */ device.internal_dispose_GC(hDC, null); break; case SWT.ICON: /* Get icon information */ ICONINFO iconInfo = new ICONINFO(); if (OS.IsWinCE) { GetIconInfo(srcImage, iconInfo); } else { if (!OS.GetIconInfo(srcImage.handle, iconInfo)) SWT.error(SWT.ERROR_INVALID_IMAGE); } int hdcMask = OS.CreateCompatibleDC(hDC); /* Create the destination bitmaps */ if (iconInfo.hbmColor == 0) hOldSrc = OS.SelectObject(hdcSource, iconInfo.hbmMask); else hOldSrc = OS.SelectObject(hdcSource, iconInfo.hbmColor); int newHbmp = OS.CreateCompatibleBitmap(hdcSource, r.width, r.height); if (newHbmp == 0) SWT.error(SWT.ERROR_NO_HANDLES); int newHmask = OS.CreateBitmap(r.width, r.height, 1, 1, null); if (newHmask == 0) SWT.error(SWT.ERROR_NO_HANDLES); /* BitBlt the source mask into the destination mask */ int hOldMask = OS.SelectObject(hdcMask, newHmask); if (iconInfo.hbmColor != 0) OS.SelectObject(hdcSource, iconInfo.hbmMask); OS.SelectObject(hdcSource, iconInfo.hbmMask); OS.BitBlt(hdcMask, 0, 0, r.width, r.height, hdcSource, 0, 0, OS.SRCCOPY); /* Attach the monochrome DIB section and the destination bitmap to the DCs */ hOldBw = OS.SelectObject(bwDC, hbmBW); /* BitBlt the bitmap into the monochrome DIB section */ if (iconInfo.hbmColor == 0) { OS.SelectObject(hdcSource, iconInfo.hbmMask);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -