Eclipse Rich Ajax Platform

org.eclipse.swt.graphics
Class Image

java.lang.Object
  extended by org.eclipse.swt.graphics.Resource
      extended by org.eclipse.swt.graphics.Image

public final class Image
extends Resource

Instances of this class are graphics which have been prepared for display on a specific device. That is, they are to display on widgets with, for example, Button.setImage().

If loaded from a file format that supports it, an Image 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.

Note: Even though constructors are provided here, it is recommended to create images by using one of the getImage methods in class Graphics. These factory methods share images among all sessions. Creating images via constructors carelessly may lead to bad performance and/or unnecessary memory consumption.

See Also:
Graphics.getImage(String), Graphics.getImage(String, ClassLoader), Graphics.getImage(String, java.io.InputStream)

Field Summary
 InternalImage internalImage
          The internal resource.
 
Constructor Summary
Image(Device device, Image srcImage, int flag)
          Constructs a new instance of this class based on the provided image, with an appearance that varies depending on the value of the flag.
Image(Device device, int width, int height)
          Constructs an empty instance of this class with the specified width and height.
 
Method Summary
 Color getBackground()
          Returns the color to which to map the transparent pixel, or null if the receiver has no transparent pixel.
 Rectangle getBounds()
          Returns the bounds of the receiver.
 ImageData getImageData()
          Returns an ImageData based on the receiver Modifications made to this ImageData will not affect the Image.
 void setBackground(Color color)
          Sets the color to which to map the transparent pixel.
 
Methods inherited from class org.eclipse.swt.graphics.Resource
dispose, getDevice, isDisposed
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

internalImage

public final InternalImage internalImage
The internal resource. (Warning: This field is platform dependent)

IMPORTANT: This field is not part of the SWT public API. It is marked public only so that it can be shared within the packages provided by SWT. It is not available on all platforms and should never be accessed from application code.

Constructor Detail

Image

public Image(Device device,
             Image srcImage,
             int flag)
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:
SWT.IMAGE_COPY
the result is an identical copy of srcImage

Parameters:
device - the device on which to create the image
srcImage - the image to use as the source
flag - the style, either IMAGE_COPY, IMAGE_DISABLE or IMAGE_GRAY
Throws:
java.lang.IllegalArgumentException -
  • ERROR_NULL_ARGUMENT - if device is null and there is no current device
  • ERROR_NULL_ARGUMENT - if srcImage is null
  • ERROR_INVALID_ARGUMENT - if the flag is not one of IMAGE_COPY, IMAGE_DISABLE or IMAGE_GRAY
  • ERROR_INVALID_ARGUMENT - if the image has been disposed
SWTException -
  • ERROR_INVALID_IMAGE - if the image is not a bitmap or an icon, or is otherwise in an invalid state
  • ERROR_UNSUPPORTED_DEPTH - if the depth of the image is not supported
SWTError -
  • ERROR_NO_HANDLES if a handle could not be obtained for image creation
Since:
1.3

Image

public Image(Device device,
             int width,
             int height)
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:
    Image i = new Image(device, width, height);
    GC gc = new GC(i);
    gc.drawRectangle(0, 0, 50, 50);
    gc.dispose();
 

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.

Parameters:
device - the device on which to create the image
width - the width of the new image
height - the height of the new image
Throws:
java.lang.IllegalArgumentException -
  • ERROR_NULL_ARGUMENT - if device is null and there is no current device
  • ERROR_INVALID_ARGUMENT - if either the width or height is negative or zero
SWTError -
  • ERROR_NO_HANDLES if a handle could not be obtained for image creation
Since:
1.4
Method Detail

getBounds

public Rectangle getBounds()
Returns the bounds of the receiver. The rectangle will always have x and y values of 0, and the width and height of the image.

Returns:
a rectangle specifying the image's bounds
Throws:
SWTException -
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • ERROR_INVALID_IMAGE - if the image is not a bitmap or an icon

getImageData

public ImageData getImageData()
Returns an ImageData based on the receiver Modifications made to this ImageData will not affect the Image.

Returns:
an ImageData containing the image's data and attributes
Throws:
SWTException -
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  • ERROR_INVALID_IMAGE - if the image is not a bitmap or an icon
Since:
1.3
See Also:
ImageData

setBackground

public void setBackground(Color color)
Sets the color to which to map the transparent pixel.

There are certain uses of Images that do not support transparency (for example, setting an image into a button or label). In these cases, it may be desired to simulate transparency by using the background color of the widget to paint the transparent pixels of the image. This method specifies the color that will be used in these cases. For example:

    Button b = new Button();
    image.setBackground(b.getBackground());
    b.setImage(image);
 

The image may be modified by this operation (in effect, the transparent regions may be filled with the supplied color). Hence this operation is not reversible and it is not legal to call this function twice or with a null argument.

This method has no effect if the receiver does not have a transparent pixel value.

Parameters:
color - the color to use when a transparent pixel is specified
Throws:
java.lang.IllegalArgumentException -
  • ERROR_NULL_ARGUMENT - if the color is null
  • ERROR_INVALID_ARGUMENT - if the color has been disposed
SWTException -
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
Since:
1.3

getBackground

public Color getBackground()
Returns the color to which to map the transparent pixel, or null if the receiver has no transparent pixel.

There are certain uses of Images that do not support transparency (for example, setting an image into a button or label). In these cases, it may be desired to simulate transparency by using the background color of the widget to paint the transparent pixels of the image. Use this method to check which color will be used in these cases in place of transparency. This value may be set with setBackground().

Returns:
the background color of the image, or null if there is no transparency in the image
Throws:
SWTException -
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
Since:
1.3

Eclipse Rich Ajax Platform

Copyright (c) EclipseSource and others 2002, 2012. All rights reserved.