In simple applets, so far we have used the
applet's ‘paint()' method to display text or graphics in the applet. However, in this
unit we said that this was a bad idea. Rather than displaying graphics directly on an
applet, it was suggested that you should create a new object and place this in an applet.
For example, we could create a new object which is a subclass of Canvas
(a blank area of screen) and use its paint
method to do the drawing. This object could then be inserted in the applet using
‘add'. A skeleton of a program that works this way may look something like this:
Class
MyApplet extends Applet
{
public
MyApplet()
{
//
This is the constructor.
// Create a new object of class Drawing and put
it in
//
the applet
add(new
Drawing());
}
}
class
Drawing
{
public
void paint (Graphics g)
{
//
All the drawing methods go here….
}
}
So in this example, all the drawing methods
are in the class Drawing and the applet itself has no graphics methods.
This approach leads to a longer program, and
it is more complex (at least superficially). So what are the advantages of this
programming strategy?
Discussion
Back
to top

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg |