ronald wrote...
I have no code of the progress bar

1 | int lengthCurrentSong; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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++; } } |
1 2 3 4 5 6 7 8 | 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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | 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; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | 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; } } |
1 | valueBar.setImage(img); |