国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Canvas和Media實現鋼琴效果

2019-11-18 16:05:59
字體:
來源:轉載
供稿:網友

Canvas和Media實現鋼琴效果本例使用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;
        }
    }
}


(出處:http://m.survivalescaperooms.com)



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙川县| 营口市| 乐至县| 库车县| 宜都市| 永吉县| 密云县| 天水市| 河池市| 湖口县| 林口县| 大渡口区| 凤城市| 朝阳县| 祁连县| 东港市| 米脂县| 永顺县| 常熟市| 宜良县| 七台河市| 南投县| 浙江省| 拉萨市| 乌恰县| 凉山| 乌审旗| 苍南县| 肥东县| 五常市| 诸暨市| 财经| 福海县| 宁强县| 中山市| 海林市| 鹤岗市| 北票市| 滦南县| 凉城县| 青铜峡市|