JTP
Class Geom

java.lang.Object
  extended by JTP.Geom

public class Geom
extends Object

Geometry related support methods

Author:
V.Termanis

Constructor Summary
Geom()
           
 
Method Summary
static Point absPos(ScreenFrames frames, Point p, int tileHeight)
          Calculates absolute location (in pixels) of centre of tile face.
static Point calcCharOnTileOffset(char c, Font f, ScreenFrames frames, Graphics2D g)
          Calculates offset for drawing a single character on the face of a tile
static Dimension calcMaxLineDimensions(String[] lines, Font f, Graphics2D g)
          Calculates maximum line height and width for a set lines of text.
static Rectangle calcTextBounds(String s, Font f, Graphics2D g)
          Calculates space taken up by given string and its offset
static Square calcTileClip(ScreenFrames frames, Point tile)
          Calculates maximum clip area for one tile
static Rectangle calcTilesClip(ScreenFrames frames, Point tile1, Point tile2)
          Calculates maximum clip area for two tiles
static void drawChar(char c, int x, int y, Color clr, Graphics2D g)
          Draws a character at the given location in the specified color
static void drawGrid(Rectangle gridArea, int gridSize, int tileSize, Color c, Graphics2D g)
          Draws grid in editor
static void drawImage(BufferedImage img, Rectangle r, Color bgColor, Graphics2D g)
          Renders an image at the specified location, clearing the background first with the specified color.
static void drawLineRel(ScreenFrames frames, Point relP1, int p1Height, Point relP2, int p2Height, Color c, Graphics2D g)
          Draws a line between tile locations on grid (relative positioning dependant on tile height)
static void drawNumber(int n, int x, int y, Color c, Graphics2D g)
          Draws a number at the given location in the specified color
static void drawRect(Rectangle r, Color c, Graphics2D g)
          Draws a rectangle.
static void drawRoundRect(Rectangle r, int arc, Color c, Stroke s, Graphics2D g)
          Draws a rectangle with rounded edges.
static void drawString(String s, int x, int y, Color c, Graphics2D g)
          Draws a string at the given location in the specified color
static void drawString(String s, Point p, Color c, Graphics2D g)
          Draws a string at the given location in the specified color
static void drawString(String s, Point p, Point offset, Color c, Graphics2D g)
          Draws a string at the given location with an offset.
static void fillRect(Rectangle r, Color c, Graphics2D g)
          Draws a filled rectangle.
protected static void fillRect(Rectangle r, int topCrop, int bottomCrop, Color c, Graphics2D g)
          Modified version of fillRect() for use in editor for correct cropping
static void fillRectMax(Rectangle r, Color c, Graphics2D g)
          Fills a rectangle exactly as specified in Graphics2D.fillRect()
static void fillRectOversize(Rectangle r, Color c, Graphics2D g)
          Like fillRectMax except offset by one pixel horizontally and vertically, i.e.: x - 1, y - 1, width + 2, height + 2.
static void fillRoundRect(Rectangle r, int arc, Color c, Graphics2D g)
          Fills a rectangle with rounded edges.
static String getMaxString(String s, String addition, int maxWidth, Font f, Graphics2D g)
          Calculates maximum substring which can fit in the given absolute width
static int getTileButtonWidth(String s, boolean is3D, ScreenFrames frames)
          Calculates width required for a button rendered with tiles
static Point gridPos(ScreenFrames frames, Point mouseLocation)
          Calculates relative grid position given absolute mouse location
static Square tilePosToArea(ScreenFrames frames, Point tile)
          Calculates absolute locations and size of a tile
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Geom

public Geom()
Method Detail

drawLineRel

public static void drawLineRel(ScreenFrames frames,
                               Point relP1,
                               int p1Height,
                               Point relP2,
                               int p2Height,
                               Color c,
                               Graphics2D g)
Draws a line between tile locations on grid (relative positioning dependant on tile height)

