net.refractions.udig.ui.graphics
Class NonAdvancedSWTGraphics

java.lang.Object
  extended by net.refractions.udig.ui.graphics.NonAdvancedSWTGraphics
All Implemented Interfaces:
ViewportGraphics

public class NonAdvancedSWTGraphics
extends java.lang.Object
implements ViewportGraphics

A Graphics object that wraps SWT's GC object

Since:
0.3
Author:
jeichar

Field Summary
static int TRANSPARENT
          The TRANSPARENT color
 
Fields inherited from interface net.refractions.udig.ui.graphics.ViewportGraphics
ALIGN_BOTTOM, ALIGN_LEFT, ALIGN_MIDDLE, ALIGN_RIGHT, ALIGN_TOP, LINE_DASH, LINE_DASHDOT, LINE_DASHDOTDOT, LINE_DOT, LINE_SOLID, LINE_SOLID_ROUNDED
 
Constructor Summary
NonAdvancedSWTGraphics(GC gc, Display display, java.awt.Dimension displaySize)
          Construct SWTGraphics.
NonAdvancedSWTGraphics(Image image, Display display)
          Construct SWTGraphics.
 
Method Summary
 void clearRect(int x, int y, int width, int height)
          Fills the specified rectangle with the background color.
static java.awt.image.BufferedImage createBufferedImage(int w, int h)
          Create a buffered image that can be be coverted to SWTland later
static Image createDefaultImage(Display display, int width, int height)
           
static ImageData createImageData(java.awt.image.RenderedImage image, boolean transparent)
           
static ImageData createImageDataFromBytes(java.awt.image.RenderedImage image)
           
static ImageDescriptor createImageDescriptor(java.awt.image.RenderedImage image, boolean transparent)
           
static Image createSWTImage(java.awt.image.RenderedImage image, boolean transparent)
           
 void dispose()
          Disposes of any resources the graphics might be hanging on to.
 void draw(java.awt.Shape s)
          Draws the outline of shape using the color, clip & transform.
 void drawImage(java.awt.Image image, int x, int y)
          Draws an Image.
 void drawImage(java.awt.Image image, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2)
          Draws a portion of the image to the target location on the viewport graphics.
 void drawImage(java.awt.image.RenderedImage rimage, int x, int y)
          Draws an image.
 void drawImage(java.awt.image.RenderedImage rimage, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2)
           
 void drawLine(int x1, int y1, int x2, int y2)
          Draws a line from x1,y1 to x2,y2
 void drawOval(int x, int y, int width, int height)
          Draws an Oval - only the boundary
 void drawPath(Path path)
          Draws the outline of the path using the color, clip and transform.
 void drawRect(int x, int y, int width, int height)
          Draws a rectangle - only the boundary.
 void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
          Draws a round cornered rectangle
 void drawString(java.lang.String string, int x, int y, int alignx, int aligny)
          Draws a string.
 void fill(java.awt.Shape s)
          Fills the interior of a Shape using the foreground color, clip & transform.
 void fillGradientRectangle(int x, int y, int width, int height, java.awt.Color startColor, java.awt.Color endColor, boolean isVertical)
          Fills a rectangle using a gradient paint
 void fillOval(int x, int y, int width, int height)
          Fills an Oval
 void fillPath(Path path)
          Fills the interior of the path with the forground color.
 void fillRect(int x, int y, int width, int height)
          Fills a rectangle.
 void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
          Fills a round cornered rectangle using the foreground color
 java.awt.Color getBackgroundColor()
          Gets the current background color value
 java.awt.Shape getClip()
          Gets the area that can be drawn in.
 java.awt.Color getColor()
          Gets the current Color value
 int getDPI()
          Get the dots per inch, used to scale fonts, but you can scale anything you want based on this value (for example a scalebar).
 int getFontAscent()
          Gets the ascent of the current font, which is the distance the font rises above its baseline.
 int getFontHeight()
          Gets the height of the current font TODO at some point maybe this could be broken out to getFontMetrics(), and a create FontMetrics object that maps between SWT and AWT.
 GC getGC()
          Gets the SWT graphics if applicable.
 java.awt.geom.Rectangle2D getStringBounds(java.lang.String str)
          Returns the bounds of a String.
 java.awt.geom.AffineTransform getTransform()
           
 void setAffineTransform(java.awt.geom.AffineTransform t)
          Sets an affine transformation for drawing shapes.
 void setBackground(java.awt.Color c)
          This is hard because - background doesn't mean what we think it means.
 void setClip(java.awt.Rectangle r)
          Sets the clip.
 void setClipBounds(java.awt.Rectangle newBounds)
          Sets the clip area.
 void setColor(java.awt.Color c)
          Sets the foreground color to draw with.
 void setFont(java.awt.Font f)
          Sets the font; size is taken to be in DPI.
 void setLineDash(int[] dash)
          Sets the line dash pattern
 void setLineWidth(int width)
          Sets the line width of the graphics (in pixels).
 void setStroke(int style, int width)
          Sets the stroke color to draw with.
 void setTransform(java.awt.geom.AffineTransform transform)
          Modifies the graphics so that further draws us minX,minY as the origin and maxX and maxY as the width and height of the display area.
 int stringWidth(java.lang.String str)
          Returns the length in pixels of the given string, or -1 if this operation is not available.
 void translate(java.awt.Point offset)
          Sets the draw offset.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TRANSPARENT

