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

2021/2/12

Bar Button Actor

ronald ronald

2021/2/12

#
rehello danpost now i'm trying to create actor buttons my question is to know if you have to copy all the code to have a button with a progress bar in actor or can I do the same thing as I did with the buttons in world I bother to copy all the code for one of your scenarios what I'm interested in is to create actor buttons with a progress bar with two buttons one which increases the progress bar and the other which decreases but in actor this time thanks for your help
danpost danpost

2021/2/12

#
ronald wrote...
my question is to know if you have to copy all the code to have a button with a progress bar in actor or can I do the same thing as I did with the buttons in world I bother to copy all the code for one of your scenarios what I'm interested in is to create actor buttons with a progress bar with two buttons one which increases the progress bar and the other which decreases but in actor this time
It would be similar to a point. Where it would be different is where you can add the bar and buttons into the world -- in constructor will not work in an Actor class.
ronald ronald

2021/2/12

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bar extends Actor
{
    /**
     * Act - do whatever the Bar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    private Actor btn03,btn04;
    private int barWidth = 100;
    private int barHeight = 10;
    private Color backgroundColor = new Color(0,0,0,0);
    private Color textColor = Color.BLACK;    
    private int value = 0;
    private int minValue = 0;
    private int maxValue = 0;
    
    static final Color TRANS = new Color(0,0,0,0);
    
    public Bar()
    {
        GreenfootImage img = new GreenfootImage(200,30);
        img.fill();
        img.setColor(Color.GREEN);
        img.fillRect(3,3,194,24);
        
        GreenfootImage text = new GreenfootImage("BUTTON",20,Color.WHITE,TRANS);
        img.drawImage(text,100-text.getWidth()/2,15-text.getHeight()/2);
        img.setTransparency(128);
        
        btn03 = new SimpleActor();
        btn03.setImage(img);
        
        btn04 = new SimpleActor();
        btn04.setImage(img);
    }
    
    protected void addedToWorld(World world)
    {
        world.addObject(btn03,200,215);
        world.addObject(btn04,200,265);
    }
    
    //public void act() 
    //{
    //    // Add your action code here.
    //    if (GreenfootImage.mouseClicked(btn03)) add(1);
    //    if (GreenfootImage.mouseClicked(btn04)) subtract(-1);
    //}
    
    private void newImage()
    {
        int barValue = (int)(barWidth*(value-minValue)/(maxValue-minValue));
        
        GreenfootImage barImg = new GreenfootImage(barWidth+4,barHeight+4);
        barImg.setColor(backgroundColor);
        barImg.fill();
        barImg.setColor(textColor);
        barImg.drawRect(0,0,barImg.getWidth()-1,barImg.getHeight()-1);
        
    }
    
    public void add(int amount)
    {
        value += amount;
        checkValue();
        newImage();
    }
    
    public void subtract(int amount)
    {
        value -= amount;
        checkValue();
        newImage();
    }
    
    private void checkValue()
    {
        if (value<minValue) value = minValue;
        if (value>maxValue) value = maxValue;
    }
}
I understand indeed, the actor constructor does not work with the world constructor I started this code, I don't know if you can help me I added btn03 and btn04, is there a way to add button number 03 and 04 to the button name, the code is not finished thank you
danpost danpost

2021/2/12

#
ronald wrote...
is there a way to add button number 03 and 04 to the button name
Why not have a method similar to getNewButton here, as well?
ronald ronald

2021/2/12

#
from the forum, add getWorld () to addObject () I think we have to put in the actor constructor a second window opens and tells me that there is nullpointerexception
danpost danpost

2021/2/12

#
ronald wrote...
from the forum, add getWorld () to addObject () I think we have to put in the actor constructor a second window opens and tells me that there is nullpointerexception
danpost wrote...
Where it would be different is where you can add the bar and buttons into the world -- in constructor will not work in an Actor class.
Lines 45 thru 49 is correct for adding the buttons into the world.
ronald ronald

2021/2/12

#
Great I moved getNewButton, I added btn03 = getNewButton (,,,) to addedToWorld and it works
You need to login to post a reply.