1 Ht DP Teachpacks
2 Ht DC Teachpacks
On this page:
2.3.1 World
big Bang
end Of Time
end Of World
on Tick
on Key Event
draw
2.3.2 Canvas
show
close
draw Circle
draw Disk
draw Rect
draw Line
draw String
Version: 4.0.2

 

2.3 Draw: draw.*

Add

   import draw.*

at the top of your Definitions Window to import this library.

This 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                  |----+

    +-----------------------------------+    |

    | boolean bigBang(int,int,double)   |    |

    | boolean endOfTime(String)         |    |

    | World endOfWorld(String)          |    |

    |                                   |    |

    |                                   |    |

    | abstract World onTick()           |    |

    | abstract World onKeyEvent(String) |    |

    | abstract boolean draw()           |    |

    +-----------------------------------+    |

                                             |

                                             v

       +---------------------------------------+

       | Canvas                                |

       +---------------------------------------+

       +---------------------------------------+

       | boolean show()                        |

       | boolean close()                       |

       | boolean drawCircle(Posn,int,IColor)   |

       | boolean drawDisk(Posn,int,IColor)     |

       | boolean drawRect(Posn,int,int,IColor) |

       | boolean drawLine(Posn,Posn,IColor)    |

       | boolean drawString(Posn,String)       |

       +---------------------------------------+

Methods in these classes may fail due to the unavailability of the physical devices, inappropriate uses, etc. In those cases, they fail with an exception.

2.3.1 World

The abstract World class exports the following methods.

bigBang : (int width,int height,double speed)

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, height and speed must be a positive.

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. The canvas remains visible.

endOfWorld : (String msg)

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 : ()

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 : (String key)

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 : ()

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.

A program may, in principle, start several instances of (subclasses of) World. If it does, the event handlers are called in a unpredictable order.

2.3.2 Canvas

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 : ()

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 : ()

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 : (Posn p,int r,IColor c)

Draws a circle on thisCanvas] at p with radius r and color c. If it succeeds, it produces true.

drawDisk : (Posn p,int r,IColor c)

Draws a disk on thisCanvas] at p with radius r and color c. If it succeeds, it produces true.

drawRect : (Posn p,int w,int h,IColor c)

Draws a solid rectangle on thisCanvas] 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 : (Posn p0,Posn p1,IColor c)

Draws a line on thisCanvas] from p0 to p1 using color c. If it succeeds, it produces true.

drawString : (Posn p,String s)

Draws the string s at p on thisCanvas]. If it succeeds, it produces true.