import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MyWorld here. * * @author (your name) * @version (a version number or a date) */ public class SliderBar extends World { protected HoSlider hoSlider; protected VeSlider veSlider; public SliderBar() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(900, 600, 1); hoSlider = new HoSlider(0, 1000); veSlider = new VeSlider(1000, 0); addSliderBars(); } public final void addSliderBars() { addObject(hoSlider, getWidth()/2, getHeight() - 30); addObject(veSlider, getWidth() - 30, getHeight()/2); } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Slider here. * * @author (your name) * @version (a version number or a date) */ public class Slider extends Menu { public static final int MIN_X = -82; public static final int MAX_X = 81; public static final double RANGE_X = MAX_X - MIN_X; public static final int MIN_Y = -82; public static final int MAX_Y = 81; public static final double RANGE_Y = MAX_Y - MIN_Y; public int getMinX() { return getX() + MIN_X; } public int getMaxX() { return getX() + MAX_X; } public int getMinY() { return getY() + MIN_Y; } public int getMaxY() { return getY() + MAX_Y; } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class HoSlider here. * * @author (your name) * @version (a version number or a date) */ public class HoSlider extends Slider { private SliderHand hand; private boolean enabled; private int min; private int max; public HoSlider() { this(0, 100); } public HoSlider(int min, int max) { this.min = min; this.max = max; //range = max - min; enabled = true; //val = (int) ((double) range/2); setImage(new GreenfootImage("slider-base.png")); } public void addedToWorld(World world) { hand = new SliderHand(); getWorld().addObject(hand, getX(), getY()); } class SliderHand extends Menu { public SliderHand() { setImage("slider-hand.png"); } public void act() { if (enabled && Greenfoot.mouseDragged(this)) { int oldX = getX(); MouseInfo mouse = Greenfoot.getMouseInfo(); int x = mouse.getX(); if (x < getMinX()) x = getMinX(); if (x > getMaxX()) x = getMaxX(); if (x != oldX) { setLocation(x, getY()); //setValueFromX(x); } } } } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class VeSlider here. * * @author (your name) * @version (a version number or a date) */ public class VeSlider extends Slider { private SliderHand hand; private boolean enabled; private int min; private int max; public VeSlider() { this(0, 100); } public VeSlider(int min, int max) { this.min = min; this.max = max; enabled = true; setImage(new GreenfootImage("slider-base.png")); setRotation(270); } public void addesToWorld(World world) { hand = new SliderHand(); getWorld().addObject(hand, getX() + 4, getY()); } class SliderHand extends Menu { public SliderHand() { setImage("slider-hand.png"); setRotation(90); } public void act() { if (enabled && Greenfoot.mouseDragged(this)) { int oldY = getY(); MouseInfo mouse = Greenfoot.getMouseInfo(); int y = mouse.getY(); if (y < getMinY()) y = getMinY(); if (y > getMaxY()) y = getMaxY(); if (y != oldY) { setLocation(getX(), y); } } } } }