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 will automatically do most of the work
and then call paint()
in the myFrame
object to do whatever
extra the programmer wants.
The work that is automatically done is a great deal of work---involving
all the graphics of Java and the operating system and control of the graphics
board.
At the tail end of all 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 paint()
is called, the system will supply this
parameter.
One of its methods is drawString( String st, int X, int Y )
which will draw a string on the graphics area at location
starting X pixels from the
left and Y pixels from the top.
Here is the MyFrame
again, with a few changes.
class myFrame extends Frame { public void paint ( Graphics g ) { g.drawString(________, __________, ________); } }