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

2021/2/11

Bar Button World

ronald ronald

2021/2/11

#
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 MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    
    private Actor valueBar;
    private int value = 50;
    private int minValue = 0;
    private int maxValue = 100;
    
    static final Color TRANS = new Color(0,0,0,0);
    
    Actor btn01,btn02;
        
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        valueBar = new SimpleActor();
        updateValueDisplay();
        addObject(valueBar, 400, 100);
              
        addObject(btn01 = getNewButton("BUTTON 01"),200,100);
        addObject(btn02 = getNewButton("BUTTON 02"),200,150);
        
    }
    
    public void adjustValue(int amount)
    {
        value += amount;
        if(Greenfoot.mouseClicked(btn01))
            if(value<minValue) value = minValue;
        if(Greenfoot.mouseClicked(btn02))
            if(value>maxValue) value = maxValue;
        updateValueDisplay();
    }
    
    //public void adjustValue(int amount) 
    //{
    //    value += amount;
    //    if (value<0) value = 0;
    //    if (value>maxValue) value = maxValue;
    //    updateValueDisplay();
    //}   
      
    private void updateValueDisplay()
    {
        int wide = 100;
        int high = 12;
        
        GreenfootImage fullImg = new GreenfootImage(wide, high);
        fullImg.setColor(Color.GREEN);
        fullImg.fill();
        
        GreenfootImage colorBar = new GreenfootImage(wide, high);
        int percentage = wide*value/maxValue;
        colorBar.drawImage(fullImg, percentage-wide, 0);
        
        GreenfootImage img = new GreenfootImage(wide+4, high+4);
        img.setColor(Color.WHITE);
        img.fill();
        img.setColor(Color.BLACK);
        img.drawRect(0,0,wide+3, high+3);
        img.drawImage(colorBar, 2, 2);
        valueBar.setImage(img);
    }
    
    protected Actor getNewButton(String caption)
    {
        GreenfootImage base = new GreenfootImage(400,230);
        base.setColor(Color.BLUE);
        base.fillRect(100,100,200,30);
        
        GreenfootImage text = new GreenfootImage(caption,20,Color.BLACK, TRANS);
        base.drawImage(text,200-text.getWidth()/2,115-text.getHeight()/2);
        base.setTransparency(128);
        
        Actor button = new Actor()
        {
        };
        button.setImage(base);
        return button;
        
    }
}

I have two buttons with a progress bar when i press the first button the progress bar increases and when i press the second button the progress bar decreases, i tried to copy the method by adding mouseCkicked i don't know if i was wrong Thank you for your help
danpost danpost

2021/2/11

#
ronald wrote...
i tried to copy the method by adding mouseCkicked i don't know if i was wrong
You were wrong. The asking of mouse clicks go in an act method and then calls the commented out method. Remove lines 39 thru 47 and restore lines 49 thru 55. Add the following act method:
public void act()
{
    if (Greenfoot.mouseClicked(btn01)) adjustValue(1);
    if (Greenfoot.mouseClicked(btn02)) adjustValue(-1);
}
ronald ronald

2021/2/11

#
thanks for the method I saw in your work a part of code with bar.add (1) and bar.subtract (-1) my code works but the two buttons do the same thing, they decrease but the 1st button does not increase the progress bar in my opinion there is a calculation error
danpost danpost

2021/2/11

#
ronald wrote...
in my opinion there is a calculation error
No calculation error. Button base image is huge. Both clicks on btn02. Recopy getNewButton method from source.
ronald ronald

2021/2/11

#
do you want me to decrease the size of the buttons? if so, i will try from source
danpost danpost

2021/2/11

#
ronald wrote...
do you want me to decrease the size of the buttons? if so, i will try from source
You will need to if you want to be able to click on the first button (without registering the click on the second one. Try this:
protected Actor getNewButton(String caption)
{
    GreenfootImage base = new GreenfootImage(200, 30);
    base.setColor(Color.BLUE);
    base.fill();
     
    GreenfootImage text = new GreenfootImage(caption, 20, Color.BLACK, TRANS);
    base.drawImage(text, 100-text.getWidth()/2, 15-text.getHeight()/2);
    base.setTransparency(128);
     
    Actor button = new SimpleActor();
    button.setImage(base);
    return button;
}
ronald ronald

2021/2/11

#
cool thanks danpost it works
You need to login to post a reply.