ch.aplu.jgamegrid
Class GGBitmap

java.lang.Object
  extended by ch.aplu.jgamegrid.GGBitmap

public class GGBitmap
extends java.lang.Object

Class to provide drawing methods for creating a bitmap image and some static helper methods for image loading and transformation.


Constructor Summary
GGBitmap(int width, int height)
          Creates a GGBitmap with given number of horizontal and vertical pixels that holds a Bitmap instance to draw graphics elements.
GGBitmap(int width, int height, java.awt.Color bgColor)
          Creates a GGBitmap with given number of horizontal and vertical pixels that holds a Bitmap instance to draw graphics elements.
 
Method Summary
 void clear()
          Clears the bitmap by painting it with the current background color.
 void clear(java.awt.Color color)
          Clears the bitmap by painting it with the given background color.
 void dispose()
          Releases all resources of the graphics context.
 void drawArc(java.awt.Point pt, int radius, double startAngle, double extendAngle)
          Draws an arc with given center, radius, start and end angle.
 void drawCircle(java.awt.Point center, int radius)
          Draws a circle with given center and given radius.
 void drawGeneralPath(java.awt.geom.GeneralPath gp)
          Draws a figure defined by the given GeneralPath.
 void drawImage(java.awt.image.BufferedImage bi)
          Draws the given image at position (0, 0) into the bitmap.
 void drawImage(java.awt.image.BufferedImage bi, int x, int y)
          Draws the given image into the bitmap.
 void drawLine(int x1, int y1, int x2, int y2)
          Draws a line from one coordinate pair to another coordinate pair.
 void drawLine(java.awt.Point pt1, java.awt.Point pt2)
          Draws a line from one coordinate pair to another coordinate pair.
 void drawPoint(java.awt.Point pt)
          Draws a single point.
 void drawPolygon(java.awt.Point[] vertexes)
          Draws a polygon with given vertexes.
 void drawRectangle(java.awt.Point pt1, java.awt.Point pt2)
          Draws a rectangle with given opposite corners.
 void drawText(java.lang.String text, java.awt.Point pt)
          Displays the given text at the given position using the current font.
 void fillArc(java.awt.Point pt, int radius, double startAngle, double extendAngle)
          Fills an arc with given center, radius, start and end angle.
 void fillCircle(java.awt.Point center, int radius)
          Draws a filled circle with given center and given radius.
 void fillGeneralPath(java.awt.geom.GeneralPath gp)
          Fills a figure defined by the given GeneralPath.
 void fillPolygon(java.awt.Point[] vertexes)
          Draws a filled polygon with given vertexes.
 void fillRectangle(java.awt.Point pt1, java.awt.Point pt2)
          Draws a filled rectangle with given opposite corners.
static java.awt.image.BufferedImage floodFill(java.awt.image.BufferedImage bi, java.awt.Point pt, java.awt.Color oldColor, java.awt.Color newColor)
          Fills a bounded single-colored region with the given color.
static java.lang.String[] getAvailableFontFamilies()
          Returns the available font families for the current platform.
 java.awt.Color getBgColor()
          Returns the current background color.
 java.awt.image.BufferedImage getBufferedImage()
          Returns the reference of the buffered image used as bitmap.
static byte[] getByteArray(java.awt.image.BufferedImage sourceImage, java.lang.String imageFormat)
          Returns the image in a byte array. imageFormat is the informal name of the format (one of the strings returned by get SupportedImageFormats()).
 java.awt.Color getColor(java.awt.Point pt)
          Returns the color of the pixel of the background at given point.
 java.awt.Graphics2D getContext()
          Returns the graphics device context used for drawing operations.
static java.awt.image.BufferedImage getImage(java.lang.String imagePath)
          Retrieves the image either from the jar resource, from local drive or from a internet server From the given filename the image file is searched in the following order:
- if application is packed into a jar archive, relative to the root of the jar archive
- relative to the directory <userhome>/gamegrid/
- relative or absolute to current application directory
- if filename starts with http://, from the given URL
- add prefix _ and search relative to the root of the jar archive
 int getLineWidth()
          Returns the current line width in pixels.
 java.awt.Color getPaintColor()
          Returns the current paint color.
