当前位置:编程学习 > JSP >>

引用 Taking a look at SWT Images

答案:
From: http://www.eclipse.org/articles/Article-SWT-images/graphics-resources.html

This first section of this article gives an introduction to colors and shows how an image records the color value of each pixel.
Introduction Image lifecycle ImageData Color PaletteData Indexed palette Direct palette The next section describes image transparency, alpha blending, animation, and how to scale images. Transparency Manipulating ImageData Saving Images Blending Single alpha value Different 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 Platform cursors Custom cursors IntroductionThe 易做图st way to create an SWT Image is to load it from a recognized graphic 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):
Image image = new Image(display,   "C:/eclipse/eclipse/plugins/org.eclipse.platform_2.0.2/eclipse_lg.gif");

Instead of hard-coding the location of the image, it's 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.getResourceAsStream(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 load 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 part of their setImage(Image image) methods.

Button button = new Button(shell,SWT.PUSH);button.setImage(image);



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(display.getSystemColor(SWT.COLOR_WHITE));gc.drawText("I've 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 lifecycleWhen an image is loaded, the 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 ImageData 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, 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 of resources in SWT when an object is garbage collected. For more information see SWT: The Standard Widget Toolkit: Managing Operating System Resources.
ImageDataImageData can be thought of as the model for an image, whereas the Image is the view that's been prepared for output onto a specific device. 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 2^4=16, a depth of 8 can store 2^8=256 values and a depth of 24 can represent 2^24=16 million different values per pixel. The larger the depth of an image the more bytes are required for its pixels, and some formats such as GIF that were initially designed for download across internet connections only support a maximum depth of 8. How the value of each pixel value translates into an actual color depends on its palette represented by the class org.eclipse.swt.graphics.PaletteData.
The next section describes how colors are represented by their RGB values, and how PaletteData maps a map pixel value to a particular color.
ColorThe class org.eclipse.swt.graphics.Color is used to manage resources that implement the RGB color model. Each color is described in terms of its red, green and blue component (each expressed as an integer value from 0 for no color to 255 for full color).
Color cyanColor = new Color(display,0,255,255);// ... Code to use the ColorcyanColor.dispose();

The convenience class org.eclipse.swt.graphics.RGB exists in SWT that combines a color's red, green and blue values into a single object.

RGB cyanRGB = new RGB(0,255,255);Color cyanColor = new Color(display,cyanRGB);// ... Code to use the ColorcyanColor.dispose();

The Color instance should be disposed when it is no longer required, whereas the RGB has no need to be disposed. This is similar to the relationship between an Image and its ImageData, where Color and Image are device specific objects using underlying native resources, while RGB and ImageData are the underlying model data.

To avoid having to create and manage instances of the commonly used colors, the Display class allow these to be retrieved using the method Display.getSystemColor(int id).

Color cyanColor = display.getSystemColor(SWT.COLOR_CYAN)

When a Color is obtained by an SWT program using the method Display.getSystemColor(int id) method, it must not be disposed. The rule of thumb that works for any SWT resource is "If you created it, you are responsible for disposing it". Because the statement above retrieved the cyan color instance, and didn't explicitly construct it, it should not be disposed.

How a Color is actually represented on the display depends on factors such as the resolution and depth of the display. For more information on this and the SWT color model see SWT Color Model.
PaletteDataThere are two kinds of PaletteData, an indexed palette and a direct palette. PaletteData is a model of how pixel values map to RGB values, and because they do not represent an underlying resource, they do not require disposing. Indexed paletteWith an indexed palette each pixel value represents a number that is then cross indexed with the palette to determine the actual color. The range of allowable pixel values is the depth of the image.
The example below is a 48 by 48 square image created with a depth of 1, and an indexed color palette. The indexed palette assigns 0 to be red and 1 to be green (by virtue of their order in the RGB[] in the constructor). The ImageData's un-initialized pixel values will initially be 0 (red), and two for loops set a 34 by 34 square in the center of the ImageData to be 1 (green).

PaletteData paletteData = new PaletteData(        new RGB[] {new RGB(255,0,0), new RGB(0,255,0)});    ImageData imageData = new ImageData(48,48,1,paletteData); for(int x=11;x<35;x++){        for(int y=11;y<35;y++){            imageData.setPixel(x,y,1);&nbs

上一个:J2SE新特性---循环语句的增强
下一个:引用 Eclipse 照亮Java众生

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,