public static final int TRANSPARENT
The TRANSPARENT color

See Also:
Constant Field Values
Constructor Detail

NonAdvancedSWTGraphics

public NonAdvancedSWTGraphics(Image image,
                              Display display)
Construct SWTGraphics.

Parameters:
Image - image
display - The display object

NonAdvancedSWTGraphics

public NonAdvancedSWTGraphics(GC gc,
                              Display display,
                              java.awt.Dimension displaySize)
Construct SWTGraphics.

Parameters:
gc - The GC object
display - The display object
Method Detail

dispose

public void dispose()
Description copied from interface: ViewportGraphics
Disposes of any resources the graphics might be hanging on to.

Specified by:
dispose in interface ViewportGraphics

getGC

public GC getGC()
Description copied from interface: ViewportGraphics
Gets the SWT graphics if applicable. May return null if no SWT graphics.

Specified by:
getGC in interface ViewportGraphics
Returns:
SWT graphics or null

draw

public void draw(java.awt.Shape s)
Description copied from interface: ViewportGraphics
Draws the outline of shape using the color, clip & transform.

Reference description from Graphics2d: Strokes the outline of a Shape using the settings of the current Graphics2D context. The rendering attributes applied include the Clip, Transform, Paint, Composite and Stroke attributes.

Specified by:
draw in interface ViewportGraphics
Parameters:
s - the Shape to be rendered
See Also:
net.refractions.udig.project.render.ViewportGraphics#draw(java.awt.Shape)

fill

public void fill(java.awt.Shape s)
Description copied from interface: ViewportGraphics
Fills the interior of a Shape using the foreground color, clip & transform.

Reference description from Graphics2d: Fills the interior of a Shape using the settings of the Graphics2D context. The rendering attributes applied include the Clip, Transform, Paint, and Composite.

Specified by:
fill in interface ViewportGraphics
Parameters:
s - the Shape to be filled

See Also:
net.refractions.udig.project.render.ViewportGraphics#draw(java.awt.Shape)

setAffineTransform

public void setAffineTransform(java.awt.geom.AffineTransform t)
Sets an affine transformation for drawing shapes.

Parameters:
t - The transform.

fillRect

public void fillRect(int x,
                     int y,
                     int width,
                     int height)
Description copied from interface: ViewportGraphics
Fills a rectangle.

Specified by:
fillRect in interface ViewportGraphics
Parameters:
x - the starting x coordinate
y - the starting y coordinate
width - the width of the rectangle.
height - the height of the rectangle.
See Also:
net.refractions.udig.project.render.ViewportGraphics#fillRect(int, int, int, int)

setColor

public void setColor(java.awt.Color c)
Description copied from interface: ViewportGraphics
Sets the foreground color to draw with.

Specified by:
setColor in interface ViewportGraphics
Parameters:
c - The new color.
See Also:
net.refractions.udig.project.render.ViewportGraphics#setColor(java.awt.Color)

