public final class Image extends Resource implements Drawable
GC.drawImage()
and 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.
There are two primary ways to use Images
.
The first is to load a graphic file from disk and create an
Image
from it. This is done using an Image
constructor, for example:
Image i = new Image(device, "C:\\graphic.bmp");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:
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);
Applications which require even greater control over the image
loading process should use the support provided in class
ImageLoader
.
Application code must explicitly invoke the Image.dispose()
method to release the operating system resources managed by each instance
when those instances are no longer required.
Modifier and Type | Field and Description |
---|---|
(package private) int |
alpha
the global alpha value to be used for every pixel
|
(package private) byte[] |
alphaData
the alpha data for the image
|
(package private) ImageData |
data
the image data used to create this image if it is a
icon.
|
(package private) static int |
DEFAULT_SCANLINE_PAD
specifies the default scanline padding
|
int |
handle
the handle to the OS image resource
(Warning: This field is platform dependent)
IMPORTANT: This field is not part of the SWT
public API.
|
(package private) int |
height
height of the image
|
(package private) GC |
memGC
the GC which is drawing on the image
|
(package private) int |
transparentColor
specifies the transparent pixel
|
(package private) int |
transparentPixel
specifies the transparent pixel
|
int |
type
specifies whether the receiver is a bitmap or an icon
(one of
SWT.BITMAP , SWT.ICON )
IMPORTANT: This field is not part of the SWT
public API. |
(package private) int |
width
width of the image
|
Constructor and Description |
---|
Image(Device device)
Prevents uninitialized instances from being created outside the package.
|
Image(Device device,
ImageData data)
Constructs an instance of this class from the given
ImageData . |
Image(Device device,
ImageData source,
ImageData mask)
Constructs an instance of this class, whose type is
SWT.ICON , from the two given ImageData
objects. |
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,
java.io.InputStream stream)
Constructs an instance of this class by loading its representation
from the specified input stream.
|
Image(Device device,
int width,
int height)
Constructs an empty instance of this class with the
specified width and height.
|
Image(Device device,
Rectangle bounds)
Constructs an empty instance of this class with the
width and height of the specified rectangle.
|
Image(Device device,
java.lang.String filename)
Constructs an instance of this class by loading its representation
from the file with the specified name.
|
Modifier and Type | Method and Description |
---|---|
(package private) static int |
createDIB(int width,
int height,
int depth) |
(package private) int |
createDIBFromDDB(int hDC,
int hBitmap,
int width,
int height)
Create a DIB from a DDB without using GetDIBits.
|
(package private) int[] |
createGdipImage() |
(package private) void |
destroy() |
boolean |
equals(java.lang.Object object)
Compares the argument to the receiver, and returns true
if they represent the same object using a class
specific comparison.
|
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.
|
(package private) static void |
GetIconInfo(Image image,
ICONINFO info)
Feature in WinCE.
|
ImageData |
getImageData()
Returns an
ImageData based on the receiver
Modifications made to this ImageData will not
affect the Image. |
int |
hashCode()
Returns an integer hash code for the receiver.
|
(package private) static int[] |
init(Device device,
Image image,
ImageData i) |
(package private) static int[] |
init(Device device,
Image image,
ImageData source,
ImageData mask) |
(package private) void |
init(ImageData i) |
(package private) void |
init(int width,
int height) |
(package private) void |
initNative(java.lang.String filename) |
void |
internal_dispose_GC(int hDC,
GCData data)
Invokes platform specific functionality to dispose a GC handle.
|
int |
internal_new_GC(GCData data)
Invokes platform specific functionality to allocate a new GC handle.
|
boolean |
isDisposed()
Returns
true if the image has been disposed,
and false otherwise. |
void |
setBackground(Color color)
Sets the color to which to map the transparent pixel.
|
java.lang.String |
toString()
Returns a string containing a concise, human-readable
description of the receiver.
|
static Image |
win32_new(Device device,
int type,
int handle)
Invokes platform specific functionality to allocate a new image.
|
public int type
SWT.BITMAP
, SWT.ICON
)
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.
public int handle
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.
int transparentPixel
int transparentColor
GC memGC
byte[] alphaData
int alpha
ImageData data
int width
int height
static final int DEFAULT_SCANLINE_PAD
Image(Device device)
public Image(Device device, int width, int height)
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.
device
- the device on which to create the imagewidth
- the width of the new imageheight
- the height of the new imagejava.lang.IllegalArgumentException
- SWTError
- public Image(Device device, Image srcImage, int flag)
SWT.IMAGE_COPY
SWT.IMAGE_DISABLE
SWT.IMAGE_GRAY
device
- the device on which to create the imagesrcImage
- the image to use as the sourceflag
- the style, either IMAGE_COPY
, IMAGE_DISABLE
or IMAGE_GRAY
java.lang.IllegalArgumentException
- IMAGE_COPY
, IMAGE_DISABLE
or IMAGE_GRAY
SWTException
- SWTError
- public Image(Device device, Rectangle bounds)
Image i = new Image(device, boundsRectangle); 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.
device
- the device on which to create the imagebounds
- a rectangle specifying the image's width and height (must not be null)java.lang.IllegalArgumentException
- SWTError
- public Image(Device device, ImageData data)
ImageData
.device
- the device on which to create the imagedata
- the image data to create the image from (must not be null)java.lang.IllegalArgumentException
- SWTException
- SWTError
- public Image(Device device, ImageData source, ImageData mask)
SWT.ICON
, from the two given ImageData
objects. The two images must be the same size. Pixel transparency
in either image will be ignored.
The mask image should contain white wherever the icon is to be visible, and black wherever the icon is to be transparent. In addition, the source image should contain black wherever the icon is to be transparent.
device
- the device on which to create the iconsource
- the color data for the iconmask
- the mask data for the iconjava.lang.IllegalArgumentException
- SWTError
- public Image(Device device, java.io.InputStream stream)
This constructor is provided for convenience when loading a single
image only. If the stream contains multiple images, only the first
one will be loaded. To load multiple images, use
ImageLoader.load()
.
This constructor may be used to load a resource as follows:
static Image loadImage (Display display, Class clazz, String string) { InputStream stream = clazz.getResourceAsStream (string); if (stream == null) return null; Image image = null; try { image = new Image (display, stream); } catch (SWTException ex) { } finally { try { stream.close (); } catch (IOException ex) {} } return image; }
device
- the device on which to create the imagestream
- the input stream to load the image fromjava.lang.IllegalArgumentException
- SWTException
- SWTError
- public Image(Device device, java.lang.String filename)
This constructor is provided for convenience when loading a single image only. If the specified file contains multiple images, only the first one will be used.
device
- the device on which to create the imagefilename
- the name of the file to load the image fromjava.lang.IllegalArgumentException
- SWTException
- SWTError
- void initNative(java.lang.String filename)
int createDIBFromDDB(int hDC, int hBitmap, int width, int height)
int[] createGdipImage()
public boolean equals(java.lang.Object object)
equals
in class java.lang.Object
object
- the object to compare with this objecttrue
if the object is the same as this object and false
otherwisehashCode()
public Color getBackground()
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().
SWTException
- public Rectangle getBounds()
SWTException
- public ImageData getImageData()
ImageData
based on the receiver
Modifications made to this ImageData
will not
affect the Image.ImageData
containing the image's data and attributesSWTException
- ImageData
public int hashCode()
true
when passed to
equals
must return the same value for this
method.hashCode
in class java.lang.Object
equals(java.lang.Object)
void init(int width, int height)
static int createDIB(int width, int height, int depth)
static void GetIconInfo(Image image, ICONINFO info)
void init(ImageData i)
public int internal_new_GC(GCData data)
IMPORTANT: This method is not part of the public
API for Image
. 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 called from
application code.
internal_new_GC
in interface Drawable
data
- the platform specific GC datapublic void internal_dispose_GC(int hDC, GCData data)
IMPORTANT: This method is not part of the public
API for Image
. 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 called from
application code.
internal_dispose_GC
in interface Drawable
hDC
- the platform specific GC handledata
- the platform specific GC datapublic boolean isDisposed()
true
if the image has been disposed,
and false
otherwise.
This method gets the dispose state for the image.
When an image has been disposed, it is an error to
invoke any other method (except Resource.dispose()
) using the image.
isDisposed
in class Resource
true
when the image is disposed and false
otherwisepublic void setBackground(Color color)
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.
color
- the color to use when a transparent pixel is specifiedjava.lang.IllegalArgumentException
- SWTException
- public java.lang.String toString()
toString
in class java.lang.Object
public static Image win32_new(Device device, int type, int handle)
IMPORTANT: This method is not part of the public
API for Image
. 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 called from
application code.
device
- the device on which to allocate the colortype
- the type of the image (SWT.BITMAP
or SWT.ICON
)handle
- the OS handle for the image