|
|
|||||||||||||||||||||||||||||
|
Drawing to a CanvasThis example shows how to draw to a Canvas from Jython. This is low level drawing using the basic Java Graphics API's.
The complete source code for this example is included below. This example defines three classes. The first is the actual applet, CoordinatesDemo sets up the label where text is displayed, and the FramedArea panel where drawing takes place. This is an example of using the GridBag utility class provided with Jython to make using the GridBagLayout style easier. This is a very powerful layout manager, and I strongly encourage you to consider using it for your own applications. Hopefully the GridBag utility class will make this powerful layout manager much easier to use in Jython. The second class is a very simple framed area. It simply draws a border around an interior CoordinateArea. The final class is the CoordinateArea which does the actual drawing. At creation time, this class sets up the callback so that its push method will be invoked when the user presses the mouse within its frame. The push method simply updates the current location of the point (setting it if it hasn't been defined yet). The paint method does the actual drawing (only if the user has clicked on some point). It first updates its controller's label widget with the current coordinates, and then it draws the point the user has selected using the fillRect method on the Graphics object.
|