It is legal to do this ― but the text will appear in the
DOS window where println
has always put it,
not in the frame where you want it.
drawString()
methodOrdinarily, a child class inherits every method defined in its parent class. A child overrides a method in its parent by defining a new method with the same signature. Now the new method will be used in place of the parent's method in a child object.
When the Java system needs to paint a MyFrame
object
it first does most of the work
and then calls paint()
in the MyFrame
object to do whatever
extra the programmer wants.
The first part of the work is vital.
It involves interacting with the graphics
of the computer system and controling the graphics board.
After all of that you get to ask for the little extra bit that
you are interested in.
Our paint()
looks like this:
public void paint ( Graphics g ) { // draw a String at location x=10 y=50 g.drawString("A MyFrame object", 10, 50 ); }
The parameter g
is a reference to a Graphics
object.
The Graphics
object represents the part of the frame that you
can draw on.
When the system calls paint()
,
the system gives it this parameter.
One of its methods is
drawString(String st, int X, int Y)
Here is MyFrame
again, with a few changes.
class MyFrame extends Frame { public void paint ( Graphics g ) { g.drawString(________, __________, ________); } }