1、 1 Taking a look at SWT Images Summary SWTs Image class can be used to display images in a GUI. The most common source of images is to load from a standard file format such as GIF, JPEG, PNG, or BMP. Some controls, including Buttons and TreeItems, are able to display an Image directly through the se
2、tImage(Image) method, but any controls paint event allows images to be drawn through the callbacks graphic context. SWTs ImageData class represents the raw data making up an SWT Image and determines the color for each pixel coordinate. This article shows the correct uses of ImageData and Image, show
3、s how to load images from files, and how to achieve graphic effects such as transparency, alpha blending, animation, scaling, and custom cursors. By Joe Winchester, IBM September 10th, 2003 This first section of this article gives an introduction to colors and shows how an image records the color va
4、lue of each pixel. Introduction Image lifecycle ImageData Color PaletteData o Indexed palette o Direct palette The next section describes image transparency, alpha blending, animation, and how to scale images. Transparency Manipulating ImageData Saving Images Blending o Single alpha value o Differen
5、t alpha value per pixel Image effects GIF animation Scaling Finally, the article shows how to create cursors from images, by using a source image together with a mask. Cursors o Platform cursors o Custom cursors Introduction The simplest way to create an SWT Image is to load it from a recognized gra
6、phic file format. This includes GIF, BMP (Windows format bitmap), JPG, and PNG. The TIFF format is also supported in more recent Eclipse releases. Images can be loaded from a known location in the file system using the constructor Image(Display display, String fileLocation): 2 Image image = new Imag
7、e(display, C:/eclipse/eclipse/plugins/org.eclipse.platform_2.0.2/eclipse_lg.gif); Instead of hard-coding the location of the image, its more common to load the Image from a folder location relative to a given class. This is done by creating an InputStream pointing to the file with the method Class.g
8、etResourceAsStream(String name), and using the result as the argument to the constructor Image(Display display, InputStream inputStream). The Eclipse package explorer below shows the class com.foo.ShellWithButtonShowingEclipseLogo and the eclipse_lg.gif in the same folder. To following code would lo
9、ad the graphic from its location relative to the class. Image image = new Image(display, ShellWithButtonShowingEclipseLogo.class.getResourceAsStream( eclipse_lg.gif); Once the image has been created it can be used as part of a control such as a Button or Label that is able to render the graphic as p
10、art of their setImage(Image image) methods. Button button = new Button(shell,SWT.PUSH); button.setImage(image); 3 Images can be drawn onto using a graphics context that is created with the constructor GC(Drawable drawable) with the Image as the argument. GC gc = new GC(image); gc.setForeground(displ
11、ay.getSystemColor(SWT.COLOR_WHITE); gc.drawText(Ive been drawn on,0,0,true); gc.dispose(); Using a GC to draw onto an Image permanently alters the graphic. More information on how to use a GC is covered in the article Graphics Context - Quick on the draw. Image lifecycle When an image is loaded, the
12、 first step is to create device independent ImageData represented by the class org.eclipse.swt.graphics.ImageData. Following this, the data is prepared for a specific device by creating an actual Image instance. As well as loading an Image directly from a file, you can separately create the ImageDat
13、a object and then construct the Image using Image(Device device, ImageData imageData). The data for an existing Image can be retrieved using getImageData(), although this will not be the same object that was used to create the image. This is because when preparing an image to be drawn onto a screen,
14、 properties such as its color depth might be different from the initial image data. Instances of Image represent an underlying resource that has been prepared for a specific device and they must be disposed when they are no longer required to free up the allocated resource. There is no finalization
15、of resources in SWT when an object is garbage collected. For more information see SWT: The Standard Widget Toolkit: Managing Operating System Resources. ImageData ImageData can be thought of as the model for an image, whereas the Image is the view thats been prepared for output onto a specific devic
16、e. The ImageData has a width and height, and a pixel value for each coordinate. The raw data of the image is a byte, and the depth of the image specifies the number of bits that are used for each coordinate. An image depth of 1 can store two possible values for each pixel (0 and 1), a depth of 4 can store 24=16, a depth of 8 can store 28=256 values and a depth of 24 can represent 224=16 million different values per pixel. The larger the depth of an