Piano MIDlet : Audio Media « J2ME « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » J2ME » Audio MediaScreenshots 
Piano MIDlet
Piano MIDlet

/*
Wireless Java 2nd edition 
Jonathan Knudsen
Publisher: Apress
ISBN: 1590590775 
*/
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 = {
     01116293248596476809396
  };
  
  private static final int[] kNoteWidth = {
    16,  816,  81616,  816,  816,  816
  };
  
  private static final int[] kNoteHeight = {
    966496649696649664966496
  };
  
  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[08;
    mMiddleCX = (w - fullWidth2;
    mMiddleCY = (h - kNoteHeight[0]) 2;
    
    mCurrentNote = 60;
  }
  
  public void paint(Graphics g) {
    int w = getWidth();
    int h = getHeight();
    
    g.setColor(0xffffff);
    g.fillRect(00, 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 - n12 5* 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 - n12 5* 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 - sw2;
    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, 1000100)}
        catch (MediaException me) {}
        break;
      default:
        break;
    }
  }
}



           
       
Related examples in the same category
1. Audio MIDletAudio MIDlet
2. Media Information MIDletMedia Information MIDlet
3. Tone MIDletTone MIDlet
4. demonstrate the functionality supported by javax.microedition.lcdui package.
5. Sound AlertSound Alert
w__w__w___.___jav___a__2_s__.___c___o_m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.