General Question

yannick's avatar

Can I set a 'limit' on an integer value in a java applet?

Asked by yannick (985points) May 1st, 2009

I’m writing some code for a jukebox-like java applet, in which I have an integer value (‘nowplaying’) which relates to arrays I have made which contain the song files, album art etc.

Clicking the skip forward or skip back buttons skips to the next or previous track in the array (by adding/subtracting one from the ‘nowplaying’ value. Of course the problem is that if the next/previous button is repeatedly clicked, the value goes beyond 0 or 10 and screws up the applet. Is there a way to prevent this?

Here is the relevant code:

int nowplaying = 0; (initialization)

public void init()
{
song[0] = getAudioClip(getCodeBase(), “resources/Hawaii_50.mid”);
song[1] = getAudioClip(getCodeBase(), “resources/I_Dream_Of_Jeannie.mid”);
song[2] = getAudioClip(getCodeBase(), “resources/Muppet_Show.mid”);
song[3] = getAudioClip(getCodeBase(), “resources/N64_Goldeneye007.mid”);
song[4] = getAudioClip(getCodeBase(), “resources/N64_MarioParty.mid”);
song[5] = getAudioClip(getCodeBase(), “resources/Pokemon.mid”);
song[6] = getAudioClip(getCodeBase(), “resources/Power_Rangers.mid”);
song[7] = getAudioClip(getCodeBase(), “resources/Seinfeld.mid”);
song[8] = getAudioClip(getCodeBase(), “resources/SG_SonicTheHedgehog.mid”);
song[9] = getAudioClip(getCodeBase(), “resources/SPS_FinalFantasy7.mid”);

songname[0] = (“1. Hawaii 5–0”);
songname[1] = (“2. I Dream of Jeannie”);
songname[2] = (“3. Muppet Show”);
songname[3] = (“4. Golden Eye”);
songname[4] = (“5. Mario Party”);
songname[5] = (“6. Pokemon”);
songname[6] = (“7. Power Rangers”);
songname[7] = (“8. Seinfeld”);
songname[8] = (“9. Sonic the Hedgehog”);
songname[9] = (“10. Final Fantasy 7”);

play = new Button(“Play”);
add(play);
play.addActionListener(this);
stop = new Button(“Stop”);
add(stop);
stop.addActionListener(this);
prev = new Button(”<<”);
add(prev);
prev.addActionListener(this);
next = new Button(”>>”);
add(next);
next.addActionListener(this);
}

This is the section giving me trouble:

public void actionPerformed(ActionEvent e)
{
Button source = (Button)e.getSource();
if (source.getLabel() == “Play”)
{
song[nowplaying].play();
repaint();
}
if(source.getLabel() == “Stop”)
{
song[nowplaying].stop();
repaint();
}
if(source.getLabel() == ”>>”)
{
song[nowplaying].stop();
nowplaying += 1;
//song[nowplaying].play();
repaint();
}
if(source.getLabel() == ”<<”)
{
song[nowplaying].stop();
nowplaying -= 1;
//song[nowplaying].play();
repaint();
}
}

Observing members: 0 Composing members: 0

5 Answers

Ivan's avatar

At the beginning of the function, always check to see if ‘nowplaying’ is > 9. If it is, set it to 0 and then have the function run normally. I’m familiar with C++, so I can’t really help you with the syntax, but this should be pretty easy.

ben's avatar

You can also use the mod (%) function, which will take the remainder when divided by something.
so instead of
nowplaying +=1
You could could
nowplaying = (nowplaying +1) % 10
And similarly for -1. (though you depending on how you want it to wrap you may want to try +9 instead of -1).

phoenyx's avatar

I prefer using something like song.length() instead of magic numbers like “10”

ben's avatar

excellent point, @phoenyx

yannick's avatar

Awesome, well I gave your suggestions and try and got it doing exactly what I wanted so thanks a lot for all the help :)

In lew of this question being answered, another has sprouted up: is there a limit to individual file size or total applet size or something like that for java applets? When I try and replace the .mid files seen in the above code with .au or .wav files, I only seem to be able to initialize a few instead of the 10 I need to…

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther