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

2021/2/19

Button Sound Array

1
2
3
4
ronald ronald

2021/2/19

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

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    
    static final Color TRANS = new Color(0,0,0,0);
    
    String[] numButton = {"BUTTON 01","BUTTON 02","BUTTON 03","BUTTON 04","BUTTON 05"};
    String[] numSounds = {"01 - Moonlight.wav","02 - Dangerous Woman.wav","03 - Be Alright.wav",
                          "04 - Into You.wav","05 - Side to Side.wav"};
       
    public Background()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 600, 1); 
    
        //addObject(btn01 = getNewButton("BUTTON 01"),200,100);
        //addObject(btn02 = getNewButton("BUTTON 02"),200,150);
        //addObject(btn03 = getNewButton("BUTTON 03"),200,200);
        
    }
    
    public void act()
    {
        createButton();    
    }
    
    private void createButton()
    {
        for(int i = 0; i<numButton.length; i++)
            for(int j = 0; j<numSounds.length; j++)
            {
            addObject(getNewButton(numButton[i]),200,i*50+100);
            addObject((numSounds[j]),400,j*50+100);
            }
    }
        
    public 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.WHITE, TRANS);
     base.drawImage(text, 100-text.getWidth()/2, 15-text.getHeight()/2);
     base.setTransparency(128);
     
     Actor button = new SimpleActor();        
     button.setImage(base);
     return button;       
    } 
}
this is a draft code I managed to create an array of buttons I am trying to create an array of songs, I cannot connect the buttons and sounds Thank you for your help
danpost danpost

2021/2/19

#
ronald wrote...
<< Code Omitted >> I managed to create an array of buttons I am trying to create an array of songs, I cannot connect the buttons and sounds
You will have an abundance of buttons in no time with that code (overlaid on top of one another). Create buttons in the constructor -- not act. The act method should test for clicks. The connection between the two arrays is already set by index value (position in arrays). No double for loops are required here.
ronald ronald

2021/2/19

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

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    
    static final Color TRANS = new Color(0,0,0,0);
    
    String[] numButton = {"BUTTON 01","BUTTON 02","BUTTON 03","BUTTON 04","BUTTON 05"};
    String[] numSounds = {"01 - Moonlight.wav","02 - Dangerous Woman.wav","03 - Be Alright.wav",
                          "04 - Into You.wav","05 - Side to Side.wav"};
       
    public Background()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 600, 1);
        
        GreenfootImage bg = getBackground();
        
        for(int i = 0; i<numButton.length; i++)
            {
            addObject(getNewButton(numButton[i]),200,i*50+100);
            GreenfootImage img = new GreenfootImage(numSounds[i],30,Color.BLUE,TRANS);
            bg.drawImage(img,350,85+i*50);
            }
    }
    
    public void act()
    {      
        if(Greenfoot.mouseClicked(numButton[i]))
        {
            Greenfoot.playSound(numSounds[i]);
        }          
    }
           
    public 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.WHITE, TRANS);
     base.drawImage(text, 100-text.getWidth()/2, 15-text.getHeight()/2);
     base.setTransparency(128);
     
     Actor button = new SimpleActor();        
     button.setImage(base);
     return button;       
    } 
}
okay I improved the code a little i dont know if i can use this piece of code as its an array of buttons and songs with mouseClicked
danpost danpost

2021/2/19

#
Change line 19 into an array of Actor objects. Initialize it to size of filename array. Populate the array with the buttons as you create them. You will be using:
arrayName[i]  = getNewButton("BUTTON 0"+(i+1));
Then use a for loop in act to check for clicks on those actors.
ronald ronald

2021/2/19

#
Actor[] numButton = new Actor[4];
    numButton[0] = "BUTTON 01";
    numButton[1] = "BUTTON 02";
    numButton[2] = "BUTTON 03";
    numButton[3] = "BUTTON 04";
    numButton[4] = "BUTTON 05";
I changed the line 19 to this it says ']' expected as an error
ronald ronald

2021/2/19

#
numButton[i]  = getNewButton("BUTTON 0"+(i+1));
I modified a little where do I put this code?
danpost danpost

