Lesson 3:
Two Applet Methods:
- public void init() { ... } is executed exactly one time when
an applet is first created. It is commonly used for setting the
colors in the applet and adding the widgets into the applet.
- public void paint(Graphics g)
{ .. } is executed every time the applet needs to be redrawn on the
screen.
Three types of variables :
- Local variables are declared inside a method and can
only be used inside that method
- Instance variables are declared outside of the class methods
and can be used in any of the methods.
- Class variables are declared with the keyword static and are
part of the class rather than part of an object. They are used
by ClassName.variableName
Try this:
- Take a look at the file Lesson3.java from the web page. Identify all
the local and instance variables. Hint: There are three instance
variables and one local variable.
- Make a project for Lesson3 and run the applet. Try resizing the
applet. What happened and why?
- Look at the documentation for the Colors class and notice all the class
variables like yellow. How do we know that they are class
variables? Look at how the color yellow is used in the init
method. See if you can figure out by looking at the documentation for
the TextField class how to change the colors of the TextFields. Try
it out.
- Change the paintText variable to be a local variable in the paint method
rather than an instance variable. What happens when you compile your
program and why?