java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
org.jjazz.instrumentcomponents.keyboard.api.PianoKey
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable

public class PianoKey extends JComponent
A piano keyboard key.

Can show if key is selected, or if pressed with an indication of the velocity. A text can also be shown.

See Also:
  • Field Details

    • WW

      public static final int WW
      Standard White Key Width in DOWN orientation.
      See Also:
    • WH

      public static final int WH
      Standard White Key Height in DOWN orientation.
      See Also:
    • WW_MIN

      public static final int WW_MIN
      Minimum White Key Width in DOWN orientation..
      See Also:
    • WH_MIN

      public static final int WH_MIN
      Minimum White Key Height in DOWN orientation..
      See Also:
  • Constructor Details

    • PianoKey

      public PianoKey(int p)
      Create a standard Pianokey with Orientation.DOWN.
      Parameters:
      p -
    • PianoKey

      public PianoKey(int p, boolean leftMost, boolean rightMost, KeyboardComponent.Orientation orientation)
      Construct a piano key for a specified pitch.
      Parameters:
      p - The pitch of the key.
      leftMost - If true this the leftmost key of the keyboard (different shape)
      rightMost - If true this the rightmost key of the keyboard (different shape)
      orientation -
  • Method Details

    • getColorProperty

      public Color getColorProperty(String key)
    • setColorProperty

      public void setColorProperty(String key, Color c)
    • getText

      public String getText()
    • setText

      public void setText(String text)
      Show an horizontal text on the note.
      Parameters:
      text - Shorter is better! If null no text is shown.
    • isWhiteKey

      public final boolean isWhiteKey()
    • setMarked

      public void setMarked(Color color)
      Add a mark on the note using the specified color.
      Parameters:
      color - If null remove the existing mark.
    • setReleased

      public void setReleased()
      Set this key released (velocity is set to 0).

      Fire a change event if velocity was changed. Do nothing if this key is not enabled.

    • setPressed

      public void setPressed(int v, Color pressedKeyColor)
      Set the key in the "pressed" state.

      Key is pressed if velocity > 0. If velocity==0, setReleased() is called. Fire a changed event if velocity was changed. Do nothing if this key is not enabled.

      Parameters:
      v -
      pressedKeyColor - Set the pressed color for white key (pressed color for a black key is calculated from this color as well). If null use default color.
    • setSelected

      public void setSelected(boolean b)
    • isSelected

      public boolean isSelected()
    • getVelocity

      public int getVelocity()
      If velocity is > 0 then it means the key is being pressed.
      Returns:
    • release

      public void release()
      Convenience method, same as setPressed(0).
    • isPressed

      public boolean isPressed()
      Convenience method, same as getVelocity() > 0.
      Returns:
    • getPitch

      public int getPitch()
    • getNextKeyPosX

      public int getNextKeyPosX()
      Return the relative X position of the next key, ie from left to right in DOWN orientation.
      Returns:
    • paint

      public void paint(Graphics g)
      Draw the key.
      Overrides:
      paint in class JComponent
      Parameters:
      g - The Graphics context in which to draw.
    • getAdaptedColor

      public static Color getAdaptedColor(Color c, int midiValue)
      Change the color c according to midiValue.
      Parameters:
      c -
      midiValue - A value 0-127.
      Returns:
    • setSizeRelativeToWhiteKeyRef

      public final void setSizeRelativeToWhiteKeyRef(int wwRef, int whRef)
      Change the size of the key from a reference rectangular white key size.
      Parameters:
      wwRef - The base width of a reference white key in DOWN orientation.
      whRef - The height of a reference white key in DOWN orientation.
    • contains

      public boolean contains(int x, int y)
      Determine whether the key polygon contains a specified point x,y.
      Overrides:
      contains in class JComponent
      Parameters:
      x -
      y -
    • isShowVelocityColor

      public boolean isShowVelocityColor()
      Returns:
      the showVelocityColor
    • setShowVelocityColor

      public void setShowVelocityColor(boolean showVelocityColor)
      Set if pressed color is adjusted depending on note velocity.
      Parameters:
      showVelocityColor - the showVelocityColor to set
    • getPressedBlackKeyColor

      public Color getPressedBlackKeyColor()
      Returns:
      Based on getPressedWhiteKeyColor()
    • getPressedWhiteKeyColor

      public Color getPressedWhiteKeyColor()
      Returns:
      the pressedWhiteKeyColor
    • getMarkedColor

      public Color getMarkedColor()
      Returns:
    • setPressedWhiteKeyColor

      public void setPressedWhiteKeyColor(Color pressedKeyColor)
      Set the pressed color for a white key.

      Used also to derive the pressed color for a black key.

      Parameters:
      pressedKeyColor - the pressedKeyColor to set
    • toString

      public String toString()
      Overrides:
      toString in class Component