views:

175

answers:

5
+1  Q: 

Drawing A Piano

Hello Everyone,

I have started working on a software synthesizer (or keyboard). I have decided to use Java because of the available Jfugue API. I am trying to figure out how to go about creating the actual keys (notes) of the keyboard user interface, but I am stuck. I have tried to create an interface by dragging/dropping black and white rectangular buttons onto the panel, but that doesn't seem to work. Could someone point me into the right direction?

A: 

If you're using Java/swing, your best bet would be to draw an image of a keyboard and map click locations to the individual keyboard keys.

Randolpho
A: 

I once had to do something similar... the best solution I found was to draw panels and set their border and background as keys. I then used mouse listeners to determine which panel was clicked and the duration of the click.

npinti
+1  A: 

Check out reply 9 of this posting for a simple piano keyboard that should get you started. You may want to read the posting from the beginning for ideas on how to handle the note represented by each button.

camickr
A: 

I think this is what you want to:

  1. Create a class that extends JPanel
  2. Override the paint() method to draw the piano keys
  3. Implement MouseListener and MouseMotionListener, and know when the user has clicked the mouse over certain keys
  4. Use the StreamingPlayer in JFugue to play notes dynamically

I'm the author of JFugue, and I've also wanted to start a side-effort, JFugueUI, that would provide things like graphical keyboards that people could include in their own applications - so, let me know if you're interested in contributing!

David