setBackground

public void setBackground(java.awt.Color c)
This is hard because - background doesn't mean what we think it means.

Specified by:
setBackground in interface ViewportGraphics
Parameters:
c - The new color.
See Also:
net.refractions.udig.project.render.ViewportGraphics#setBackground(java.awt.Color)

setStroke

public void setStroke(int style,
                      int width)
Description copied from interface: ViewportGraphics
Sets the stroke color to draw with.

Specified by:
setStroke in interface ViewportGraphics
Parameters:
style - The style of line to draw.
width - the width, in pixels, to draw lines with.
See Also:
net.refractions.udig.project.render.ViewportGraphics#setStroke(int, int)

setClip

public void setClip(java.awt.Rectangle r)
Description copied from interface: ViewportGraphics
Sets the clip.

Specified by:
setClip in interface ViewportGraphics
Parameters:
r - the rectangle to clip to.
See Also:
net.refractions.udig.project.render.ViewportGraphics#setClip(java.awt.Rectangle)

translate

public void translate(java.awt.Point offset)
Description copied from interface: ViewportGraphics
Sets the draw offset.

Specified by:
translate in interface ViewportGraphics
Parameters:
offset - The amount the draw is offset in the graphics.
See Also:
net.refractions.udig.project.render.ViewportGraphics#translate(java.awt.Point)

clearRect

public void clearRect(int x,
                      int y,
                      int width,
                      int height)
Description copied from interface: ViewportGraphics
Fills the specified rectangle with the background color.

Specified by:
clearRect in interface ViewportGraphics
Parameters:
x - The starting corner's x-coordinate.
y - The starting corner's y-coordinate.
width - the width of the rectangle
height - the height of the rectangle
See Also:
net.refractions.udig.project.render.ViewportGraphics#clearRect(int, int, int, int)

drawImage

public void drawImage(java.awt.image.RenderedImage rimage,
                      int x,
                      int y)
Description copied from interface: ViewportGraphics
Draws an image.

Specified by:
drawImage in interface ViewportGraphics
x - The x coordinate of the image top left corner of the image.
y - The y coordinate of the image top left corner of the image.
See Also:
net.refractions.udig.project.render.ViewportGraphics#drawImage(javax.media.jai.PlanarImage, int, int)

createDefaultImage

public static Image createDefaultImage(Display display,
                                       int width,
                                       int height)

createImageDescriptor

public static ImageDescriptor createImageDescriptor(java.awt.image.RenderedImage image,
                                                    boolean transparent)

createBufferedImage

public static java.awt.image.BufferedImage createBufferedImage(int w,
                                                               int h)
Create a buffered image that can be be coverted to SWTland later


createSWTImage

public static Image createSWTImage(java.awt.image.RenderedImage image,
                                   boolean transparent)

createImageDataFromBytes

public static ImageData createImageDataFromBytes(java.awt.image.RenderedImage image)

createImageData

public static ImageData createImageData(java.awt.image.RenderedImage image,
                                        boolean transparent)

drawString

public void drawString(java.lang.String string,
                       int x,
                       int y,
                       int alignx,
                       int aligny)
Description copied from interface: ViewportGraphics
Draws a string. Alignment parameters specify where the string should be located relative to coordinate (x,y).

Specified by:
drawString in interface ViewportGraphics
Parameters:
string - The string to draw.
x - the x coordinate of the location where the of the string will be placed.
y - the y coordinate of the location where the of the string will be placed.
alignx - horizontal alignment, ViewportGraphics.ALIGN_LEFT, ViewportGraphics.ALIGN_MIDDLE or ViewportGraphics.ALIGN_RIGHT
aligny - vertical alignment, ViewportGraphics.ALIGN_BOTTOM, ViewportGraphics.ALIGN_MIDDLE or ViewportGraphics.ALIGN_TOP

setTransform

public void setTransform(java.awt.geom.AffineTransform transform)
Description copied from interface: ViewportGraphics
Modifies the graphics so that further draws us minX,minY as the origin and maxX and maxY as the width and height of the display area.

Specified by:
setTransform in interface ViewportGraphics

drawImage

public void drawImage(java.awt.Image image,
                      int x,
                      int y)