static java.awt.image.BufferedImage getScaledImage(java.awt.image.BufferedImage bi, double factor, double angle)
          Transforms the given buffered image by scaling by the given factor and rotating by the given angle.
static java.awt.image.BufferedImage getScaledImage(java.lang.String imagePath, double factor, double angle)
          Retrieves the image either from the jar resource, from local drive or from a internet server and transforms it by scaling it by the given factor and rotating it by the given angle.
static java.lang.String[] getSupportedImageFormats()
          Returns all supported image formats.
 void setFont(java.awt.Font font)
          Sets the font for displaying text with setText()
 void setLineWidth(int width)
          Sets the current line width in pixels.
 void setPaintColor(java.awt.Color color)
          Sets the given new paint color (for drawing and filling).
 void setPaintMode()
          Sets the paint mode of the graphics context to overwrite with current paint color.
static java.awt.image.BufferedImage setTransparency(java.awt.image.BufferedImage bi, double factor)
          Returns a BufferedImage where each pixel has a new transparency value (alpha component in the ARGB color model).
 void setXORMode(java.awt.Color c)
          Sets the paint mode to alternate between the current color and the given color.
static boolean writeImage(java.awt.image.BufferedImage bi, java.lang.String filename, java.lang.String type)
          Writes a image file of the given BufferedImage.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GGBitmap

public GGBitmap(int width,
                int height)
Creates a GGBitmap with given number of horizontal and vertical pixels that holds a Bitmap instance to draw graphics elements. The coordinates for the drawing methods are 0..width-1, 0..height-1 with zero at upper left corner.
Defaults:
- paint color: white
- line width: 1 pixel
- background color: white fully-transparent, ARGB = (255,255,255,0)
- font: SansSerif, Font.PLAIN, 24 pixel

Parameters:
width - the width of the Bitmap in pixels
height - the height of the Bitmap in pixels

GGBitmap

public GGBitmap(int width,
                int height,
                java.awt.Color bgColor)
Creates a GGBitmap with given number of horizontal and vertical pixels that holds a Bitmap instance to draw graphics elements. The coordinates for the drawing methods are 0..width-1, 0..height-1 with zero at upper left corner. Defaults:
- paint color: white
- line width: 1 pixel
- font: SansSerif, Font.PLAIN, 24 pixel

Parameters:
width - the width of the Bitmap in pixels
height - the height of the Bitmap in pixels
bgColor - the color of the background, may be (semi-)transparent
Method Detail

getScaledImage

public static java.awt.image.BufferedImage getScaledImage(java.lang.String imagePath,
                                                          double factor,
                                                          double angle)
Retrieves the image either from the jar resource, from local drive or from a internet server and transforms it by scaling it by the given factor and rotating it by the given angle. From the given filename the image file is searched in the following order:
- if application is packed into a jar archive, relative to the root of the jar archive
- relative to the directory <userhome>/gamegrid/
- relative or absolute to current application directory
If filename starts with http://, the image is loaded from the given URL

Parameters:
imagePath - the file name or url
factor - the zoom factor (>1 zoom-in, <1 zoom-out)
angle - the rotation angle (in degrees clockwise)
Returns:
the transformed image or null, if the image search fails

getScaledImage

public static java.awt.image.BufferedImage getScaledImage(java.awt.image.BufferedImage bi,
                                                          double factor,
                                                          double angle)
Transforms the given buffered image by scaling by the given factor and rotating by the given angle.

Parameters:
bi - the buffered image to transform
factor - the zoom factor (>1 zoom-in, <1 zoom-out)
angle - the rotation angle (in degrees clockwise)
Returns:
the transformed image

getImage

public static java.awt.image.BufferedImage getImage(java.lang.String imagePath)
Retrieves the image either from the jar resource, from local drive or from a internet server From the given filename the image file is searched in the following order:
- if application is packed into a jar archive, relative to the root of the jar archive
- relative to the directory <userhome>/gamegrid/
- relative or absolute to current application directory
- if filename starts with http://, from the given URL
- add prefix _ and search relative to the root of the jar archive

