{ (define LIBNAME "A Functional Drawing Library (HtDC)") (include "head.tinc") }
Add
import draw.*
at the top of your Definitions Window to import this library.
This draw
package provides classes and methods for a visual
world. Here is its class diagram of public fields and methods:
import colors.*;
import geometry.*;
+-----------------------------------+
| abstract World |
+-----------------------------------+ +---------------------------------------+
| Canvas theCanvas |------>| Canvas |
+-----------------------------------+ +---------------------------------------+
| boolean bigBang(int,int,double) | +---------------------------------------+
| boolean endOfTime(String) | | boolean show() |
| World endOfWorld(String) | | boolean close() |
| | | boolean drawCircle(Posn,int,IColor) |
| | | boolean drawDisk(Posn,int,IColor) |
| abstract World onTick() | | boolean drawRect(Posn,int,int,IColor) |
| abstract World onKeyEvent(String) | | boolean drawLine(Posn,Posn,IColor) |
| abstract boolean draw() | | boolean drawString(Posn,String) |
+-----------------------------------+ +---------------------------------------+
The abstract World
class exports the following methods:
bigBang(width,height,speed)
, which initializes the world,
associates it with a width
x height
Canvas
, displays
this canvas, enables keyevents, and finally starts the clock at a rate of one tick per
speed
seconds. If it succeeds with all of its actions, the method
produces true
.
Note: width
and height
must be
positive int
s, speed
must be a positive
double
.
The canvas in World
is called
theCanvas
. References to a "canvas" in conjunction with the
World
class denote this default canvas.
endOfTime
, displays the given message, stops the clock and, if it succeeds, produces
true
. After the end of time, events no longer trigger calls
to onTick
or onKeyEvent
(see below). The canvas
remains visible.
endOfWorld
, displays the given message, stops the clock and, if it succeeds, produces the
last World
. After the end of the world, events no longer trigger calls
to onTick
or onKeyEvent
(see below). The canvas
remains visible.
A derived concrete class must supply definitions for the following methods:
onTick()
, which is invoked for every tick of the clock. Its
purpose is to create a World
whose differences with
this
one represent what happened during the amount of time it takes
the clock to tick.
onKeyEvent(key)
, which is invoked for every keyboard event
associated with the canvas. Its purposes is to create a
World
whose differences with this
one represent
what happens due to the user's use of the keyboard. The latter is
represented with the string-valued argument key
.
draw()
, which is invoked after one of the two event
handlers has been called. Its purpose is to present this World
graphically on its canvas. If it succeeds, its result is true.
World
. If it does, the event handlers are called in a unpredictable
order.
To create an instance of the Canvas
class, a program must supply
two int
values: one for the width of the canvas and one for its
height. The canvas is a rectangle, whose borders are parallel to the computer
screen's borders. A program can use the following methods on instances of
Canvas:
show()
, which initializes the canvas to a white area,
enables the drawing methods, and finally displays the canvas. If it
succeeds, it produces true
. Invoking the method a second
time without calling close
before has no effect.
close()
, which hides the canvas and erases the current content.
If it succeeds, it produces true
.
Closing the Canvas using the display controls does not fully hide the
canvas; it is still necessary to invoke close
before
show
is re-enabled.
drawCircle(p,r,c)
, which draws a circle on this
Canvas
at p
with radius r
and color
c
. If it succeeds, it produces true
.drawDisk(p,r,c)
, which draws a disk on
this Canvas
at p
with radius r
and color
c
. If it succeeds, it produces true
.drawRect(p,w,h, c)
, which draws a solid rectangle on this
Canvas
at p
with width w
, height
h
, and color c
. The rectangle's lines are parallel to
the canvas's borders. If it succeeds, it produces true
.drawLine(p0,p1,c)
, which draws a line on this
Canvas
from p0
to p1
using color
c
. If it succeeds, it produces true
.drawString(p,s)
, which draws the string s
at
p
on this Canvas
. If it succeeds, it produces
true
.