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

2019/5/5

About Green foot Creating images

bbishal69 bbishal69

2019/5/5

#
Exercise 10.49 Add a display that shows the current volume. for exercise 10.49, you need to display the volume either in a vertical bar or a horizontal bar. This volume bar is for display volume only, and nothing should happen if you click on the bar, because it is not supposed to respond to user clicking on the bar. The lowest value is 0, and the highest value is 100. According to class GreenfootSound , its method getVolume() indicates that the volume is between 0 and 100. Alone with the bar, you also need to display the current volume level in numeric value, such as number 97 in the picture below. An example volume bar looks like this horizontal bar (you should ignore the dark background in these two pictures when you implement the volume bart in your homework.): , or this vertical bar: I have a code made so far are following: Please guys help me on this, ThaNK YOU..

public class CurrentVolume extends Actor
{
    private String message;
    private int currentVolume;
    
    public CurrentVolume() 
    {
        this(0);
    } 
    
    public CurrentVolume(int currentVolume) 
    {
        message = "VOLUME: ";
        //GreenfootImage image = new GreenfootImage("currentvol.png");
        setImage(new GreenfootImage(message.length() * 20, 30));
        //image.scale(200, 200);
        GreenfootImage image = getImage();
        
        this.currentVolume = currentVolume;
        
    }
    
    public void act() 
    {
        updateImage();
    }
    
    public void setCurrentVolume(int newCurrentVolume){
        this.currentVolume = newCurrentVolume;
        updateImage();
    }

    private void updateImage(){
        GreenfootImage image = getImage();
        image.clear();
        image.setFont(image.getFont().deriveFont(20f));
        image.drawString(message + currentVolume,14,20);
    }
}
If you guys need more code linked with it with other class and I have one. please respond it and help would be really appreciate.. Thank You Guys...
danpost danpost

2019/5/5

#
Main issues are: (1) the GreenfootImage constructor call on line 15 is missing both Color parameters; (2) the act method is causing the image to be unnecessarily recreated (cleared and redrawn) every act cycle; (3) it is probably better to create a new image (when needed) rather than clearing and redrawing it; (4) the font size is a parameter in the GreenfootImage constructor, so with (3), lines 36 and 37 would not be needed; Currently, your CurrentVolume class is really close to being like a cross between the Label and Score classes (which is not a bad thing). However, I was thinking it would be nice if you just supplied the GreenfootSound object whose volume it is to display and let the CurrentVolume object control itself. You could add a method to link a different GreenfootSound object to it. Well, it was just an idea. At any rate, at this point, you have it showing only text. You need to build a bar image also and combine them by drawing both the text and bar images on an image sized to contain both. Then set that as the image of the actor.
bbishal69 bbishal69

2019/5/5

#
Thank you for your help #danpost. Well, I am confused on how to create a bar image so would you please help me on that ?. Thank you.....
danpost danpost

2019/5/6

#
bbishal69 wrote...
I am confused on how to create a bar image so would you please help me on that ?
Please refer to my Value Display Tutorial scenario.
bbishal69 bbishal69

2019/5/7

#
Hey danpost, I could not figured out on this as you said, "You need to build a bar image also and combine them by drawing both the text and bar images on an image sized to contain both. Then set that as the image of the actor." Could you help me on how to build a bar image please? Thanks Bishal
danpost danpost

2019/5/8

#
bbishal69 wrote...
Hey danpost, I could not figured out on this as you said, "You need to build a bar image also and combine them by drawing both the text and bar images on an image sized to contain both. Then set that as the image of the actor." Could you help me on how to build a bar image please? Thanks Bishal
danpost wrote...
Please refer to my Value Display Tutorial scenario.
You need to login to post a reply.