import java.applet.*; import java.awt.*; public class AnAppletWithParms extends Applet { public void init() { String parmString = getParameter("number"); System.err.println("The single parameter: " + parmString); n = Integer.parseInt(parmString); setBackground(Color.blue); } public void paint(Graphics g) { System.out.println("In paint: n = " + n); n++; } int n; }
String
values to int
paint
method is invoked
String
objects passed to main
public static void main(String [] args) {...}
main
method, an alternative method for passing parameters to the applet is employed
<param>
tag using a name
/value
representation
<param name="number" value="1">
number
and its value is 1
param
tag appears betwen the <applet> and </applet> tags
<applet code="AppletWithParms.class" width=50 height=50> <param name="number" value="1"> </applet>
getParameter
method
String
)
String
, a conversion may be required.
number
is passed to the applet with a value of 1
.
AnAppletWithParms
Class
init
Method
init
method and its value is converted using the parseInt
method of the Integer
wrapper
class, and then stored in the instance variable n
.
parseInt
is a static
method-- it has no receiver (and requires none, since the only thing
it needs is the String
is is converting -- and that is passed as a parameter.
parseInt
being static
(and thus not having a receiver) is that the method is
invoked using the name of the class (in this case Integer
) rather than a receiver object.
Integer
class is called a wrapper
is because it contains (or wraps itself around)
several methods that provide support for working for integers (parseInt
, for exmaple).
init
paint
Method
paint
prints out a diagnostic message with the value of the instance variable n
and then increments
n
.
paint
will print out increasing values of n
.
Applet
class
getParameter
method
getParameterInfo
method do?Integer
wrapper class
parseInt
method
Place the following three applets on a single page:
AppletSubClass
example and pass in three integers corresponding to an RGB value and use the appropriate Color
constructor
to set the background color.
AnAppletWithParms
so that it accepts three parameters: operand1
, operand2
, and
operator
. If operator
is a +
, the sum of the two numbers is sent to System.out
,
similarly for -
, *
, and /
Color
class-- or use your own color names).
Color
constructor that accepts the name of a color (as a String
). Instead, you must have
logic to test the parameter (e.g., a series of conditionals) and based on the value of the parameter, you would create the
appropriate color using the RGB constructor for String
.
String
objects for equality, you must use the equals
method (the ==
operator
when used on reference variables for objects merely tests if they reference (i.e., point to) the same object. Thus:
String x = "Hello", y = "Hello", z = x;would result in:
x == y | false | not same object two different String objects containing the value Hello
|
x.equals(y) | true | the values are equal (by whatever semantics were used in the equals method for the String class)
|
x == z | true | z was initialized with a reference to the same object x is referecning |
x.equals(z) | true | the values are equal (by whatever semantics were used in the equals method for the String class)
|