Transcript PowerPoint
Class 10 - Applets
Using drawLineList
Color
Instance variables
GUI components
Input in applets
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -1
Reading assignment
Savitch Ch. 7 & 13, or
Horstmann Ch. 4 & 10.
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -2
Example: drawing the Hilbert curve
We can use drawLineList to draw the
Hilbert curve. No change in the HC function
is needed (well, almost none....).
To keep our files small, we will place the HC
function in a separate class called
HilbertCurve. Thus, to create the Hilbert
curve line list, we will call
HilbertCurve.HC(order, segment-length).
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -3
Example: drawing the Hilbert curve
(cont.)
The HilbertCurve class looks like our
previous version, but it doesn’t have the
main method:
class HilbertCurve {
LineList HC (int order, int sl) {
....
}
... definitions of translate and other auxiliary
methods ...
}
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -4
Example: drawing the Hilbert curve
(cont.)
The Hilbert curve applet, HCApplet, has
only a paint method:
public class HCApplet extends Applet {
public void paint (Graphics g) {
drawLineList(HilbertCurve.HC(4, 10), g);
}
static void drawLineList (LineList L, Graphics g) {
... as shown in Class 17 ...
}
}
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -5
Scaling the curve
The way we have drawn Hilbert curves,
each curve grows larger and larger as the
order increases.
Would instead like to scale the picture to fit
into the window
Scaling is a simple matter of multiplying
each point by a scaling factor.
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -6
Scaling the curve (cont.)
static LineList scale (LineList L, double sc) {
if (L.empty()) return LL.nil;
else {
Line ln = L.hd();
ln = new Line(scale(sc, ln.x0()),
scale(sc, ln.y0()),
scale(sc, ln.x1()),
scale(sc, ln.y1()));
return LL.cons(ln, scale(L.tl(), sc));
}
}
static int scale (double sc, int x) {
return (int)(Math.round(x*sc));
}
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -7
Scaling the curve (cont.)
Add a call to scale in the paint method:
public void paint (Graphics g) {
LineList hc =
scale(HilbertCurve.HC(4, 10), 0.5);
drawLineList(hc, g);
}
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -8
Color
The Color class (in the java.awt package) has
values that denote different colors.
Symbolic constants Color.black, Color.red,
etc., are predefined.
Arbitrary colors can be created by giving RGB
values in the range 0-255 when constructing a
Color value:
Color col1 = new Color(100,220,155);
Color darkred = new Color(100,50,50);
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -9
Drawing in color
A Graphics objects has a current color, in
which all lines and fills are drawn.
Change the current color of Graphics object
g by the call: g.setColor(Color object)
E.g. draw a red circle:
g.setColor(Color.red);
g.drawOval(10,10,50,50);
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -10
Example: drawing the Hilbert curve
in color
Suppose you want to draw the Hilbert curve
with each segment in a color that depends
upon the location of the segment:
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -11
Example: drawing the Hilbert curve
in color (cont.)
static void drawLineList (LineList L, Graphics g) {
if (!L.empty()) {
Line ln = L.hd();
g.setColor(makeColor(ln.x0(), ln.y0()));
g.drawLine(ln.x0(), ln.y0(), ln.x1(), ln.y1());
drawLineList(L.tl(), g);
}
}
static Color makeColor (int x, int y) {
return new Color(255,Math.min(255,x),
Math.min(255,y));
}
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -12
Instance variables
Variables declared inside a method (the kind
we’ve used so far) are either parameters or
local variables.
Variables declared outside of any method
are either class variables or instance
variables. (They are class variables if they
are declared with the static keyword; we
will consider only instance variables for
now.)
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -13
Instance variables (cont.)
Public class MyApplet extends Applet {
int x = initial-value;
double y;
public void init () {
y = initial value;
}
public void paint (Graphics g) {
… x … y …
}
}
29/2/00
SEM107 © Kamin & Reddy
Declare here may initialize
here, or here
Refer to in
this or any
other instance
method
Class 10 - Applets -14
Instance variables (cont.)
Class and instance variables can be referred
to by any method in the class. (Exception:
instance variables cannot be used by class
methods.)
The value of the variable is the last value
assigned by the last method that made an
assignment to that variable.
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -15
Instance variable example
In our HCApplet, the curve is calculated
each time the paint method is called. To
avoid this, declare an instance variable and
assign to it in the init method; then refer
to it in the paint method.
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -16
Instance variable example (cont.)
public class HCApplet extends Applet {
LineList theCurve;
public void init () {
theCurve = LL.scale(HilbertCurve.HC(5, 10),
0.8);
}
public void paint (Graphics g) {
drawLineList(theCurve, g);
}
}
static void drawLineList (LineList L, Graphics g) {
. . .
}
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -17
GUI Components
Aside from drawing, one can place Graphical
User Interface (GUI) components, such as
buttons and text boxes (for input).
java.awt contains many classes
corresponding to components (including one
called Component). See:
http://java.sun.com/products/jdk/1.1/docs/api/packages.html
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -18
Placing GUI components in an
applet
Normally, declare an instance variable of
the component type (Button, Checkbox,
TextField, etc.)
In init
Initialize
variable.
Use add method to place it in applet.
paint
does not have to draw components they are automatically drawn
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -19
Components
Some sample components:
Label:
Place text in applet (alternative to
drawString):
new Label(“A label”);
Button:
Place button, possibly with label:
new Button();
new Button(“Press me”);
TextField:
Create field of given width to input
text:
new TextField(10);
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -20
Example: A component-filled applet
public class BusyApplet extends Applet {
Label L = new Label(“A label”);
Button b = new Button();
Button bl = new Button(“Press me”);
TextField t = new TextField(10);
CheckBox cb = new Checkbox();
public void init () {
add(L); add(b); add(bl);
add(t); add(cb);
}
}
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -21
Example: A component-filled applet
(cont.)
An applet can have components and also
graphics.
Add paint method to previous applet:
public void paint (Graphics g) {
g.drawLine(0, 100, 150, 50);
g.setColor(Color.red);
g.fillRect(50, 50, 30, 60);
}
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -22
Input in applets
Users enter text in TextField components
Reading the value in a TextField involves
four changes to what we’ve seen:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
1
public class appletname extends Applet
implements ActionListener {
2
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -23
Input in applets (cont.)
TextField t = new TextField(4);
public void init () {
...
add(t);
t.addActionListener(this);
}
3
public void actionPerformed (ActionEvent e) {
... t.getText() ...
repaint();
4
}
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -24
Input in applets (cont.)
1
2
3
“import java.awt.event.*” must appear
verbatim
“implements ActionListener” must appear
verbatim
TextField variable, say t, is declared as
instance variable
In init, in addition to calling add, call
t.addActionListener(this);
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -25
Input in applets (cont.)
4
Define actionPerformed method.
Called
when user types Enter key in the text
field.
Use getText method of TextField class to get
the String contained in t.
If there is a paint method, it needs to be called
again. Call repaint().
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -26
Interactive Hilbert applet
This version of the Hilbert applet allows the
user to input the order of the curve to be
drawn.
(Needed enhancements:
Scale
to window
Separate text field from drawing.
)
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -27
Interactive Hilbert applet source
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class HCApplet5 extends Applet
implements ActionListener {
TextField order = new TextField(2);
LineList theCurve;
public void init () {
theCurve = LLops.scale(
HilbertCurve.HC(5, 10), 0.8);
add(new Label("Order: "));
add(order);
order.addActionListener(this);
}
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -28
Interactive Hilbert applet source
public void paint (Graphics g) {
drawLineList(theCurve, g);
}
static void drawLineList (LineList L, Graphics g) {
if (!LL.empty(L)) {
Line ln = LL.hd(L);
g.drawLine(ln.x0(), ln.y0(), ln.x1(), ln.y1());
drawLineList(LL.tl(L), g);
}
}
public void actionPerformed (ActionEvent e) {
int ord = Integer.parseInt(order.getText());
theCurve = LLops.scale(HilbertCurve.HC(ord, 10), 0.8);
repaint();
}
}
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -29
Interactive Hilbert screen dump
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -30
The event model
Code above is one instance of the event
model.
With the event model, can respond to button
clicks, mouse movements, etc.
Can also use multiple text fields and
distinguish which one was modified.
More next week...
29/2/00
SEM107 © Kamin & Reddy
Class 10 - Applets -31