It is legal to do this ― but the text will appear in the
command line window where println()
has always put text,
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, for a child object, the new method will be used in place of the parent's method.
When the Java system needs to paint a MyFrame
object
it first draws most of the picture
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.
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 provides a value for this parameter.
One of the methods of Graphics
is
drawString(String st, int X, int Y)
Here is MyFrame
again, with a few changes.
Fill in the blanks so that when a MyFrame
object is drawn it will
contain "Hello" written at the extreme left about halfway
down (assume that the frame is width=150, height=100).