Algorithm Steps:
Step 1: Write a html applet tag with code property set to the class name and comment it
Step 2: Import necessary packages and classes
Step 3: Define a class that extends applet and implements adjustment listener
Step 4: Declare three scroll bars red, green, and blue
Step 5: Declare a panel to hold scroll bars and three labels for them
Step 6: In the init() method, do the following:
i) Create the scroll bars, set their unit increment and add adjustment listern to them
ii) Create the panel and set its layout to grid layout of 3 rows and two columns
iii) Create the labels and add them to the panel and then add the scroll bars
iv) Add the scroll panel to the applet
Step 7: In the adjustmentValueChanged() method, repaint the applet
Step 8: In the paint() method, do the following:
i) Create a color from the values of red, green and blue scroll bars
ii) Draw a rectangle and fill it with the created color
Java Program To Demonstrate Awt Scrollbar
import java.applet.*;
import java.awt.*;
public class scroller extends Applet
{
Scrollbar colorscroll = new
Scrollbar(Scrollbar.VERTICAL,0,0,0,255);
int redvalue = 175;
Label scrollvalue = new Label
(String.valueOf(redvalue));
public void init()
{
add(new Label("Scroll to change the
color of the box."));
add(colorscroll);
colorscroll.setPageIncrement(50);
colorscroll.setValue(175);
add(new Label("value of the scrollbar ="));
add(scrollvalue);
}
public void paint (Graphics g)
{
g.setColor(new Color(redvalue,0,0));
g.fillRect(90,90,100,100);
}
public boolean handleEvent(Event evt)
{
if (evt.target instanceof Scrollbar)
{
redvalue = colorscroll.getValue();
scrollvalue.setText(String.valueOf
(redvalue));
repaint();
return true;
}
return false;
}
}
SAMPLE OUTPUT SCREEN:
|