ronald wrote...
I have no code of the progress barint lengthCurrentSong;
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(); } if(lengthCurrentSong==0) lengthCurrentSong++; } }
String[] songs = { ... }; // list of song file names int[] songFrames = { ... }; // list of total frames per song Actor[] buttons; // list of song buttons GreenfootSound currentSong; // song last chosen to play int songIndex; // index of last song chosen Actor bar; // the progress bar int barValue; // current length of colored portion of progress bar in pixels int songProgress; // number of frames the song has been playing for
public void act() { for (int i=0; i<buttons.length; i++) { if (Greenfoot.mouseClicked(buttons[i])) { songIndex = i; songProgress = 0; if (currentSong != null) currentSong.stop(); currentSong = new GreenfootSound(songs[i]); currentSong.play(); break; } } if (currentSong != null && currentSong.isPlaying() && songProgress < songFrames[songIndex]) songProgress++; int newValue = barLength*songProgress/songFramexs[songIndex]; if (newValue != barValue) updateBarValue(newValue); }
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 = { "60s rock beat.wav", "Blues style piano.wav", "Electronic.wav", "Jingle bells.mp3", "Mystic melody synth.wav", "Nostalgic piano.mp3", "Psycho rhodes.wav", "Tech.mp3" }; Actor[] buttons = new Actor[songs.length]; int[] songFrames = {960,1020,1500,900,900,4800,1200,1920}; int songIndex; Actor Bar; int barValue; int songProgress; int barLength; 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])) { songIndex=i; songProgress = 0; if(currentSong!=null) currentSong.stop(); currentSong = new GreenfootSound(songs[i]); currentSong.play(); break; } } if(currentSong!= null && currentSong.isPlaying() && songProgress<songFrames[songIndex]) songProgress++; int newValue = barLength*songProgress/songFrames[songIndex]; if (newValue!=barValue) updateBarValue(newValue); } 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; } }
import greenfoot.*; public class Background extends World { static final Color TRANS = new Color(0,0,0,0); String[] songs = { "60s rock beat.wav", "Blues style piano.wav", "Electronic.wav", "Jingle bells.mp3", "Mystic melody synth.wav", "Nostalgic piano.mp3", "Psycho rhodes.wav", "Tech.mp3" }; int songIndex; private GreenfootSound currentSong; Actor[] buttons = new Actor[songs.length]; int[] songFrames = { 960, 1020, 1500, 900, 900, 4800, 1200, 1920 }; int songProgress; private Actor valueBar; private int value = 0; private int maxValue = 100; public Background() { 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, 100+i*50); GreenfootImage img = new GreenfootImage(songs[i], 30, Color.BLUE, TRANS); bg.drawImage(img, 50, 85+i*50); } valueBar = new SimpleActor(); updateValueDisplay(); addObject(valueBar, 750, 100); } public void act() { for (int i=0; i<buttons.length; i++) { if (Greenfoot.mouseClicked(buttons[i])) { songIndex = i; songProgress = 0; if (currentSong != null) currentSong.stop(); currentSong = new GreenfootSound(songs[i]); currentSong.play(); valueBar.setLocation(750, 100+i*50) break; } } if (currentSong != null && currentSong.isPlaying()) songProgress++; setValue(maxValue*songProgress/songFrames[songIndex]); } public void setValue(int amount) { value = amount; if (amount < 0) amount = 0; if (amount > maxValue) amount = maxValue; if (amount != value) { value = amount; 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; } }
valueBar.setImage(img);