uchicago.src.sim.gui
Class SimGraphics

java.lang.Object
  extended by uchicago.src.sim.gui.SimGraphics

public class SimGraphics
extends java.lang.Object

A Wrapper around java.awt.Graphics2D. Simplifies the drawing of circles, rectangles, and so forth. Rectangles are generally faster to draw than circles. The Displays should take care of layouts while objects that wish to be drawn as a shape need only call the appropriate method.

Many of the draw methods have a drawFast counterpart. This is due to speed problems with the Java2D api. In general the Java2D api is more flexible and usually looks nicer than its ordinary AWT counterpart. Moreover, somethings are easier to do with it (i.e. drawing borders). For now though, the drawFast methods are better.

Version:
$Revision: 1.11 $ $Date: 2005/07/12 20:56:14 $
Author:
Nick Collier

Constructor Summary
SimGraphics()
           
 
Method Summary
 void draw4ColorHollowRect(java.awt.Color top, java.awt.Color bottom, java.awt.Color left, java.awt.Color right)
          Draws the sides of a hollow rectangle in the four specifed colors.
 void drawCircle(java.awt.Color c)
          Draws a true circle with the specified color.
 void drawDirectedLink(java.awt.Color c, int fromX, int toX, int fromY, int toY)
          Draws a directed link (a line with a square head) from the specified coordinates to the specified coordinates in the specified color.
 void drawFastCircle(java.awt.Color c)
          Draws a circle of the specified color at the current coordinates faster than drawCircle.
 void drawFastOval(java.awt.Color c)
          Draws an oval in the specified color faster than drawOval.
 void drawFastRect(java.awt.Color color)
          Draws a rectangle at the current x and y coordinate with the current width and height in the specified color.
 void drawFastRoundRect(java.awt.Color c)
          Draws a rounded rectangle of the specified color, faster than drawRoundRect.
 void drawHollowFastOval(java.awt.Color color)
          Draws a hollow oval in the specified color faster than drawHollowOval.
 void drawHollowFastRect(java.awt.Color color)
          Draws a hollow rectangle of the specified color faster than drawHollowRect.
 void drawHollowFastRoundRect(java.awt.Color c)
          Draws a hollow rounded rectangle with the specified color, faster than drawHollowCircle.
 void drawHollowOval(java.awt.Color color)
          Draws a hollow oval in the specified color.
 void drawHollowRect(java.awt.Color color)
          Draws a hollow rectangle of the specified color.
 void drawHollowRoundRect(java.awt.Color c)
          Draws a hollow rounded rectangle of the specified color.
 void drawImage(java.awt.Image img)
          Draws the specified image.
 void drawImageScaled(java.awt.Image img)
          Draws the specified image, scaling the image according to the SimGraphic's current scale.
 void drawImageToFit(java.awt.Image img)
          Draws the specified image, scaling the image to the correct size if necessary.
 void drawLine(java.awt.Color color)
           
 void drawLink(java.awt.Color c, int fromX, int toX, int fromY, int toY)
          Draws a link (a line) from the specified coordinates to the specified coordinates in the specified color.
 void drawMultiPolygon(java.awt.Color color, int[] polys)
           
 void drawOval(java.awt.Color color)
          Draws an oval in the specified color.
 void drawOvalBorder(java.awt.BasicStroke stroke, java.awt.Color color)
          Draws an oval shaped border using the current drawing parameters.
 void drawPolygon(java.awt.Color color)
           
 void drawRect(java.awt.Color color)
          Draws a rectangle of the specified color.
 void drawRectBorder(java.awt.BasicStroke stroke, java.awt.Color color)
          Draws an rectangular border using the current drawing parameters.
 void drawRoundRect(java.awt.Color c)
          Draws a rounded rectangle of the specified color.
 void drawString(java.lang.String string, java.awt.Color stringColor)
          Draws a string using the current drawing parameters.
 void drawStringInHollowOval(java.awt.Color ovalColor, java.awt.Color stringColor, java.lang.String string)
          Draws the specified string in a hollow oval using the specified colors.
 void drawStringInHollowRect(java.awt.Color rectColor, java.awt.Color stringColor, java.lang.String string)
          Draws the specified string in a hollow rectangle using the specified colors.
 void drawStringInHollowRoundRect(java.awt.Color rectColor, java.awt.Color stringColor, java.lang.String string)
          Draws a string of the specified color in a hollow rounded rectangle of the specified color.
 void drawStringInOval(java.awt.Color ovalColor, java.awt.Color stringColor, java.lang.String string)
          Draws the specified string in an oval using the specified colors.
 void drawStringInRect(java.awt.Color rectColor, java.awt.Color stringColor, java.lang.String string)
          Draws the specified string in a rectangle using the specified colors.
 void drawStringInRoundRect(java.awt.Color rectColor, java.awt.Color stringColor, java.lang.String string)
          Draws the specified string inside a rounded rectangle.
 void fillPolygon(java.awt.Color color)
           
 int getCellHeightScale()
           
 int getCellWidthScale()
           
 int getCurHeight()
           
 int getCurWidth()
           
 int getDisplayHeight()
           
 int getDisplayWidth()
           
 java.awt.Graphics2D getGraphics()
          Gets the Graphics2D object around which this is a wrapper.
static SimGraphics getInstance()
           
 java.awt.geom.Rectangle2D getStringBounds(java.lang.String s)
          Get the bounding rectangle for the specified string using the current font.
 java.awt.geom.Rectangle2D getStringBounds(java.lang.String s, java.awt.Font f)
          Get the bounding rectangle for the specified string and the specified font.
 float getXScale()
           
 float getYScale()
           
 void setCurLineX(int[] xs)
           
 void setCurLineY(int[] ys)
           
 void setDisplaySurface(DisplaySurface surface)
          Sets the display surface associated with this SimGraphics instance.
 void setDrawingCoordinates(float x, float y, float z)
          Sets the coordinates for the next drawing operation.
 void setDrawingParameters(int width, int height, int depth)
          Sets the parameters for the next drawing operation.
 void setDrawingParametersNoScale(int width, int height, int depth)
          Sets the parameters for the next drawing operation.
 void setFont(java.awt.Font font)
          Sets the current font.
 void setGraphics(java.awt.Graphics2D g)
          Sets the wrapped graphics2D
 void setXScale(float scale)
           
 void setYScale(float scale)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimGraphics

public SimGraphics()
Method Detail

getInstance

public static SimGraphics getInstance()

setGraphics

public void setGraphics(java.awt.Graphics2D g)
Sets the wrapped graphics2D

Parameters:
g - the Graphics2D to wrap

getGraphics

public java.awt.Graphics2D getGraphics()
Gets the Graphics2D object around which this is a wrapper.


setDisplaySurface

public void setDisplaySurface(DisplaySurface surface)
Sets the display surface associated with this SimGraphics instance.

Parameters:
surface - the display surface on which this draws

getDisplayWidth

public int getDisplayWidth()

getDisplayHeight

public int getDisplayHeight()

setFont

public void setFont(java.awt.Font font)
Sets the current font.

Parameters:
font - the new font

getXScale

public float getXScale()

setXScale

public void setXScale(float scale)

getCellWidthScale

public int getCellWidthScale()

getYScale

public float getYScale()

setYScale

public void setYScale(float scale)

getCellHeightScale

public int getCellHeightScale()

setDrawingCoordinates

public void setDrawingCoordinates(float x,
                                  float y,
                                  float z)
Sets the coordinates for the next drawing operation. Z is ignored at this time.

Parameters:
x - the x coordinate
y - the y coordinate
z - the z coordinate

setDrawingParameters

public void setDrawingParameters(int width,
                                 int height,
                                 int depth)
Sets the parameters for the next drawing operation. This scales the specified width etc. appropriately.

Parameters:
width - the new width
height - the new height
depth - the new depth

setDrawingParametersNoScale

public void setDrawingParametersNoScale(int width,
                                        int height,
                                        int depth)
Sets the parameters for the next drawing operation. This performs no scaling and assumes that the caller has scaled the parameters appropriately.

Parameters:
width - the new width
height - the new height
depth - the new depth

getCurWidth

public int getCurWidth()

getCurHeight

public int getCurHeight()

drawImage

public void drawImage(java.awt.Image img)
Draws the specified image. This does not perform any scaling of the image and thus if you are using this to draw the image in a grid, the image might be too large for the grid cell. drawImageToFit will scale the image appropriately before drawing it.

Parameters:
img - the Image to draw.