Description copied from interface: ViewportGraphics
Draws an Image.

Specified by:
drawImage in interface ViewportGraphics
x - The x coordinate of the image top left corner of the image.
y - The y coordinate of the image top left corner of the image.
See Also:
Current version can only draw Image if the image is an RenderedImage

drawImage

public void drawImage(java.awt.Image image,
                      int dx1,
                      int dy1,
                      int dx2,
                      int dy2,
                      int sx1,
                      int sy1,
                      int sx2,
                      int sy2)
Description copied from interface: ViewportGraphics
Draws a portion of the image to the target location on the viewport graphics.

Specified by:
drawImage in interface ViewportGraphics
dx1 - - the x coordinate of the first corner of the destination rectangle.
dy1 - - the y coordinate of the first corner of the destination rectangle.
dx2 - - the x coordinate of the second corner of the destination rectangle.
dy2 - - the y coordinate of the second corner of the destination rectangle.
sx1 - - the x coordinate of the first corner of the source rectangle.
sy1 - - the y coordinate of the first corner of the source rectangle.
sx2 - - the x coordinate of the second corner of the source rectangle.
sy2 - - the y coordinate of the second corner of the source rectangle.
See Also:
ViewportGraphics.drawImage(java.awt.Image, int, int, int, int, int, int, int, int)

drawImage

public void drawImage(java.awt.image.RenderedImage rimage,
                      int dx1,
                      int dy1,
                      int dx2,
                      int dy2,
                      int sx1,
                      int sy1,
                      int sx2,
                      int sy2)

getFontHeight

public int getFontHeight()
Description copied from interface: ViewportGraphics
Gets the height of the current font TODO at some point maybe this could be broken out to getFontMetrics(), and a create FontMetrics object that maps between SWT and AWT.

Specified by:
getFontHeight in interface ViewportGraphics
Returns:
the height of the current font

stringWidth

public int stringWidth(java.lang.String str)
Description copied from interface: ViewportGraphics
Returns the length in pixels of the given string, or -1 if this operation is not available.

Specified by:
stringWidth in interface ViewportGraphics
Returns:

getFontAscent

public int getFontAscent()
Description copied from interface: ViewportGraphics
Gets the ascent of the current font, which is the distance the font rises above its baseline.

Specified by:
getFontAscent in interface ViewportGraphics
Returns:

getStringBounds

public java.awt.geom.Rectangle2D getStringBounds(java.lang.String str)
Description copied from interface: ViewportGraphics
Returns the bounds of a String. Does not expand tabs or newlines

Specified by:
getStringBounds in interface ViewportGraphics
Returns:

drawLine

public void drawLine(int x1,
                     int y1,
                     int x2,
                     int y2)
Description copied from interface: ViewportGraphics
Draws a line from x1,y1 to x2,y2

Specified by:
drawLine in interface ViewportGraphics

getTransform

public java.awt.geom.AffineTransform getTransform()
Specified by:
getTransform in interface ViewportGraphics

drawPath

public void drawPath(Path path)
Description copied from interface: ViewportGraphics
Draws the outline of the path using the color, clip and transform.

Specified by:
drawPath in interface ViewportGraphics

fillPath

public void fillPath(Path path)
Description copied from interface: ViewportGraphics
Fills the interior of the path with the forground color.

Specified by:
fillPath in interface ViewportGraphics
Parameters:
path - the path to fill.

drawRect

public void drawRect(int x,
                     int y,
                     int width,
                     int height)
Description copied from interface: ViewportGraphics
Draws a rectangle - only the boundary.

Specified by:
drawRect in interface ViewportGraphics
Parameters:
x - the starting x coordinate
y - the starting y coordinate
width - the width of the rectangle.
height - the height of the rectangle.

drawOval

public void drawOval(int x,
                     int y,
                     int width,
                     int height)
Description copied from interface: ViewportGraphics
Draws an Oval - only the boundary

Specified by:
drawOval in interface ViewportGraphics
Parameters:
x - the starting x coordinate
y - the starting y coordinate
width - the width of the Oval.
height - the height of the Oval.

fillOval

public void fillOval(int x,
                     int y,
                     int width,
                     int height)
