Java Reference
In-Depth Information
be thrown by
and so must be accounted for, in our case by a simple
Thread.sleep
block. The class
is in the
package and so
catch
InterruptedException
java.lang
requires no import statement.
Thread.sleep
Thread.sleep
that pauses the thread that includes
the invocation. It pauses for the number of milliseconds (thousandths of a second) given as
an argument.
The method
is a static method in the class
Thread
Thread.sleep
may throw an
InterruptedException
, which is a checked
exception and so must be either caught in a
catch
block or declared in a
throws
clause.
The classes
InterruptedException are both in the package java.lang , so
neither requires any import statement.
Note that Thread.sleep can be invoked in an ordinary (single thread) program of the kind
we have seen before this chapter. It will insert a pause in the single thread of that program.
SYNTAX
Thread.sleep( Number_Of_Milliseconds );
EXAMPLE
try
{
Thread.sleep(100); //Pause of 1/10 of a second
}
catch (InterruptedException e)
{
System.out.println("Unexpected interrupt");
}
Thread
and
The
Method
getGraphics
The other new method in Display 19.1 is the
method, which is used in
getGraphics
the following line from the method
:
fill
getGraphics
Graphics g = box.getGraphics( );
method is almost self-explanatory. As we already noted in Chapter
18, almost every item displayed on the screen (more precisely, every
The
getGraphics
) has an
JComponent
associated
object. The method
is an accessor method that returns
Graphics
getGraphics
the associated
object (of the calling object for
), in this case, the
Graphics
getGraphics
object associated with the panel box. This gives us a
object that can
Graphics
Graphics
draw circles (or anything else) in the panel box.
We still need to say a bit more about why the program in Display 19.1 makes you
wait before it will respond to the close-window button, but otherwise this concludes
our explanation of Display 19.1. The rest of the code consists of standard things we
have seen before.
Search WWH ::




Custom Search