Parameters:
frames - dimensions required for calculating absolute locations
relP1 - relative position of 1st point
p1Height - height of pile for 1st location
relP2 - relative position of 2nd point
p2Height - height of pile for 2st location
c - color of the line
g - graphics context for drawing

drawRect

public static void drawRect(Rectangle r,
                            Color c,
                            Graphics2D g)
Draws a rectangle. Note: The resulting rectangle will be 1 pixel smaller in both directions than if Graphics2D version of drawRect were called.

Parameters:
r - size and location of rectangle to draw
c - color of rectangle outline
g - graphics context for drawing

fillRect

public static void fillRect(Rectangle r,
                            Color c,
                            Graphics2D g)
Draws a filled rectangle.

Parameters:
r - size and location of rectangle to draw
c - color of rectangle
g - graphics context for drawing

fillRect

protected static void fillRect(Rectangle r,
                               int topCrop,
                               int bottomCrop,
                               Color c,
                               Graphics2D g)
Modified version of fillRect() for use in editor for correct cropping


fillRectMax

public static void fillRectMax(Rectangle r,
                               Color c,
                               Graphics2D g)
Fills a rectangle exactly as specified in Graphics2D.fillRect()

Parameters:
r - size and location of rectangle to draw
c - color of rectangle outline
g - graphics context for drawing

fillRectOversize

public static void fillRectOversize(Rectangle r,
                                    Color c,
                                    Graphics2D g)
Like fillRectMax except offset by one pixel horizontally and vertically, i.e.: x - 1, y - 1, width + 2, height + 2.

Parameters:
r - size and location of rectangle to draw
c - color of rectangle outline
g - graphics context for drawing

fillRoundRect

public static void fillRoundRect(Rectangle r,
                                 int arc,
                                 Color c,
                                 Graphics2D g)
Fills a rectangle with rounded edges. Graphics2D.fillRect()

Parameters:
r - size and location of rectangle to draw
arc - amount of curvature (see Graphics2D.fillRoundRect() for more details)
c - color of rectangle
g - graphics context for drawing

drawRoundRect

public static void drawRoundRect(Rectangle r,
                                 int arc,
                                 Color c,
                                 Stroke s,
                                 Graphics2D g)
Draws a rectangle with rounded edges. Graphics2D.fillRect()

Parameters:
r - size and location of rectangle to draw
arc - amount of curvature (see Graphics2D.fillRoundRect() for more details)
c - color of rectangle outline
g - graphics context for drawing

drawChar

public static void drawChar(char c,
                            int x,
                            int y,
                            Color clr,
                            Graphics2D g)
Draws a character at the given location in the specified color

Parameters:
c - character to draw
x - absolute horizontal position
y - absolute vertical position
clr - color of character
g - graphics context for drawing

drawNumber

public static void drawNumber(int n,
                              int x,
                              int y,
                              Color c,
                              Graphics2D g)
Draws a number at the given location in the specified color

Parameters:
n - number to draw
x - absolute horizontal position
y - absolute vertical position
c - color of character
g - graphics context for drawing

drawString

public static void drawString(String s,
                              int x,
                              int y,
                              Color c,
                              Graphics2D g)
Draws a string at the given location in the specified color

Parameters:
s - string to draw
x - absolute horizontal position
y - absolute vertical position
c - color of character
g - graphics context for drawing

drawString

public static void drawString(String s,
                              Point p,
                              Color c,
                              Graphics2D g)
Draws a string at the given location in the specified color

Parameters:
s - string to draw
p - absolute position
c - color of character
g - graphics context for drawing

drawString

public static void drawString(String s,
                              Point p,
                              Point offset,
                              Color c,
                              Graphics2D g)
Draws a string at the given location with an offset. This is useful once the offset has been calculated (see Geom.calcTextBounds())

Parameters:
s - string to draw
p - absolute position
offset - deviation from absolute position
c - color of character
g - graphics context for drawing

drawImage

public static void drawImage(BufferedImage img,
                             Rectangle r,
                             Color bgColor,
                             Graphics2D g)
