Class KeyboardComponent

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

public class KeyboardComponent extends JPanel
A JPanel representing a Piano keyboard with selectable keys.

The keyboard respects a fixed width/length ratio and is painted centered.

Features:
- 4 possible orientations
- optional "out of range" graphic indicators
- optional text on notes
- show pressed notes with color depending on velocity
- different keyboard sizes
- scalable

See Also:
  • Constructor Details

    • KeyboardComponent

      public KeyboardComponent()
      Create an horizontal component with 88 notes and Orientation.DOWN, and no out of range indicators.

    • KeyboardComponent

      public KeyboardComponent(KeyboardRange kbdSize)
      Create an horizontal component with the specified size, Orientation.DOWN, and no out of range indicators.

      Parameters:
      kbdSize -
    • KeyboardComponent

      public KeyboardComponent(KeyboardRange kbdSize, KeyboardComponent.Orientation orientation, boolean useOutOfRangeIndicator)
      Create a keyboard with the specified parameters.
      Parameters:
      kbdSize -
      orientation -
      useOutOfRangeIndicator - Show an indicator on the side if a note is out of the range of the keyboard.
  • Method Details

    • useOutOfRangeIndicator

      public boolean useOutOfRangeIndicator()
    • getOrientation

      public KeyboardComponent.Orientation getOrientation()
    • toString

      public String toString()
      Overrides:
      toString in class Component
    • getRange

      public KeyboardRange getRange()
    • setScaleFactor

      public void setScaleFactor(float factorX, float factorY)
      Make the component smaller or larger.
      Parameters:
      factorX - A value > 0. Impact the keyboard width in DOWN/UP orientation (height in LEFT/RIGHT orientation).
      factorY - A value > 0. Impact the keyboard height in DOWN/UP orientation (width in LEFT/RIGHT orientation).
    • getScaleFactorX

      public float getScaleFactorX()
    • getScaleFactorY

      public float getScaleFactorY()
    • setRange

      public final void setRange(KeyboardRange kbdRange)
      Set the keyboard size.

      New PianoKeys are created. Pressed/marked notes are maintained. This updates also the preferred and minimum size. Caller must synchronize this method if other threads update this keyboard in parallel.

      Parameters:
      kbdRange -
    • getKeysBounds

      public Rectangle getKeysBounds()
      Get the rectangle enclosing all the keys.
      Returns:
      The Rectangle position is relative to this KeyboardComponent.
    • getAllKeys

      public List<PianoKey> getAllKeys()
      Get all the PianoKeys.
      Returns:
    • getBlackKeys

      public List<PianoKey> getBlackKeys()
    • getWhiteKeys

      public List<PianoKey> getWhiteKeys()
    • getKey

      public PianoKey getKey(int pitch)
      Get the PianoKey for specified pitch.

      Parameters:
      pitch -
      Returns:
      Can be null if pitch is out of range.
    • setEnabled

      public void setEnabled(boolean b)
      Overrides:
      setEnabled in class JComponent
    • reset

      public void reset()
      Set all keys unpressed, remove all marks.
    • setReleased

      public void setReleased(int pitch)
      Release the specified key.
      Parameters:
      pitch -
    • setPressed

      public void setPressed(int pitch, int velocity, Color pressedKeyColor)
      Set the pressed status of specified key.

      Method delegates to setPressed() of the relevant PianoKey.

      If pitch is outside the KeyboardRange, show an indicator on the leftmost/rightmost note.

      Parameters:
      pitch -
      velocity - If 0 equivalent to calling setReleased()
      pressedKeyColor - The pressed key color to be used. If null use default color.
    • setMarked

      public void setMarked(int pitch, Color markColor)
      Set the marked status of specified key.

      Method delegates to setMarked() of the relevant PianoKey.

      Parameters:
      pitch -
      markColor - If null remove the mark.
    • getPressedVelocity

      public int getPressedVelocity(int pitch)
      Get the pressed velocity of a specific key.

      Method delegates to getVelocity() of the relevant PianoKey.

      Parameters:
      pitch - The pitch of the key. Must be a valid pitch for the KeyboardRange.
      Returns:
      If 0 it means the key is released.
    • getKey

      public PianoKey getKey(Point p)
      Get the PianoKey that correspond to a specific point.
      Parameters:
      p - A Point object relative to this component.
      Returns:
      Can be null.
    • paintComponent

      public void paintComponent(Graphics g)
      Overridden to paint the out of range indicators.
      Parameters:
      g -
    • getMinimumSize

      public Dimension getMinimumSize()
      Overrides:
      getMinimumSize in class JComponent
    • getPreferredSize

      public Dimension getPreferredSize()
      Overrides:
      getPreferredSize in class JComponent