Parameters:
imagePath - the file name or url
Returns:
the buffered image or null, if the image search fails

drawImage

public void drawImage(java.awt.image.BufferedImage bi,
                      int x,
                      int y)
Draws the given image into the bitmap.

Parameters:
x - x-coordinate of upper left corner
y - y-coordinate of upper left corner

drawImage

public void drawImage(java.awt.image.BufferedImage bi)
Draws the given image at position (0, 0) into the bitmap.


clear

public void clear(java.awt.Color color)
Clears the bitmap by painting it with the given background color.

Parameters:
color - the color of the background

clear

public void clear()
Clears the bitmap by painting it with the current background color.


getLineWidth

public int getLineWidth()
Returns the current line width in pixels.

Returns:
the line width

setLineWidth

public void setLineWidth(int width)
Sets the current line width in pixels.

Parameters:
width - the new line width

getPaintColor

public java.awt.Color getPaintColor()
Returns the current paint color.

Returns:
the paint color

setPaintColor

public void setPaintColor(java.awt.Color color)
Sets the given new paint color (for drawing and filling).

Parameters:
color - the new line color

getBgColor

public java.awt.Color getBgColor()
Returns the current background color.

Returns:
the background color

drawLine

public void drawLine(int x1,
                     int y1,
                     int x2,
                     int y2)
Draws a line from one coordinate pair to another coordinate pair. The line width and color is determined by setLineWidth() and setPaintColor().

Parameters:
x1 - the x-coordinate of the start point
y1 - the y-coordinate of the start point
x2 - the x-coordinate of the endpoint
y2 - the y-coordinate of the endpoint

drawLine

public void drawLine(java.awt.Point pt1,
                     java.awt.Point pt2)
Draws a line from one coordinate pair to another coordinate pair. The line width and color is determined by setLineWidth() and setPaintColor().

Parameters:
pt1 - the start point
pt2 - the endpoint

drawCircle

public void drawCircle(java.awt.Point center,
                       int radius)
Draws a circle with given center and given radius.

Parameters:
radius - the radius of the circle

fillCircle

public void fillCircle(java.awt.Point center,
                       int radius)
Draws a filled circle with given center and given radius.

Parameters:
center - the center of the circle
radius - the radius of the circle

drawRectangle

public void drawRectangle(java.awt.Point pt1,
                          java.awt.Point pt2)
Draws a rectangle with given opposite corners.

Parameters:
pt1 - upper left vertex of the rectangle
pt2 - lower right vertex of the rectangle

fillRectangle

public void fillRectangle(java.awt.Point pt1,
                          java.awt.Point pt2)
Draws a filled rectangle with given opposite corners. (The filling includes the complete outline of the rectangle.)

Parameters:
pt1 - upper left vertex of the rectangle
pt2 - lower right vertex of the rectangle

drawArc

public void drawArc(java.awt.Point pt,
                    int radius,
                    double startAngle,
                    double extendAngle)
Draws an arc with given center, radius, start and end angle.

Parameters:
pt - the center of the arc
radius - the radius of the arc
startAngle - the start angle in degrees (zero to east, positive counterclockwise)
extendAngle - the extend angle in degrees (zero to east, positive counterclockwise)

fillArc

public void fillArc(java.awt.Point pt,
                    int radius,
                    double startAngle,
                    double extendAngle)
Fills an arc with given center, radius, start and end angle.

Parameters:
pt - the center of the arc
radius - the radius of the arc
startAngle - the start angle in degrees (zero to east, positive counterclockwise)
extendAngle - the extendAngle in degrees (zero to east, positive counterclockwise)

drawPolygon

public void drawPolygon(java.awt.Point[] vertexes)
Draws a polygon with given vertexes.

Parameters:
vertexes - the vertexes of the polygon

fillPolygon

public void fillPolygon(java.awt.Point[] vertexes)
Draws a filled polygon with given vertexes.

Parameters:
vertexes - the vertexes of the polygon

drawGeneralPath

public void drawGeneralPath(java.awt.geom.GeneralPath gp)
Draws a figure defined by the given GeneralPath.

