ソースコードの追記 |
下記のようにMainCanvas.javaを書き換えます。 |
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Random;
import com.jblend.media.smaf.*;
import com.jblend.media.*;
import java.io.*;
class MainCanvas extends Canvas implements CommandListener,
MediaPlayerListener
{
private Command diceCmd = new
Command("サイコロ",Command.SCREEN,2);
private Command playCmd = new Command("再生",Command.SCREEN,2);
private Command stopCmd = new Command("停止",Command.SCREEN,2);
private Command quitCmd = new Command("終了",Command.SCREEN,1);
private DiceMusic diceMusic;
Random rnd = new Random();
int dice[] = new int[8*2];
boolean init_flg = false;
int play_index;
SmafPlayer player=new SmafPlayer();
SmafData sound[]=new SmafData[187];
public MainCanvas(DiceMusic diceMusic)
{
this.diceMusic = diceMusic;
addCommand(quitCmd);
addCommand(diceCmd);
setCommandListener(this);
try {
int
num_sound_file = 0;
int i;
for(i=0;i<2;i++)
{
String Part = new String();
Part = "";
if(i==0)
{
Part = "A";
}
else
{
Part = "B";
}
int j;
for(j=2;j<=12;j++)
{
String Dice = new String();
Dice = "";
if(j<10)
{
Dice = Dice + "0";
}
Dice = Dice + Integer.toString(j);
int k;
for(k=1;k<=9;k++)
{
String Bar = new String();
Bar = "";
if(i==1 && k==9)
{
continue;
}
Bar = Bar + Integer.toString(k);
sound[num_sound_file] =new SmafData("resource:sound/"+Part+Dice+Bar+".mmf");
num_sound_file++;
}
}
}
}catch(Exception e){
System.out.println(e.toString());
}
}
public void paint( Graphics g )
{
if(init_flg == false)
{
init_flg =
true;
return;
}
Font font =
Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_PLAIN,Font.SIZE_SMALL);
g.setFont(font);
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
int i;
g.setColor(0,0,0);
for(i=0;i<8*2;i++)
{
g.drawString(Integer.toString(i+1)+"回目のサイコロ",0,i*13+30,Graphics.LEFT
| Graphics.TOP);
g.drawString(Integer.toString(dice[i]),100,i*13+30,Graphics.LEFT
| Graphics.TOP);
}
}
public void commandAction(Command c, Displayable d){
if (c.getLabel() == "サイコロ")
{
int i;
for(i=0;i<8*2;i++)
{
dice[i] = rnd.nextInt(11)+2;
}
removeCommand(diceCmd);
addCommand(playCmd);
repaint();
}
else if (c.getLabel() == "再生")
{
play_index = 0;
int idx = getSoundIndex();
removeCommand(playCmd);
addCommand(stopCmd);
player.setData(sound[play_index]);
player.play();
player.addMediaPlayerListener(this);
}
else if (c.getLabel() == "停止")
{
removeCommand(stopCmd);
addCommand(diceCmd);
player.removeMediaPlayerListener(this);
player.stop();
}
else if(c.getLabel() == "終了" )
{
diceMusic.exit();
}
}
private int getSoundIndex()
{
int idx = 0;
if(play_index <16)
{
if(play_index ==15)
{
idx = 9*(dice[play_index%8]-2) + 8;
}
else
{
idx = 9*(dice[play_index%8]-2) + play_index%8;
}
}
else if(play_index <24)
{
idx = 99 +
8*(dice[play_index-8]-2)+(play_index-16);
}
return idx;
}
public void playerRepeated(MediaPlayer play) {
}
public void playerStateChanged(MediaPlayer play) {
if (play.getState() ==
MediaPlayer.READY) { //停止中
player.removeMediaPlayerListener(this);
play_index = play_index + 1;
if(play_index <24)
{
int idx = getSoundIndex();
player.setData(sound[play_index]);
player.play();
player.addMediaPlayerListener(this);
}
else
{
player.stop();
removeCommand(stopCmd);
addCommand(diceCmd);
}
}
if (play.getState() ==
MediaPlayer.ERROR) { //エラー発生により停止中
}
}
} |
|
|