drawImageToFit

public void drawImageToFit(java.awt.Image img)
Draws the specified image, scaling the image to the correct size if necessary.

Parameters:
img - the Image to draw.

drawImageScaled

public void drawImageScaled(java.awt.Image img)
Draws the specified image, scaling the image according to the SimGraphic's current scale.

This is different then drawImageToFit(Image) in that it doesn't scale it to the total size of the SimGraphics, it just scales it according to the general scaling. In a different phrasing, it scales the image according to the scaling of everything else, while drawImageToFit scales to fit in the total available size.

This is more appropriate for a non-grid display then drawImageToFit.

Parameters:
img - the Image to draw.

drawRoundRect

public void drawRoundRect(java.awt.Color c)
Draws a rounded rectangle of the specified color.

Parameters:
c - the color of the round rectangle

drawFastRoundRect

public void drawFastRoundRect(java.awt.Color c)
Draws a rounded rectangle of the specified color, faster than drawRoundRect. This uses fillRoundRect where drawRoundRect uses g2.fill(roundRect).

Parameters:
c - the color of the round rectangle

drawHollowRoundRect

public void drawHollowRoundRect(java.awt.Color c)
Draws a hollow rounded rectangle of the specified color.

Parameters:
c - the color of the round rectangle

drawHollowFastRoundRect

public void drawHollowFastRoundRect(java.awt.Color c)
Draws a hollow rounded rectangle with the specified color, faster than drawHollowCircle.

Parameters:
c - the color of the round rectangle

drawCircle

public void drawCircle(java.awt.Color c)
Draws a true circle with the specified color. The circle's radius will be current width / 2.

Parameters:
c - the color of the circle

drawFastCircle

public void drawFastCircle(java.awt.Color c)
Draws a circle of the specified color at the current coordinates faster than drawCircle. The circle's radius will be the current width / 2.

Parameters:
c - the color of the circle

drawDirectedLink

public void drawDirectedLink(java.awt.Color c,
                             int fromX,
                             int toX,
                             int fromY,
                             int toY)
Draws a directed link (a line with a square head) from the specified coordinates to the specified coordinates in the specified color. A small square will be drawn on the link close to the to side.

Parameters:
c - the color of the link
fromX - the x coordinate to draw from
toX - the x coordinate to draw to
fromY - the y coordinate to from
toY - the y coordinate to draw to

drawLink

public void drawLink(java.awt.Color c,
                     int fromX,
                     int toX,
                     int fromY,
                     int toY)
Draws a link (a line) from the specified coordinates to the specified coordinates in the specified color.

Parameters:
c - the color of the link
fromX - the x coordinate to draw from
toX - the x coordinate to draw to
fromY - the y coordinate to from
toY - the y coordinate to draw to

drawStringInRoundRect

public void drawStringInRoundRect(java.awt.Color rectColor,
                                  java.awt.Color stringColor,
                                  java.lang.String string)
Draws the specified string inside a rounded rectangle. The string will be clipped to fit inside the rectangle.

Parameters:
rectColor - the color of the rounded rectangle
stringColor - the color of the string
string - the string to draw

drawStringInHollowRoundRect

public void drawStringInHollowRoundRect(java.awt.Color rectColor,
                                        java.awt.Color stringColor,
                                        java.lang.String string)
Draws a string of the specified color in a hollow rounded rectangle of the specified color. The string is cliped to fit in the rectangle.

Parameters:
rectColor - the color of the circle
stringColor - the color of the String
string - the string to draw.

drawOval

public void drawOval(java.awt.Color color)
Draws an oval in the specified color.

Parameters:
color - the color to draw the oval with

drawFastOval

public void drawFastOval(java.awt.Color c)
Draws an oval in the specified color faster than drawOval.

Parameters:
c - the color to draw the oval with

drawHollowFastOval

public void drawHollowFastOval(java.awt.Color color)
Draws a hollow oval in the specified color faster than drawHollowOval.

Parameters:
color -

drawHollowOval

public void drawHollowOval(java.awt.Color color)
Draws a hollow oval in the specified color.

Parameters:
color - the color to draw the oval with

drawStringInOval

public void drawStringInOval(java.awt.Color ovalColor,
                             java.awt.Color stringColor,
                             java.lang.String string)