Parameters:
gp - the GeneralPath that defines the shape

fillGeneralPath

public void fillGeneralPath(java.awt.geom.GeneralPath gp)
Fills a figure defined by the given GeneralPath.

Parameters:
gp - the GeneralPath that defines the shape

drawPoint

public void drawPoint(java.awt.Point pt)
Draws a single point. If the lineWidth is greater than 1, more than one pixel may be involved.

Parameters:
pt - the point to draw

getColor

public java.awt.Color getColor(java.awt.Point pt)
Returns the color of the pixel of the background at given point.

Parameters:
pt - point, where to pick the color; zero at upper left corner, x to the left, y downwards
Returns:
the color at the selected point; Color.black, if pt is outside the bitmap

setFont

public void setFont(java.awt.Font font)
Sets the font for displaying text with setText()

Parameters:
font -

drawText

public void drawText(java.lang.String text,
                     java.awt.Point pt)
Displays the given text at the given position using the current font.

Parameters:
text - the text to display
pt - the start point of the text baseline

getAvailableFontFamilies

public static java.lang.String[] getAvailableFontFamilies()
Returns the available font families for the current platform.

Returns:
a string that describes the available font families

getContext

public java.awt.Graphics2D getContext()
Returns the graphics device context used for drawing operations.

Returns:
the Graphics2D reference

getBufferedImage

public java.awt.image.BufferedImage getBufferedImage()
Returns the reference of the buffered image used as bitmap.

Returns:
the buffered image holding the graphics

setPaintMode

public void setPaintMode()
Sets the paint mode of the graphics context to overwrite with current paint color.


setXORMode

public void setXORMode(java.awt.Color c)
Sets the paint mode to alternate between the current color and the given color. This specifies that logical pixel operations are performed in the XOR mode, which alternates pixels between the current color and a specified XOR color. When drawing operations are performed, pixels which are the current color are changed to the specified color, and vice versa. Pixels that are of colors other than those two colors are changed in an unpredictable but reversible manner; if the same figure is drawn twice, then all pixels are restored to their original values.


dispose

public void dispose()
Releases all resources of the graphics context.


getSupportedImageFormats

public static java.lang.String[] getSupportedImageFormats()
Returns all supported image formats.

Returns:
a string array containing all image format descriptions of the current platform.

writeImage

public static boolean writeImage(java.awt.image.BufferedImage bi,
                                 java.lang.String filename,
                                 java.lang.String type)
Writes a image file of the given BufferedImage. Normally the following image formats are supported: bmp, gif, jpg, png, but call getSupportedImageFormats() to find all formats supported by the current platform.

Parameters:
bi - the given BufferedImage to copy
filename - the path to the image file
type - the image file format like "bmp", "gif", "jpg", "png" (all lowercase)
Returns:
true, if the format is supported and the file was successfully written; otherwise false

floodFill

public static java.awt.image.BufferedImage floodFill(java.awt.image.BufferedImage bi,
                                                     java.awt.Point pt,
                                                     java.awt.Color oldColor,
                                                     java.awt.Color newColor)
Fills a bounded single-colored region with the given color. The given point is part of the region and used to specify it.

Parameters:
bi - the BufferedImage containing the connected region
pt - a point inside the region
oldColor - the old color of the region
newColor - the new color of the region
Returns:
a new BufferedImage with the transformed region, the given BufferedImage remains unchanged

setTransparency

public static java.awt.image.BufferedImage setTransparency(java.awt.image.BufferedImage bi,
                                                           double factor)
Returns a BufferedImage where each pixel has a new transparency value (alpha component in the ARGB color model). The new transparency value is the old value multiplied by the transparency multiplier (limited to 0..255).

Parameters:
bi - the original image
factor - the transparency multiplier
Returns:
the transformed image

getByteArray

public static byte[] getByteArray(java.awt.image.BufferedImage sourceImage,
                                  java.lang.String imageFormat)
Returns the image in a byte array. imageFormat is the informal name of the format (one of the strings returned by get SupportedImageFormats()).

Returns:
the image data as byte array, null if the image format is not supported