import java.awt.*; public class Slider extends Widget { public String label = "SLIDER"; private double value = 0; //---------------- OBJECT CONSTRUCTOR public Slider(int x, int y, int width, int height) { super(x, y, width, height); } //---------------- SET AND GET VALUE public void setValue(double value) { this.value = Math.max(0, Math.min(1, value)); } public double getValue() { return value; } //---------------- HANDLING DRAG EVENT public boolean drag(int x, int y) { if (super.drag(x, y)) { setValue((double)(x - this.x) / width); return true; } return false; } //---------------- RENDERING THE SLIDER public void render(Graphics g) { g.setColor(bgColor); // RENDER FRAME AND SLIDE g.fill3DRect(x, y, width, height, false); g.fill3DRect(x+1,y+1, (int)((width-2) * value), height-2,true); g.setColor(fgColor); // RENDER TEXT NAME g.drawString(label, x + 2, y + 12); } }