org.eclipse.swt.graphics
Class GC

java.lang.Object
  |
  +--org.eclipse.swt.graphics.GC

public final class GC
extends Object

Class GC is where all of the drawing capabilities that are supported by SWT are located. Instances are used to draw on either an Image, a Control, or directly on a Display.

Styles:
LEFT_TO_RIGHT, RIGHT_TO_LEFT

Application code must explicitly invoke the GC.dispose() method to release the operating system resources managed by each instance when those instances are no longer required. This is particularly important on Windows95 and Windows98 where the operating system has a limited number of device contexts available.

Note: Only one of LEFT_TO_RIGHT and RIGHT_TO_LEFT may be specified.

See Also:
org.eclipse.swt.events.PaintEvent

Constructor Summary
GC(Drawable drawable)
          Constructs a new instance of this class which has been configured to draw on the specified drawable.
 
Method Summary
 void dispose()
          Disposes of the operating system resources associated with the graphics context.
 void drawLine(int x1, int y1, int x2, int y2)
          Draws a line, using the foreground color, between the points (x1, y1) and (x2, y2).
 void drawRectangle(int x, int y, int w, int h)
          Draws the outline of the rectangle specified by the arguments, using the receiver's foreground color.
 void drawText(String string, int x, int y, int flags)
          Draws the given string, using the receiver's current font and foreground color.
 void fillRectangle(int x, int y, int w, int h)
          Fills the interior of the rectangle specified by the arguments, using the receiver's background color.
 boolean isDisposed()
          Returns true if the GC has been disposed, and false otherwise.
 void setBackground(Color color)
          Sets the background color.
 void setForeground(Color color)
          Sets the foreground color.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, toString
 

Constructor Detail

GC

public GC(Drawable drawable)
Constructs a new instance of this class which has been configured to draw on the specified drawable. Sets the foreground and background color in the GC to match those in the drawable.

You must dispose the graphics context when it is no longer required.

Parameters:
drawable - the drawable to draw on
Throws:
IllegalArgumentException -
SWTError -
Method Detail

dispose

public void dispose()
Disposes of the operating system resources associated with the graphics context. Applications must dispose of all GCs which they allocate.


drawLine

public void drawLine(int x1,
                     int y1,
                     int x2,
                     int y2)
Draws a line, using the foreground color, between the points (x1, y1) and (x2, y2).

Parameters:
x1 - the first point's x coordinate
y1 - the first point's y coordinate
x2 - the second point's x coordinate
y2 - the second point's y coordinate
Throws:
SWTException -
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed

drawRectangle

public void drawRectangle(int x,
                          int y,
                          int w,
                          int h)
Draws the outline of the rectangle specified by the arguments, using the receiver's foreground color. The left and right edges of the rectangle are at x and x + width. The top and bottom edges are at y and y + height.

Parameters:
x - the x coordinate of the rectangle to be drawn
y - the y coordinate of the rectangle to be drawn
w - the width of the rectangle to be drawn
h - the height of the rectangle to be drawn
Throws:
SWTException -
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed

fillRectangle

public void fillRectangle(int x,
                          int y,
                          int w,
                          int h)
Fills the interior of the rectangle specified by the arguments, using the receiver's background color.

Parameters:
x - the x coordinate of the rectangle to be filled
y - the y coordinate of the rectangle to be filled
w - the width of the rectangle to be filled
h - the height of the rectangle to be filled
Throws:
SWTException -
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
See Also:
drawRectangle(int, int, int, int)

isDisposed

public boolean isDisposed()
Returns true if the GC has been disposed, and false otherwise.

This method gets the dispose state for the GC. When a GC has been disposed, it is an error to invoke any other method using the GC.

Returns:
true when the GC is disposed and false otherwise

setBackground

public void setBackground(Color color)
Sets the background color. The background color is used for fill operations and as the background color when text is drawn.

Parameters:
color - the new background color for the receiver
Throws:
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

setForeground

public void setForeground(Color color)
Sets the foreground color. The foreground color is used for drawing operations including when text is drawn.

Parameters:
color - the new foreground color for the receiver
Throws:
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

drawText

public void drawText(String string,
                     int x,
                     int y,
                     int flags)
Draws the given string, using the receiver's current font and foreground color. Tab expansion, line delimiter and mnemonic processing are performed according to the specified flags. If flags includes DRAW_TRANSPARENT, then the background of the rectangular area where the text is being drawn will not be modified, otherwise it will be filled with the receiver's background color.

The parameter flags may be a combination of:

SWT.DRAW_DELIMITER
draw multiple lines
SWT.DRAW_TAB
expand tabs
SWT.DRAW_MNEMONIC
underline the mnemonic character
SWT.DRAW_TRANSPARENT
transparent background

Parameters:
string - the string to be drawn
x - the x coordinate of the top left corner of the rectangular area where the text is to be drawn
y - the y coordinate of the top left corner of the rectangular area where the text is to be drawn
flags - the flags specifing how to process the text
Throws:
IllegalArgumentException -
  • ERROR_NULL_ARGUMENT - if the string is null
SWTException -
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed