new SubCanvas( this ) new SubCanvas( this, width-in-pixels, height-in-pixels)
Since it is difficult to control the placement of components within the applet's region of the screen, it can be tricky to use the drawing methods associated with the applet's pen when components are also being used. Components are logically placed "above" the pen's drawing surface. Any lines, shapes or text drawn on an area of the screen in which a component has been placed will be obscured by the component. As a result, it is usually not a good idea to use the applet's pen drawing routines when components have been added to an applet's display.
To provide a means to write applets that display information using both user interface components and drawing methods, JavaTools provides a type of component called a SubCanvas. A SubCanvas is intended to provide a "blank slate" onto which you can draw. Each SubCanvas has its own pen. Using the pen associated with a SubCanvas, you can draw on the surface of the SubCanvas using any of the drawing methods available with the applet's pen. When you draw with a SubCanvas' pen, however, the coordinate system used is relative to the SubCanvas with the point (0,0) at the upper left corner of the SubCanvas. So, you can draw within the SubCanvas without knowing exactly where it has been placed within the applet's display.
To use the pen associated with a SubCanvas, a name must be associated with the SubCanvas. Therefore, if an applet uses a SubCanvas it will typically include the declaration of a variable like:
Then, in the "begin" method instructions resemblingSubCanvas drawingBox;
would be included to actually create and add a SubCanvas to the applet.drawingBox = new SubCanvas(this,120,120); add(drawingBox)
Given a name for a SubCanvas, the pen associated with the SubCanvas is referenced by adding ".pen" to the name of the SubCanvas. So, to draw on the SubCanvas that would be created by the example code just shown, one might include the method invocation:
drawingBox.pen.frameOval(20,20,80,80);
Normally, the constructor method for SubCanvases is called with two integer parameters specifying the desired width and height. A version expecting no parameters can be used, but this leaves the determination of the drawing area provided up to the layout manager for the applet.
There are no event handling methods associated with SubCanvases and there are no methods that can be invoked to interact with a SubCanvas other than the drawing methods associated with its pen.