📄 drummercanvas.java
字号:
package Drummer;import java.util.*;import java.io.*;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.media.*;import javax.microedition.media.control.*;public class DrummerCanvas extends Canvas implements CommandListener { private static final String TITLE = "Drummer例子"; private static final String LOGO = "/Logo.png"; private Command backCommand = new Command("返回", Command.BACK, 1); private Command exitCommand = new Command("退出", Command.EXIT, 1); private Command helpCommand = new Command("帮助", Command.ITEM, 10); private static int TITLE_TOP = 2; private static int LOGO_GAP = 2; private static int CURR_DRUM_GAP = 2; private static int STATUS_GAP = 2; private Image logo = null; private Display parent; private String status = ""; private String currDrum = ""; private int displayWidth = -1; private int displayHeight = -1; private int textHeight = 10; private int logoTop = 0; private int currDrumTop = 0; private int statusTop = 0; private MIDIControl mc; //MIDI鼓声数据 private static final int[] DRUM_NUMBERS = { 0, // not used 0x2A, // 1: closed hihat 0x2E, // 2: open hihat 0x36, // 3: Tambourine 0x32, // 4: hi tom 0x2F, // 5: mid tom 0x2B, // 6: low tom 0x33, // 7: ride cymbal 0x38, // 8: cow bell 0x31, // 9: crash cymbal 0x24, // *: bass drum 0x27, // 0: hand clap 0x28, // #: snare drum }; //屏幕显示的当前敲击的鼓声名称 private static final String[] DRUM_NAMES = { "", // not used "Closed Hi-Hat", // 1 "Open Hi-Hat", // 2 "Tambourine", // 3 "Hi Tom", // 4 "Mid Tom", // 5 "Low Tom", // 6 "Ride Cymbal", // 7 "Cow Bell", // 8 "Crash Cymbal", // 9 "Bass Drum", // * "Hand Clap", // 0 "Snare Drum", // # }; private static final String[] SHORT_DRUM_NAMES = { "", "CH", "OH", "TB", "HT", "MT", "LT", "RC", "CB", "CC", "BD", "HC", "SD", }; private static final int[] DRUM_KEYS = { 0, // not used KEY_NUM1, KEY_NUM2, KEY_NUM3, KEY_NUM4, KEY_NUM5, KEY_NUM6, KEY_NUM7, KEY_NUM8, KEY_NUM9, KEY_STAR, KEY_NUM0, KEY_POUND, }; public DrummerCanvas(Display dis) { super(); this.parent = dis; show(); } public void commandAction(Command c, Displayable s) { if (c == backCommand) { parent.setCurrent(this); } else if (c == helpCommand) { showHelp(); } if (c ==exitCommand) { } } private void status(String s) { status = s; repaint(0, statusTop, displayWidth, textHeight); serviceRepaints(); } //提示当前敲鼓声音 private void setCurrDrum(int num) { currDrum = DRUM_NAMES[num]; repaint(0, currDrumTop, displayWidth, textHeight); serviceRepaints(); } public void updateDisplay() { repaint(); serviceRepaints(); } //初始化DrummerDemo public void show() { addCommand(exitCommand); addCommand(helpCommand); setCommandListener(this); status("Prefetching and getting MIDIControl"); updateDisplay(); new Thread(new Runnable() { public void run() { try { mc = getMIDIControl(); status("Ready."); } catch (Exception e) { //status(Utils.friendlyException(e)); mc = null; } } }).start(); } protected void keyPressed(int keycode) { try { for (int num = 1; num < DRUM_KEYS.length; num++) { if (keycode == DRUM_KEYS[num]) { playDrum(num); return; } } } catch (Throwable t) { } } //播放鼓声 private void playDrum(int num) { if (mc == null) return; setCurrDrum(num); mc.shortMidiEvent(0x99, DRUM_NUMBERS[num], 120); mc.shortMidiEvent(0x99, DRUM_NUMBERS[num], 0); } private boolean intersects(int clipY, int clipHeight, int y, int h) { return (clipY <= y + h && clipY + clipHeight >= y); } public void paint(Graphics g) { try { if (displayHeight == -1) { displayWidth = getWidth(); displayHeight = getHeight(); textHeight = g.getFont().getHeight(); logo = getLogo(); int currTop = TITLE_TOP + textHeight; if (logo != null) { currTop += LOGO_GAP; logoTop = currTop; currTop += logo.getHeight(); } // curr drum: before-last line currDrumTop = displayHeight - 2 * textHeight - STATUS_GAP; // status: last line. statusTop = displayHeight - textHeight; } int clipX = g.getClipX(); int clipY = g.getClipY(); int clipWidth = g.getClipWidth(); int clipHeight = g.getClipHeight(); // 绘画背景 g.setColor(0); g.fillRect(clipX, clipY, clipWidth, clipHeight); // 绘画标题 if (intersects(clipY, clipHeight, TITLE_TOP, textHeight)) { g.setColor(0xFF7f00); g.drawString(TITLE, displayWidth >> 1, TITLE_TOP, Graphics.TOP | Graphics.HCENTER); } // 显示图标 if (logo != null && intersects(clipY, clipHeight, logoTop, logo.getHeight())) { g.drawImage(logo, displayWidth / 2, logoTop, Graphics.TOP | Graphics.HCENTER); } // 提示当前鼓声名称 if (intersects(clipY, clipHeight, currDrumTop, textHeight)) { g.setColor(0xE0E0FF); g.drawString(currDrum, 0, currDrumTop, Graphics.TOP | Graphics.LEFT); } // 状态 if (intersects(clipY, clipHeight, displayHeight - textHeight, textHeight)) { g.setColor(0xFAFAFA); g.drawString(status, 0, displayHeight, Graphics.BOTTOM | Graphics.LEFT); } } catch (Throwable t) { } } /** * 显示帮组 */ public void showHelp() { List list = new List("Drummer Help", Choice.IMPLICIT); list.append("Use the numeric keys", null); list.append("to play drums. Each", null); list.append("Key corresponds to a", null); list.append("different drum.", null); list.addCommand(backCommand); list.setCommandListener(this); parent.setCurrent(list); } private Image getLogo() { if (logo == null) { try { logo = Image.createImage(LOGO); } catch (Exception ex) { logo = null; } if (logo == null) { } } return logo; } MIDIControl getMIDIControl() throws Exception { Player mp = null; if (mp == null) { mp = Manager.createPlayer(Manager.MIDI_DEVICE_LOCATOR); mp.prefetch(); } return (MIDIControl) mp .getControl("javax.microedition.media.control.MIDIControl"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -