This site requires JavaScript, please enable it in your browser!
Greenfoot back
ronald
ronald wrote ...

2021/5/24

SLIDER

ronald ronald

2021/5/24

#
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);                    
                }
            }
        }
        
    }
}
Hello I work on the Mikael of Greenfoot slider If I understand the Slider Hand well should appear on Veslider But it is not there when that of Hoslider, there is. Maybe it's me to add it with setimage ()? On line 50 of Veslider I put Gety () instead of getx () Mikael I do not know if it's good Thank you for your help
ronald ronald

2021/5/25

#
Great, I found my mistake In Veslider, I put Addestoworld instead addedtoworld bizarre it did not indicate error during the run
danpost danpost

2021/5/25

#
ronald wrote...
bizarre it did not indicate error during the run
:Why would it? You declared a method with a valid name (by naming convention). How is the compiler to know you wanted to override a method in the class extended from?
ronald ronald

2021/5/25

#
Bah uh, I thought it indicated a mistake during a spelling fault as usual, what otherwise I do not understand your question
danpost danpost

2021/5/25

#
ronald wrote...
Bah uh, I thought it indicated a mistake during a spelling fault as usual, what otherwise I do not understand your question
My point was that you are allowed to declare any (new) method with any valid name. The compiler would see "addesToWorld(World)" as a new method and would, by necessity, not regard it as a mistake.
ronald ronald

2021/5/25

#
And yes, I tried to change the method several times, it does not indicate an error Yet Addedtoworld is part of the Greenfoot API I know I can put anything as a method and there, I am allowed I have to test it elsewhere on another code
danpost danpost

2021/5/26

#
ronald wrote...
Addedtoworld is part of the Greenfoot API
Yes, addedToWorld(World) is part of the Actor class API. To use (aka override) the method, you must duplicate the name precisely and use the same number and type parameters in the same order. Anything else would be a different method altogether.
You need to login to post a reply.