Class gpjpp.GPDrawing
All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class gpjpp.GPDrawing

java.lang.Object
   |
   +----java.awt.Component
           |
           +----java.awt.Container
                   |
                   +----java.awt.Window
                           |
                           +----java.awt.Frame
                                   |
                                   +----gpjpp.GPDrawing

public class GPDrawing
extends Frame
Used to draw graphic images of the results of the genetic algorithm. Most commonly, it is used to draw a graphic rendition of genetic program trees. It can also be used to draw other images such as the path followed by a mower in the lawnmower problem. Because the rest of gpjpp is designed without any user interface, this class attempts to shoehorn graphics into the library with minimal side effects, which turned out to be surprisingly difficult. The technique chosen was to create a subclass of Java's standard AWT Frame class, to add a single Canvas to this frame, and to create an off-screen Image where the drawing is performed relative to the Canvas. Then the freeware class Acme.JPM.Encoders.GifEncoder is used to write the graphic image to a gif file for viewing in a web browser, drawing program, or word processor.

In my experience using Java console applications running under the Microsoft J++ 1.1 VM in Windows 95, creating a GPDrawing object causes the console window to temporarily lose focus until the GPDrawing.dispose() method (inherited from Frame) is called. See GPRun.showFinalGeneration for an example. Although the effect can be disconcerting, a mouse click returns focus to any window you desire without disturbing the drawing process. Numerous experiments have not uncovered a solution. The same effect does not occur under the Sun VM.

The GPGenePrint and LawnGP classes show examples of drawing images with GPDrawing. If you just want to create gif files of the genetic trees, set PrintTree to true in your configuration file and don't worry about the rest.

Version:
1.0

Variable Index

 o as
The maximum ascent of any character in the current font.
 o ch
The height of the current font.
 o cvs
The canvas on which the image is drawn.
 o cw
The width of the widest character in the current font.
 o defFontSize
The default font size used for the font.
 o fm
Font metrics for the active font.
 o fnt
The active font for the image.
 o fontName
The typeface used to display text in genetic trees.
 o fontSize
The current font size.
 o gra
A graphics context whose methods are called to produce the off-screen image.
 o img
An off-screen image associated with the canvas on which the drawing is created.

Constructor Index

 o GPDrawing()
Public null constructor for this class.

Method Index

 o getGra()
Returns the graphics context on which drawing is done.
 o prepImage(int, int)
Prepares an off-screen drawing image of the specified pixel width and height.
 o prepImage(int, int, int)
Sets a new font size and prepares an off-screen drawing image.
 o setFontSize(int)
Changes the font size.
 o stringWidth(String)
Returns the pixel width of a text string when displayed in the default font.
 o writeGif(String)
Writes the current off-screen image to a gif file.

Variables

 o fontName
  public static String fontName
The typeface used to display text in genetic trees. This should be a monospace font. Courier was chosen as a default because it is a standard Java font that should be available on all platforms. There's nothing that limits you to just one font for other applications, but GPDrawing enables this one and gets various font metrics that are useful while drawing.
 o defFontSize
  public static int defFontSize
The default font size used for the font. This can be changed while constructing the object or by calling setFontSize. Default value 12.
 o cvs
  protected Canvas cvs
The canvas on which the image is drawn.
 o fnt
  protected Font fnt
The active font for the image.
 o fm
  protected FontMetrics fm
Font metrics for the active font.
 o img
  protected Image img
An off-screen image associated with the canvas on which the drawing is created.
 o gra
  protected Graphics gra
A graphics context whose methods are called to produce the off-screen image.
See Also:
getGra
 o fontSize
  protected int fontSize
The current font size.
 o cw
  public int cw
The width of the widest character in the current font. It's public so that users of the class can get the value efficiently.
 o ch
  public int ch
The height of the current font. It's public so that users of the class can get the value efficiently.
 o as
  public int as
The maximum ascent of any character in the current font. It's public so that users of the class can get the value efficiently.

Constructors

 o GPDrawing
  public GPDrawing()
Public null constructor for this class. It creates a Frame with the title "gif drawing", adds a Canvas to it, creates the peer components, creates the default font at default size, and gets the cw, ch, and as font metrics. The frame remains hidden and disabled.

Methods

 o setFontSize
  public void setFontSize(int fontSize)
Changes the font size. The typeface is specified by the fontName field of the class. Updates the fm, cw, ch, and as fields of the class.
 o prepImage
  public void prepImage(int w,
                        int h)
Prepares an off-screen drawing image of the specified pixel width and height. The image is cleared to a white background, the default font is enabled, and the foreground color is set to black.
 o prepImage
  public void prepImage(int w,
                        int h,
                        int fontSize)
Sets a new font size and prepares an off-screen drawing image.
See Also:
prepImage
 o stringWidth
  public int stringWidth(String s)
Returns the pixel width of a text string when displayed in the default font.
 o getGra
  public Graphics getGra()
Returns the graphics context on which drawing is done. See java.awt.Graphics for a complete list of available drawing methods. prepImage() must be called first.
 o writeGif
  public void writeGif(String fname) throws IOException
Writes the current off-screen image to a gif file.
Parameters:
fname - the name of the gif file to create.
Throws: IOException
if an error occurs while writing the file.

All Packages  Class Hierarchy  This Package  Previous  Next  Index