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

2020/11/13

Value Display Danpost

ronald ronald

2020/11/13

#
public class Test2 extends Actor
{
    /**
     * Act - do whatever the Test2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    private Actor valueBar;
    private int value = 3600;
    private int maxValue = value;
    
    public Test2()
    {
        valueBar = new SimpleActor();
        updateValueDisplay();
        getWorld().addObject(valueBar,80,20);
    }
    
    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);
    }
}

I'm trying to understand this progress bar from danpost
but I didn’t manage to make it work
it crashes with new SimpleActor in the constructor
and also with addObject, I tried to put getWorld () next to it, it seems to work but it doesn't compile with new SimpleActor
thanks for your help danpost
MrSkyPanda MrSkyPanda

2020/11/13

#
I can't tell you what's wrong if you don't tell me.
ronald ronald

2020/11/13

#
I'm trying to understand this progress bar from danpost but I didn’t manage to make it work it crashes with new SimpleActor in the constructor and also with addObject, I tried to put getWorld () next to it, it seems to work but it doesn't compile with new SimpleActor thanks for your help danpost well if I just said it new simpleactor does not pass it is written in the code
danpost danpost

2020/11/13

#
ronald wrote...
I'm trying to understand this progress bar from danpost but I didn’t manage to make it work it crashes with new SimpleActor in the constructor and also with addObject, I tried to put getWorld () next to it, it seems to work but it doesn't compile with new SimpleActor
Do you have a SimpleActor class extending Actor?
ronald ronald

2020/11/13

#
yes i thought of changing SimpleActor or keeping it but another window appears at SimpleActor. <init> (SimpleActor.java:22) at SimpleActor. <init> (SimpleActor.java:22) at SimpleActor. <init> (SimpleActor.java:22) at SimpleActor. <init> (SimpleActor.java:22) at SimpleActor. <init> (SimpleActor.java:22) at SimpleActor. <init> (SimpleActor.java:22) at SimpleActor. <init> (SimpleActor.java:22) at SimpleActor. <init> (SimpleActor.java:22) here it is line 14 which will block
danpost danpost

2020/11/13

#
Your SimpleActor class should consist of exactly one line of code -- no more; no less;; namely:
public class SimpleActor extends greenfoot.Actor {}
ronald ronald

2020/11/13

#
nothing to do I kept SimpleActor and put greenfoot.Actor, I still have the same message above sorry
danpost danpost

2020/11/13

#
ronald wrote...
nothing to do I kept SimpleActor and put greenfoot.Actor, I still have the same message above sorry
Show code.
ronald ronald

2020/11/13

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

/**
 * Write a description of class Test2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SimpleActor extends greenfoot.Actor
{
    /**
     * Act - do whatever the Test2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    private Actor valueBar;
    private int value = 3600;
    private int maxValue = value;
    
    public SimpleActor()
    {
        valueBar = new SimpleActor();
        updateValueDisplay();
        getWorld().addObject(valueBar,80,20);
    }
    
    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);
    }
}

danpost danpost

2020/11/13

#
Move everything except lines 1, 9 and 20 into your World subclass. Then remove all but lines 9, 10 and 56.
ronald ronald

2020/11/14

#
I created a subclass of World and put everything in World1 but it is still the line valueBar = new World1 () which blocks and launches a white window I even put everything in World and it's still this line that blocks
danpost danpost

2020/11/14

#
Try this World subclass:
import greenfoot.*;

public class MyWorld extends World
{
    private Actor valueBar;
    private int value = 3600;
    private int maxValue = value;
     
    public MyWorld()
    {
        super(600, 400, 1);
        valueBar = new SimpleActor();
        updateValueDisplay();
        addObject(valueBar, 80, 20);
    }
     
    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);
    }
}
and this for Actor subclass:
public class SimpleActor extends greenfoot.Actor{}
ronald ronald

2020/11/14

#
thanks danpost
You need to login to post a reply.