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
-
as
- The maximum ascent of any character in the current font.
-
ch
- The height of the current font.
-
cvs
- The canvas on which the image is drawn.
-
cw
- The width of the widest character in the current font.
-
defFontSize
- The default font size used for the font.
-
fm
- Font metrics for the active font.
-
fnt
- The active font for the image.
-
fontName
- The typeface used to display text in genetic trees.
-
fontSize
- The current font size.
-
gra
- A graphics context whose methods are called to produce the
off-screen image.
-
img
- An off-screen image associated with the canvas on which
the drawing is created.
-
GPDrawing()
- Public null constructor for this class.
-
getGra()
- Returns the graphics context on which drawing is done.
-
prepImage(int, int)
- Prepares an off-screen drawing image of the specified
pixel width and height.
-
prepImage(int, int, int)
- Sets a new font size and prepares an off-screen drawing
image.
-
setFontSize(int)
- Changes the font size.
-
stringWidth(String)
- Returns the pixel width of a text string when displayed
in the default font.
-
writeGif(String)
- Writes the current off-screen image to a gif file.
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.
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.
cvs
protected Canvas cvs
- The canvas on which the image is drawn.
fnt
protected Font fnt
- The active font for the image.
fm
protected FontMetrics fm
- Font metrics for the active font.
img
protected Image img
- An off-screen image associated with the canvas on which
the drawing is created.
gra
protected Graphics gra
- A graphics context whose methods are called to produce the
off-screen image.
- See Also:
- getGra
fontSize
protected int fontSize
- The current font size.
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.
ch
public int ch
- The height of the current font.
It's public so that users of the class can get the value
efficiently.
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.
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.
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.
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.
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
stringWidth
public int stringWidth(String s)
- Returns the pixel width of a text string when displayed
in the default font.
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.
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