More Applets

advertisement
Lec 17
Using Nested Loops and Objects
in an Applet Class
Agenda
• More Applet stuff
– Drawing a Brick Wall
• Constants (final)
• While Loops again
• Nested Loops
– Using objects in Applets
• Point object to locate LL corner of wall (and move it)
• Default values and null
Introduction to final
• You can make sure that a variable never gets changed once
it's initialized (i.e. contstants)
– Use final in the variable declaration to do that
• It means: ``Once this variable gets a value, it's final!''
– Let's try it out in an Applet that draws Bricks of certain height and
width, all the same (constant) size
– We use the all caps convention when declaring names for
constants
• Use underscores to separate multiple words in the variable name
– Can also combine static and final to get a class constant
– Constants can be made public (this is the one exception to the
``must be private'' rule for instance and class variables)
• If it's a constant, we know nobody will mess it up by accident (especially
ourselves)
Constants that pin down the size of a Brick
public static final int BRICK_WIDTH=30;
public static final int BRICK_HEIGHT=10;
...
...
...
public void paint(Graphics g) {
g.fillRect(100,100,BRICK_WIDTH,BRICK_HEIGHT);
g.fillRect(140,100,BRICK_WIDTH,BRICK_HEIGHT);
g.fillRect(180,100,BRICK_WIDTH,BRICK_HEIGHT);
g.fillRect(220,100,BRICK_WIDTH,BRICK_HEIGHT);
}
BUT this is kind of silly, we could use a loop to draw a row of bricks
The while Loop
• A control construct for specifying repetition
• General Structure:
while (condition) {
//Statements to be repeated
}
The Counting while loop
• Counting up
int i=initialValue;
while(i<endValue){
//statements to be repeated
i++;
}
We'll use while loops to draw our bricks
And, now that that works
• What if we want the wall to move around with
the arrow keys.
• Need instance variables, x and y
• OR a Point for lowerLeft corner of wall.
• Let's use Point since that lets us talk about null
Default values
• Every variable defaults to some fixed value
– For primitive variables:
• Variables of numerical type (double, float, int, byte, short, long) all
default to 0
• boolean variables default to false
– For reference (i.e. object) variables:
• Variables of any class type default to null
• Default values are a moot point for:
– local variables (which are required to be initialized before
being used)
– parameters (they are always initialized by the call to the
method)
– If you always initialize your static variables and instance
variables, you don't have to worry about this at all
null
• If a variable is null then it points to no object at all
– So if you try calling a method on a variable that points to
null, you get a runtime error
• You can set a reference variable to null using an
assignment statement if you don't want it to point to
anything
• Think of null as being a fixed point in memory with
nothing there
• Primitive variables cannot be assigned null. The
concept null only makes sense when talking about
reference variables
Lab17TreasureApplet
• Redo Treasure Island as an applet class
Download