Scrollers

This example uses listeners and scrollers. The area of a rectangle is calculated from the length and width. Scrollers are used to set these parameters.

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class AreaRect extends Applet
implements AdjustmentListener {

private Scrollbar widthScroller, lengthScroller;
private int width = 0;
private int length = 0;
public void init() {

Label widthLabel, lengthLabel;
lengthLabel = new Label("Length");
add(lengthLabel);
lengthScroller = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 100);
add(lengthScroller);
lengthScroller.addAdjustmentListener(this);
widthLabel = new Label("Width");
add(widthLabel);
widthScroller = new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 100);
add(widthScroller);
widthScroller.addAdjustmentListener(this);

}
public void paint(Graphics g) {

g.drawString("Width of rectangle is "+width, 100, 100);
g.drawString("Length of rectangle is "+length, 100, 120);
g.drawString("Area is " +length*width, 100, 150);

}
public void adjustmentValueChanged(AdjustmentEvent e) {

width = widthScroller.getValue();
length = lengthScroller.getValue();
repaint();

}

}

This is the interface produced:

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License