博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Canvas和Media实现钢琴效果(转)
阅读量:2495 次
发布时间:2019-05-11

本文共 2944 字,大约阅读时间需要 9 分钟。

本例使用Canvas绘制钢琴的界面,同时使用了MIDP 2.0 media子系统来播放钢琴按键触发的声音效果。是个不错的图形用户界面和音效结合的范例。

/*

* PianoMIDlet.java
*
* Created on 2005年12月6日, 下午2:02
*/

package com.j2medev.piano;

import javax.microedition.media.*;

import javax.microedition.lcdui.*;

import javax.microedition.midlet.*;

public class PianoMIDlet extends MIDlet {

public void startApp() {
Displayable d = new PianoCanvas();
d.addCommand(new Command("Exit", Command.EXIT, 0));
d.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable s) {
notifyDestroyed();
}
});
Display.getDisplay(this).setCurrent(d);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
}

class PianoCanvas extends Canvas {

private static final int[] kNoteX = {
0, 11, 16, 29, 32, 48, 59, 64, 76, 80, 93, 96
};
private static final int[] kNoteWidth = {
16, 8, 16, 8, 16, 16, 8, 16, 8, 16, 8, 16
};
private static final int[] kNoteHeight = {
96, 64, 96, 64, 96, 96, 64, 96, 64, 96, 64, 96
};
private static final boolean[] kBlack = {
false, true, false, true, false,
false, true, false, true, false, true, false
};
private int mMiddleCX, mMiddleCY;
private int mCurrentNote;
public PianoCanvas() {
int w = getWidth();
int h = getHeight();
int fullWidth = kNoteWidth[0] * 8;
mMiddleCX = (w - fullWidth) / 2;
mMiddleCY = (h - kNoteHeight[0]) / 2;
mCurrentNote = 60;
}
public void paint(Graphics g) {
int w = getWidth();
int h = getHeight();
g.setColor(0xffffff);
g.fillRect(0, 0, w, h);
g.setColor(0x000000);
for (int i = 60; i <= 72; i++)
drawNote(g, i);
drawSelection(g, mCurrentNote);
}
private void drawNote(Graphics g, int note) {
int n = note % 12;
int octaveOffset = ((note - n) / 12 - 5) * 7 * kNoteWidth[0];
int x = mMiddleCX + octaveOffset + kNoteX[n];
int y = mMiddleCY;
int w = kNoteWidth[n];
int h = kNoteHeight[n];
if (isBlack(n))
g.fillRect(x, y, w, h);
else
g.drawRect(x, y, w, h);
}
private void drawSelection(Graphics g, int note) {
int n = note % 12;
int octaveOffset = ((note - n) / 12 - 5) * 7 * kNoteWidth[0];
int x = mMiddleCX + octaveOffset + kNoteX[n];
int y = mMiddleCY;
int w = kNoteWidth[n];
int h = kNoteHeight[n];
int sw = 6;
int sx = x + (w - sw) / 2;
int sy = y + h - 8;
g.setColor(0xffffff);
g.fillRect(sx, sy, sw, sw);
g.setColor(0x000000);
g.drawRect(sx, sy, sw, sw);
g.drawLine(sx, sy, sx + sw, sy + sw);
g.drawLine(sx, sy + sw, sx + sw, sy);
}
private boolean isBlack(int note) {
return kBlack[note];
}
public void keyPressed(int keyCode) {
int action = getGameAction(keyCode);
switch (action) {
case LEFT:
mCurrentNote--;
if (mCurrentNote < 60)
mCurrentNote = 60;
repaint();
break;
case RIGHT:
mCurrentNote++;
if (mCurrentNote > 72)
mCurrentNote = 72;
repaint();
break;
case FIRE:
try {
Manager.playTone(mCurrentNote, 1000, 100);
} catch (MediaException me) {
}
break;
default:
break;
}
}
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10294527/viewspace-127163/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10294527/viewspace-127163/

你可能感兴趣的文章
echarts 为例讲解 uni-app 如何引用 npm 第三方库
查看>>
uni-app跨页面、跨组件通讯
查看>>
springmvc-helloworld(idea)
查看>>
JDK下载(百度网盘)
查看>>
idea用得溜,代码才能码得快
查看>>
一篇掌握python魔法方法详解
查看>>
数据结构和算法5-非线性-树
查看>>
数据结构和算法6-非线性-图
查看>>
数据结构和算法7-搜索
查看>>
数据结构和算法8-排序
查看>>
windows缺少dll解决办法
查看>>
JPA多条件动态查询
查看>>
JPA自定义sql
查看>>
BigDecimal正确使用了吗?
查看>>
joplin笔记
查看>>
JNDI+springmvc使用
查看>>
vue+springboot分页交互
查看>>
vue+springboot打包发布
查看>>
XSL 开发总结
查看>>
【NOI 2018】归程(Kruskal重构树)
查看>>