Lesson 3:


Two Applet Methods:

  1. 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.
  2. public void paint(Graphics g) { .. } is executed every time the applet needs to be redrawn on the screen.  
     

Three types of variables :

  1. Local variables  are declared inside a method and  can only be used inside that method
  2. Instance variables are declared outside of the class methods and can be used in any of the methods.
  3. 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:

  1. 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.
  2. Make a project for Lesson3 and run the applet.  Try resizing the applet.  What happened and why?
  3. 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.
  4. 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?