Previous Topic Tutorial Home Page Next Topic
Section 3: Use AWT Controls


This class extends Panel. It has four buttons - pause, restart, fast forward, rewind - and a scrollbar. The scrollbar is used to control the playback rate at a finer level.
class Controls extends Panel {

Place the scrollbar at the center, then two buttons on either side.
Controls(VCRModel movie) {
_movie = movie;
setLayout(new BorderLayout());

_rateBar = new Scrollbar(Scrollbar.HORIZONTAL, 10, 1, -40, 40);
add("Center", _rateBar);

Pause and restart buttons on the west side.
Panel pWest = new Panel();
pWest.setLayout(new GridLayout(1, 2));
pWest.add(new Button("| |"));
pWest.add(new Button(">"));
add("West", pWest);

Fast forward and rewind buttons on the east side.
Panel pEast = new Panel();
pEast.setLayout(new GridLayout(1, 2));
pEast.add(new Button(">>"));
pEast.add(new Button("<<"));
add("East", pEast);
}

The event handler when any buttons are pressed.
public boolean handleEvent(Event ev) {
switch (ev.id) {
case Event.SCROLL_LINE_UP:
case Event.SCROLL_LINE_DOWN:
case Event.SCROLL_PAGE_UP:
case Event.SCROLL_PAGE_DOWN:
case Event.SCROLL_ABSOLUTE:

Scroll position changed: Clear the pause state, set the rate to the current scroll position.
_bPaused = false;
_movie.setRate(_rateBar.getValue());
return true;

case Event.ACTION_EVENT:
if (ev.arg == "| |") {

Pause: Toggle the pause state. This means that pressing the pause button the second time resumes the normal playback rate. The normal playback rate is 10, the pause rate is 0. Need to reflect the current rate in the scrollbar.
int rate;
if (_bPaused) {
_bPaused = false;
rate = 10;
} else {
_bPaused = true;
rate = 0;
}
_movie.setRate(rate);
_rateBar.setValue(rate);
return true;
} else if (ev.arg == ">") {

Restart: Clear the pause state, restart the movie at normal rate.
_bPaused = false;
_movie.restart();
_rateBar.setValue(10);
return true;
} else if (ev.arg == ">> {// Fast forward

FastForward: Clear the pause state, set rate to 30 and update the scrollbar position.
_bPaused = false;
_movie.setRate(30);
_rateBar.setValue(30);
return true;
} else if (ev.arg == "<<") {

Rewind: Clear the pause state, set rate to -30 and update the scrollbar position.
_bPaused = false;
_movie.setRate(-30);
_rateBar.setValue(-30);
return true;
}
}
return false;
}

boolean _bPaused = false;
Scrollbar _rateBar;
VCRModel _movie;
}


© 1998 Microsoft Corporation. All rights reserved. Terms of Use.

Previous Topic Tutorial Home Page Next Topic