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

2021/2/17

Background Sound

1
2
ronald ronald

2021/2/17

#
public void act() 
    {
        // Add your action code here.
        File file = new File ("01 - Moonlight.wav");

        AudioInputStream audioInputStream = null;

        try 
        {
            audioInputStream = AudioSystem.getAudioInputStream(file);		
        } 
        catch (UnsupportedAudioFileException uafe) 
        {	
            uafe.printStackTrace();
        } 
        catch (IOException ioe) 
        {		
            ioe.printStackTrace();
        }

        AudioFormat format = audioInputStream.getFormat();     
        long frames = audioInputStream.getFrameLength();
        double durationInSeconds = (frames+0.0) / format.getFrameRate();

        int i = 0;

        if(Greenfoot.mouseClicked(btn01)) 
        {
            //valueBar.setVisible(true);
            try 
            {
                Clip clip = AudioSystem.getClip();
                clip.open(AudioSystem.getAudioInputStream(file));
                clip.start();

                while (i <= 100)
                {
                    Thread.sleep((long) (durationInSeconds*1000/100));
                    valueBar.paintImmediately(0, 0, 200, 20);
                    valueBar.setValue(i);
                    i++;         
                }
                //System.out.println(durationInSeconds1);
            } 
            catch (Exception e)
            {
                e.printStackTrace();  
            }
        }

    }
I also work and train on Eclipse the progress bar stops perfectly at the end of the song on Eclipse I was wondering if it was possible to do the same on Greenfoot with a piece of Eclipse code, I know that doesn't always work Thank you for your help I block line 39 and 40 - cannot find symbol method setPaintImmediatley and method setValue
danpost danpost

2021/2/17

#
As what type is valueBar declared?
ronald ronald

2021/2/17

#
good question I will say randomly a character string valueBar is a progress bar
danpost danpost

2021/2/17

#
ronald wrote...
good question I will say randomly a character string valueBar is a progress bar
You misunderstand. Show fields at top of class.
ronald ronald

2021/2/17

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.UnsupportedAudioFileException;

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Background extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */

    Actor btn01;

    private Actor valueBar;
    private int value = 50;
    private int maxValue = 100;

    static final Color TRANS = new Color(0,0,0,0);
    
    public Background()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 600, 1); 

        valueBar = new SimpleActor();
        updateValueDisplay();

        addObject(valueBar,500,500);
        addObject(btn01 = getNewButton("BUTTON 01"),200,500);

        prepare();
    }

    public void act() 
    {
        // Add your action code here.
        File file = new File ("01 - Moonlight.wav");

        AudioInputStream audioInputStream = null;

        try 
        {
            audioInputStream = AudioSystem.getAudioInputStream(file);		
        } 
        catch (UnsupportedAudioFileException uafe) 
        {	
            uafe.printStackTrace();
        } 
        catch (IOException ioe) 
        {		
            ioe.printStackTrace();
        }

        AudioFormat format = audioInputStream.getFormat();     
        long frames = audioInputStream.getFrameLength();
        double durationInSeconds = (frames+0.0) / format.getFrameRate();

        int i = 0;

        if(Greenfoot.mouseClicked(btn01)) 
        {
            //valueBar.setVisible(true);
            try 
            {
                Clip clip = AudioSystem.getClip();
                clip.open(AudioSystem.getAudioInputStream(file));
                clip.start();

                while (i <= 100)
                {
                    Thread.sleep((long) (durationInSeconds*1000/100));
                    valueBar.paintImmediately(0, 0, 200, 20);
                    valueBar.setValue(i);
                    i++;         
                }
                //System.out.println(durationInSeconds1);
            } 
            catch (Exception e)
            {
                e.printStackTrace();  
            }
        }

    }

    public void adjustValue(int amount)
    {
        value+=amount;
        if(value<0) value = 0;
        if(value>maxValue) value = maxValue;
        updateValueDisplay();
    }

    private void updateValueDisplay()
    {
        int wide = 200;
        int high = 20;

        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(200,30);
        base.fill();
        base.setColor(Color.BLUE);
        base.fillRect(3,3,194,24);

        GreenfootImage text = new GreenfootImage(caption,24,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;
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
    }
}
I will make it easier I will display all the code
danpost danpost

2021/2/17

#
It is declared as an Actor object (see line 26). There are no methods in the Actor class called paintImmediately or setValue. You do have a method in this class called adjustValue which you can use.
ronald ronald

2021/2/17

#
I add adjusValue to valueBar line 84 and 85 it indicates cannot find symbol - variable adjustValue and if I add a variable a adjustvalue it indicates int be cannot deferenced something like this
danpost danpost

2021/2/17

#
ronald wrote...
I add adjusValue to valueBar line 84 and 85 it indicates cannot find symbol - variable adjustValue and if I add a variable a adjustvalue it indicates int be cannot deferenced something like this
Show code tried.
ronald ronald

2021/2/17

#
 int i = 0;
        int adjustValue = 0;
        
        if(Greenfoot.mouseClicked(btn01)) 
        {
            //valueBar.setVisible(true);
            try 
            {
                Clip clip = AudioSystem.getClip();
                clip.open(AudioSystem.getAudioInputStream(file));
                clip.start();

                while (i <= 100)
                {
                    Thread.sleep((long) (durationInSeconds*1000/100));
                    adjustValue.paintImmediately(0, 0, 200, 20);
                    adjustValue.setValue(i);
                    i++;         
                }
                //System.out.println(durationInSeconds1);
            } 
            catch (Exception e)
            {
                e.printStackTrace();  
            }
        }

    }
not on int adjustValue = 0 since there is the method
danpost danpost

2021/2/17

#
Replace lines 16 and 17 with:
adjustValue(i-value);
ronald ronald

2021/2/17

#
while (i <= 100)
                {
                    Thread.sleep((long) (durationInSeconds*1000/100));
                    adjustValue(i-value).paintImmediately(0, 0, 200, 20);
                    adjustValue(i-value).setValue(i);
                    i++;         
                }
paintImmediately and setValue - void be cannot deferenced
danpost danpost

2021/2/17

#
ronald wrote...
<< Code Omitted >> paintImmediately and setValue - void be cannot deferenced
No. That is not what I said to do.
danpost wrote...
Replace lines 16 and 17 with:
adjustValue(i-value);
ronald ronald

2021/2/17

#
while (i <= 100)
                {
                    Thread.sleep((long) (durationInSeconds*1000/100));
                    adjustValue(i-value);                    
                    i++;         
                }
ronald ronald

2021/2/17

#
java.io.FileNotFoundException: 01 - Moonlight.wav (Le fichier spécifié est introuvable) at java.base/java.io.FileInputStream.open0(Native Method) at java.base/java.io.FileInputStream.open(FileInputStream.java:219) at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157) at java.desktop/com.sun.media.sound.SunFileReader.getAudioInputStream(SunFileReader.java:117) at java.desktop/javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1062) at Background.act(Background.java:55) at greenfoot.core.Simulation.actWorld(Simulation.java:573) at greenfoot.core.Simulation.runOneLoop(Simulation.java:506) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) java.lang.NullPointerException at Background.act(Background.java:66) at greenfoot.core.Simulation.actWorld(Simulation.java:573) at greenfoot.core.Simulation.runOneLoop(Simulation.java:506) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183)
danpost danpost

2021/2/17

#
ronald wrote...
<< Error Trace Omitted >>
Please clear terminal, reproduce error and then post error. Also, post entire (revised) class codes.
There are more replies on the next page.
1
2