Draws the specified string in an oval using the specified colors. The string will be clipped to fit inside the oval.

Parameters:
ovalColor - the color of the oval
stringColor - the color of the string
string - the string to draw

drawStringInHollowOval

public void drawStringInHollowOval(java.awt.Color ovalColor,
                                   java.awt.Color stringColor,
                                   java.lang.String string)
Draws the specified string in a hollow oval using the specified colors. The string will be clipped to fit inside the oval.

Parameters:
ovalColor - the color of the oval
stringColor - the color of the string
string - the string to draw

drawOvalBorder

public void drawOvalBorder(java.awt.BasicStroke stroke,
                           java.awt.Color color)
Draws an oval shaped border using the current drawing parameters.

Parameters:
stroke - the stroke to draw the border with
color - the color of the border

getStringBounds

public java.awt.geom.Rectangle2D getStringBounds(java.lang.String s,
                                                 java.awt.Font f)
Get the bounding rectangle for the specified string and the specified font.

Parameters:
s - the string
f - the font
Returns:
the bounding rectangle for the string when drawn with the specified font

getStringBounds

public java.awt.geom.Rectangle2D getStringBounds(java.lang.String s)
Get the bounding rectangle for the specified string using the current font.

Parameters:
s - the string
Returns:
the bounding rectangle for the string when drawn with the current font.

drawFastRect

public void drawFastRect(java.awt.Color color)
Draws a rectangle at the current x and y coordinate with the current width and height in the specified color. This is faster than drawRect as it does a graphics.fillRect(...) and drawRect does a graphics.fill(rect) where rect is a Rectangle. As Java2D gets faster, this should no longer be necessary.

Parameters:
color - the color of the rectangle to draw

drawRect

public void drawRect(java.awt.Color color)
Draws a rectangle of the specified color. Uses the current width and height as set by setDrawingParameters.

Parameters:
color - the color of the rectangle

drawHollowRect

public void drawHollowRect(java.awt.Color color)
Draws a hollow rectangle of the specified color. Uses the current width and height as set by setDrawingParameters.

Parameters:
color - the color of the rectangle

draw4ColorHollowRect

public void draw4ColorHollowRect(java.awt.Color top,
                                 java.awt.Color bottom,
                                 java.awt.Color left,
                                 java.awt.Color right)
Draws the sides of a hollow rectangle in the four specifed colors. Uses the current with and height as set by setDrawingParameters.

Parameters:
top - the color of the top side
bottom - the color of the bottom side
left - the color of the left side
right - the color of the right side

drawHollowFastRect

public void drawHollowFastRect(java.awt.Color color)
Draws a hollow rectangle of the specified color faster than drawHollowRect. Uses the current width and height as set by setDrawingParameters.

Parameters:
color - the color of the rectangle

drawStringInRect

public void drawStringInRect(java.awt.Color rectColor,
                             java.awt.Color stringColor,
                             java.lang.String string)
Draws the specified string in a rectangle using the specified colors. The string will be clipped to fit inside the rectangle

Parameters:
rectColor - the color of the rectangle
stringColor - the color of the string
string - the string to draw

drawStringInHollowRect

public void drawStringInHollowRect(java.awt.Color rectColor,
                                   java.awt.Color stringColor,
                                   java.lang.String string)
Draws the specified string in a hollow rectangle using the specified colors. The string will be clipped to fit inside the rectangle

Parameters:
rectColor - the color of the rectangle
stringColor - the color of the string
string - the string to draw

drawRectBorder

public void drawRectBorder(java.awt.BasicStroke stroke,
                           java.awt.Color color)
Draws an rectangular border using the current drawing parameters.

Parameters:
stroke - the stroke to draw the border with
color - the color of the border

drawString

public void drawString(java.lang.String string,
                       java.awt.Color stringColor)
Draws a string using the current drawing parameters. The string will be clipped to the current width and height.

Parameters:
string - the string to draw
stringColor - the color the the string

setCurLineX

public void setCurLineX(int[] xs)

setCurLineY

public void setCurLineY(int[] ys)

drawLine

public void drawLine(java.awt.Color color)

drawPolygon

public void drawPolygon(java.awt.Color color)

fillPolygon

public void fillPolygon(java.awt.Color color)

drawMultiPolygon

public void drawMultiPolygon(java.awt.Color color,
                             int[] polys)