paint()
method that produces almost the expected results. Start from Lecture Notes 25 (or lab notes from last week).
Work with class AWTExample2
.
Add a call to
repaint();
at the end of actionPerformed()
, and the following method
as an instance method:
This will essentially bring your output into thepublic void paint(Graphics g) { if (myList == null) { g.drawString("Please start by pressing New", 30, 100); } else if (myList.first == null) { g.drawString("Your list is empty.", 30, 100); } else { g.drawString(myList.first.show(myList.current), 30, 100); } }
Frame
.
You will still be displaying a String
, but at least you will
be doing it through your paint()
method. Now all you need to do
is to define a show
function that draws squares and arrows,
instead of returning a String
.
You will need to pass the graphics context to that method, otherwise it will not know where to draw.
As a final comment please remember that you never invoke
paint()
directly.
It will be called for you in the right context.
All you can do is to provide a definition for it, a definition that has a placeholder (the graphics context that is its formal parameter) in it, and which will be filled in appropriately upon its invocation.
The most you can do is to indirectly request that paint()
be
invoked, and we do this through repaint()
. You will note that
we inherit this method from Frame
or one of its superclasses
(from class Component
to be more precise).
Note that the purpose of paint()
is that of giving the user a
picture of the current situation. It is completely separate from any actions
on the OneWayList
that the user is working with.
Changes to the list are performed by actionPerformed()
, which
calls repaint()
, and that in turn erases the drawable area and
then asks paint()
to paint the current status again.