2021/2/19

#
ronald wrote...
<< Code Omitted >> where do I put this code?
String[] songs =
{
    "01 - Moonlight.wav",
    "02 - Dangerous Woman.wav",
    "03 - Be Alright.wav",
    "04 - Into You.wav",
    "05 - Side to Side.wav"
};
Actor[] buttons = new Actor[songs.length];

public Background()
{
    super(900, 600, 1);
    for (int i=0; i<buttons.length; i++)
    {
        button[i] = getNewButton("BUTTON 0"+(i+1));
        addObject(button[i], 200, 100+i*50);
    }
}
ronald ronald

2021/2/19

#
public void act()
    {      
        for(int i = 0; i<5; i++)
        {
            if(Greenfoot.mouseClicked(buttons[i]))
            {
                Greenfoot.playSound(songs[i]);
            }
        }      
    }
thanks danpost I added for loop to act (), it works I will now try to add the progress bars thanks again
ronald ronald

2021/2/20

#
File file = new File ("sounds/songs[i]");
Hello I would like more information about the index counter about the last time and by the way i would also like to know about a piece of code not sure I can do this with File Thank you for your help
danpost danpost

2021/2/20

#
ronald wrote...
not sure I can do this with File
File class not required by you. Use GreenfootSound class.
I would like more information about the index counter
You will need a GreenfootSound field -- not an int field. It is required for the currently playing song so you can stop it before starting a different one.
ronald ronald

2021/2/20

#
I use the GreenfootSound class instead of the File class I don't see how to do it I find that the greenfoot API is quite limited compared to the java API
danpost danpost

2021/2/21

#
ronald wrote...
I use the GreenfootSound class instead of the File class I don't see how to do i
// global
private GreenfootSound currentSong;

// on button click
if (currentSong != null) currentSong.stop();
currentSong = new GreenfootSound(songs[i]);
currentSong.play();
ronald ronald

2021/2/21

#
ok thanks for this piece of code I will try to place it, I have the impression that I have to modify everything in my code I'll keep you informed
ronald ronald

2021/2/21

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

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    
    
    static final Color TRANS = new Color(0,0,0,0);
     
    String[] songs = {
                     "01 - 60s rock beat.wav",
                     "02 - 60s rock beat.wav",
                     "03 - 60s rock beat.wav",
                     "04 - 60s rock beat.wav",
                     "05 - 60s rock beat.wav",
                     };
    Actor[] buttons = new Actor[songs.length];
    private GreenfootSound currentSong;
    
    private Actor valueBar;
    private int value = 0;
    private int maxValue = 100;
        
    public Background()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 600, 1);
         
        GreenfootImage bg = getBackground();
         
        for(int i = 0; i<buttons.length; i++)
            {
            buttons[i] = getNewButton("BUTTON 0"+(i+1));
            addObject(buttons[i],500,i*50+100);
            
            GreenfootImage img = new GreenfootImage(songs[i],30,Color.BLUE,TRANS);
            bg.drawImage(img,50,85+i*50);
            
            valueBar = new SimpleActor();
            updateValueDisplay();           
            addObject(valueBar,750,i*50+100);
            }
    }
     
    public void act()
    {      
        for(int i = 0; i<buttons.length; i++)
        {
        if(Greenfoot.mouseClicked(buttons[i]))
            {
            if(currentSong!=null) currentSong.stop();
            currentSong = new GreenfootSound(songs[i]);
            currentSong.play();
            }
        }
    }
    
    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);
    }    
            
    public 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.WHITE, TRANS);
     base.drawImage(text, 100-text.getWidth()/2, 15-text.getHeight()/2);
     base.setTransparency(128);
      
     Actor button = new SimpleActor();        
     button.setImage(base);
     return button;       
    } 
}

it seems to work I added the progress bar but I tell myself that without java.io.File and others I will not be able to operate the progress bars
danpost danpost

2021/2/21

#
ronald wrote...
I added the progress bar but I tell myself that without java.io.File and others I will not be able to operate the progress bars
You can add an int array for total frames per song (loaded with proper values, of course).
There are more replies on the next page.
1
2
3
4