Description copied from interface: ViewportGraphics
Fills an Oval

Specified by:
fillOval in interface ViewportGraphics
Parameters:
x - the starting x coordinate
y - the starting y coordinate
width - the width of the Oval.
height - the height of the Oval.

getClip

public java.awt.Shape getClip()
Description copied from interface: ViewportGraphics
Gets the area that can be drawn in.

Specified by:
getClip in interface ViewportGraphics
Returns:
the area that can be drawn in.

setClipBounds

public void setClipBounds(java.awt.Rectangle newBounds)
Description copied from interface: ViewportGraphics
Sets the clip area.

Specified by:
setClipBounds in interface ViewportGraphics
Parameters:
newBounds - new clip area

getBackgroundColor

public java.awt.Color getBackgroundColor()
Description copied from interface: ViewportGraphics
Gets the current background color value

Specified by:
getBackgroundColor in interface ViewportGraphics
Returns:
the current background color value

getColor

public java.awt.Color getColor()
Description copied from interface: ViewportGraphics
Gets the current Color value

Specified by:
getColor in interface ViewportGraphics
Returns:
the current Color value

drawRoundRect

public void drawRoundRect(int x,
                          int y,
                          int width,
                          int height,
                          int arcWidth,
                          int arcHeight)
Description copied from interface: ViewportGraphics
Draws a round cornered rectangle

Specified by:
drawRoundRect in interface ViewportGraphics
Parameters:
x - the x component of the upper left corner
y - the y component of the upper left corner
width - the width of the rectangle
height - the height of the rectangle
arcWidth - the horizontal diameter of the arc at the four corners.
arcHeight - the vertical diameter of the arc at the four corners

fillRoundRect

public void fillRoundRect(int x,
                          int y,
                          int width,
                          int height,
                          int arcWidth,
                          int arcHeight)
Description copied from interface: ViewportGraphics
Fills a round cornered rectangle using the foreground color

Specified by:
fillRoundRect in interface ViewportGraphics
Parameters:
x - the x component of the upper left corner
y - the y component of the upper left corner
width - the width of the rectangle
height - the height of the rectangle
arcWidth - the horizontal diameter of the arc at the four corners.
arcHeight - the vertical diameter of the arc at the four corners

setLineDash

public void setLineDash(int[] dash)
Description copied from interface: ViewportGraphics
Sets the line dash pattern

Specified by:
setLineDash in interface ViewportGraphics
Parameters:
dash - the pattern of dashes.

setLineWidth

public void setLineWidth(int width)
Description copied from interface: ViewportGraphics
Sets the line width of the graphics (in pixels).

If you need to make your drawing device independent please consider making use of the DPI setting.


 g.setLineWith( width ); // BEFORE
 g.setLineWith( (width * g.getDPI()) / 72 ); // AFTER 
 
Where 72 is chosen because that is what a Java image resolutions is assumed to be by default.

Specified by:
setLineWidth in interface ViewportGraphics

setFont

public void setFont(java.awt.Font f)
Description copied from interface: ViewportGraphics
Sets the font; size is taken to be in DPI.

Please note this is a change in behaviour over AWT (where size is documented to be in 72 dpi. They are changing their mind in later versions of java but we cannot wait for them to get there act together.

Specified by:
setFont in interface ViewportGraphics

getDPI

public int getDPI()
Description copied from interface: ViewportGraphics
Get the dots per inch, used to scale fonts, but you can scale anything you want based on this value (for example a scalebar).

Specified by:
getDPI in interface ViewportGraphics

fillGradientRectangle

public void fillGradientRectangle(int x,
                                  int y,
                                  int width,
                                  int height,
                                  java.awt.Color startColor,
                                  java.awt.Color endColor,
                                  boolean isVertical)
Description copied from interface: ViewportGraphics
Fills a rectangle using a gradient paint

Specified by:
fillGradientRectangle in interface ViewportGraphics
Parameters:
x - the x component of the upper left corner
y - the y component of the upper left corner
width - the width of the rectangle
height - the height of the rectangle
startColor - the first color used in the gradient paint
endColor - the last color used in the gradient paint
isVertical - orientation of the gradient