Renders an image at the specified location, clearing the background first with the specified color.

Parameters:
img - image to render
r - size of image and absolute position for rendering
bgColor - color with which to clear area first
g - graphics context for drawing

drawGrid

public static void drawGrid(Rectangle gridArea,
                            int gridSize,
                            int tileSize,
                            Color c,
                            Graphics2D g)
Draws grid in editor

Parameters:
gridArea - where grid starts on screen
gridSize - specifies number of rows and columns to draw
tileSize - size of each grid square
c - color of the grid
g - graphics context for drawing

tilePosToArea

public static Square tilePosToArea(ScreenFrames frames,
                                   Point tile)
                            throws NullPointerException
Calculates absolute locations and size of a tile

Parameters:
frames - dimensions required for calculating absolute locations
tile - relative position on grid
Returns:
the absolute size and position of the tile on the screen
Throws:
NullPointerException - if either of the parameters is null.

calcTileClip

public static Square calcTileClip(ScreenFrames frames,
                                  Point tile)
Calculates maximum clip area for one tile

Parameters:
frames - dimensions required for calculating absolute locations
tile - relative position on grid
Returns:
absolute clip area on the screen

calcTilesClip

public static Rectangle calcTilesClip(ScreenFrames frames,
                                      Point tile1,
                                      Point tile2)
Calculates maximum clip area for two tiles

Parameters:
frames - dimensions required for calculating absolute locations
tile1 - 1st tile
tile2 - 2nd tile
Returns:
absolute clip area on the screen

gridPos

public static Point gridPos(ScreenFrames frames,
                            Point mouseLocation)
Calculates relative grid position given absolute mouse location

Parameters:
frames - dimensions required for calculating absolute locations
mouseLocation - absolute location of mouse cursor
Returns:
relative position on grid, or null if mouse not on grid

absPos

public static Point absPos(ScreenFrames frames,
                           Point p,
                           int tileHeight)
Calculates absolute location (in pixels) of centre of tile face. This is used to draw teleport lines in editor

Parameters:
frames - dimensions required for calculating absolute locations
p - relative grid position
tileHeight - height of pile at given position
Returns:
absolute location of centre of tile face

calcTextBounds

public static Rectangle calcTextBounds(String s,
                                       Font f,
                                       Graphics2D g)
Calculates space taken up by given string and its offset

Parameters:
s - string to calculate required space for
f - font to calculate space usage for
g - graphics context for off-screen rendering

calcCharOnTileOffset

public static Point calcCharOnTileOffset(char c,
                                         Font f,
                                         ScreenFrames frames,
                                         Graphics2D g)
Calculates offset for drawing a single character on the face of a tile

Parameters:
c - the character for which to calculate the offset
f - font to calculate offset for
frames - dimensions required for calculating absolute locations
g - graphics context for off-screen rendering

getMaxString

public static String getMaxString(String s,
                                  String addition,
                                  int maxWidth,
                                  Font f,
                                  Graphics2D g)
Calculates maximum substring which can fit in the given absolute width

Parameters:
s - the string for which to calculate fitting substring
addition - string to append to substring if all will not fit
maxWidth - maximum width of whole string in pixels
f - font to calculate dimensions for
g - graphics context for off-screen rendering

getTileButtonWidth

public static int getTileButtonWidth(String s,
                                     boolean is3D,
                                     ScreenFrames frames)
Calculates width required for a button rendered with tiles

Parameters:
s - string which will be rendered on button
is3D - indicates whether button is raised or not
frames - dimensions required for calculating absolute locations
Returns:
width in pixels of button

calcMaxLineDimensions

public static Dimension calcMaxLineDimensions(String[] lines,
                                              Font f,
                                              Graphics2D g)
Calculates maximum line height and width for a set lines of text. Useful for spacing lines with the exact same spacing

Parameters:
lines - text to analyze
f - font with which to calculate text dimensions
Returns:
maximum dimensions for each line of text