J2ME音乐程序【原创】
import
java.io.IOException;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.io.file.IllegalModeException;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
class DemoCanvas extends Canvas{
byte C4 = ToneControl.C4;
byte D4 = (byte)(C4+2);
byte E4 = (byte)(C4+4);
byte G4 = (byte)(C4+5);
byte S4 = (byte)(C4+7);
byte L4 =(byte)(C4+9);
byte X4 = (byte)(C4+11);
byte O4 = (byte)(C4+13);
private void simpleTone(){
try{
Manager.playTone(ToneControl.C4, 100, 80);
}catch (Exception ex){}
}
protected void keyPressed(int keyCode){
if(keyCode == KEY_NUM1)
{
simpleTone();
}
else if(keyCode == KEY_NUM2)
{
try{
Manager.playTone(D4, 100, 80);
}catch (Exception ex){}
}else
if(keyCode == KEY_NUM3){
try{
Manager.playTone(E4, 100, 80);
}catch (Exception ex){}
}
else if(keyCode == KEY_NUM4){
try{
Manager.playTone(G4, 100, 80);
}catch (Exception ex){}
}
else if(keyCode == KEY_NUM5)
{
try{
Manager.playTone(S4, 100, 80);
}catch (Exception ex){}
}
else if(keyCode == KEY_NUM6){
try{
Manager.playTone(L4, 100, 80);
}catch (Exception ex){}
}else if(keyCode == KEY_NUM7)
{
try{
Manager.playTone(X4, 100, 80);
}catch (Exception ex){}
}else if(keyCode == KEY_NUM8){
try{
Manager.playTone(O4, 100, 80);
}catch (Exception ex){}
}
}
protected void paint(Graphics g) {
g.setColor(0x00FF6633);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(0x00ffffff);
g.drawString("│5 3 3 -│4 2 2 -│1 2 3 4│5 5 5 -│ ", 0, 0, Graphics.TOP|Graphics.LEFT);
g.drawString("│5 3 3 -│4 2 2 -│1 3 5 5│3 - - -│ ", 0, 40, Graphics.TOP|Graphics.LEFT);
g.drawString("│2 2 2 2│2 3 4 -│3 3 3 3│3 4 5 -│ ", 0, 80, Graphics.TOP|Graphics.LEFT);
g.drawString("│5 3 3 -│4 2 2 -│1 3 5 5│1 - - -│ ", 0, 120, Graphics.TOP|Graphics.LEFT);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE));
g.drawString("请根据以上乐谱弹奏可爱的《小蜜蜂》吧 ", 0, 140, Graphics.TOP|Graphics.LEFT);
}
}
public class PlaySimpleTone extends MIDlet implements CommandListener {
private Player mp;
private Display display;
private DemoCanvas myDemo;
private Command exitCommand = new Command("Exit",Command.EXIT,1);
private Command playCommand = new Command("Play",Command.ITEM,1);
public PlaySimpleTone() {
display = Display.getDisplay(this);
myDemo = new DemoCanvas();
myDemo.addCommand(exitCommand);
myDemo.addCommand(playCommand);
myDemo.setCommandListener(this);
}
public void commandAction(Command c,Displayable s)
{
if(c == exitCommand){
try {
destroyApp(false);
} catch (MIDletStateChangeException e) {
e.printStackTrace();
}
notifyDestroyed();
}
else if(c == playCommand){
simpleTone();
}
}
private void simpleTone(){
try{
Manager.playTone(ToneControl.C4, 100, 80);
}catch (Exception ex){}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
display.setCurrent(myDemo);
}
}