|
Scrollbar Class
import java.applet.Applet;
import java.awt.*;
public class GraySlider extends Applet {
private Scrollbar slider;
private Canvas canvas;
public void init() {
// create the horizontal scrollbar and the canvas
slider = new Scrollbar(Scrollbar.VERTICAL, 0, 51, 0, 255);
// start with black canvas
canvas = new Canvas();
canvas.setBackground(new Color(0,0,0));
// choose border layout and add components
setLayout(new BorderLayout());
add("West", slider);
add("Center", canvas);
}
public boolean handleEvent(Event evt) {
if (evt.target == slider) { // slider moved?
int gl = slider.getValue(); // get graylevel
canvas.setBackground(new Color(gl,gl,gl)); // change background color
}
return true;
}
}
The way we have created the scrollbar needs some explanation. A vertical scrollbar looks as follows:
Scrollbar are:
orientation, which specifies whether the scrollbar is vertical
or horizontal;
minimum, which is the smallest in the range of possible values;
maximum, which is the largest in the range of possible values;
value, which is the currently selected value;
sVisible, which is the size of the tab;
lineIncrement , which is
the increment or decrement of the current value when you click
on the up- or down-arrow of the scrollbar;
pageIncrement , which is
the increment or decrement of the current value when you click
in the paging area of the scrollbar;
slider = new Scrollbar(Scrollbar.VERTICAL, 0, 51, 0, 255);
The current value of the scrollbar is used to define a gray color for the drawing area: the red, green and blue components in the RGB-color scheme are given the the current value of the scrollbar between 0 and 255 (8-bit color specification), where 0 means black, and 255 means white.
The current value of the scrollbar is obtained via
the getValue() method. Of course the opposite can be done as well:
the setValue(int value) method sets
the value of the scrollbar to the specified one. The following table summarizes
all constructors and main methods for scrollbars.
| Constructors | Meaning |
Scrollbar()
|
creates a vertical scrollbar |
Scrollbar(int orientation)
|
creates a scrollbar with specified orientation; available orientations
are Scrollbar.HORIZONTAL (0) and Scrollbar.VERTICAL
(1)
|
Scrollbar(int orientation, int value,
int sVisible, int minimum, int maximum)
|
creates a scrollbar with the specified orientation, initial value, tab size, and minimum and maximum values |
| Methods | Meaning |
getLineIncrement()
|
returns the line increment (default: 1) |
getMaximum()
|
returns the maximum value |
getMinimum()
|
returns the minimum value |
getOrientation()
|
returns the orientation (default: 1, vertical) |
getPageIncrement()
|
returns the page increment (default: 10) |
getValue()
|
returns the current value |
getVisible()
|
returns the size of the tab |
setLineIncrement(int lineIncrement)
|
changes the line increment |
setMaximum(int maximum)
|
changes the maximum value |
setMinimum(int minimum)
|
changes the minimum value |
setOrientation(int orientation)
|
sets the orientation |
setPageIncrement(int lineIncrement)
|
changes the page increment |
setValue(int value)
|
changes the current value |
setValues(int value, int sVisible,
int minimum, int maximum)
|
changes the values (value,
sVisible, minimum, maximum)
that define the scrollbar
|
setVisible(int sVisible)
|
changes the size of the tab |