Class KeyboardComponent
- All Implemented Interfaces:
ImageObserver
,MenuContainer
,Serializable
,Accessible
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:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
Piano keyboard orientation.Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
Nested classes/interfaces inherited from class java.awt.Component
Component.BaselineResizeBehavior
-
Field Summary
Fields inherited from class javax.swing.JComponent
TOOL_TIP_TEXT_KEY, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
-
Constructor Summary
ConstructorDescriptionCreate an horizontal component with 88 notes and Orientation.DOWN, and no out of range indicators.KeyboardComponent
(KeyboardRange kbdSize) Create an horizontal component with the specified size, Orientation.DOWN, and no out of range indicators.KeyboardComponent
(KeyboardRange kbdSize, KeyboardComponent.Orientation orientation, boolean useOutOfRangeIndicator) Create a keyboard with the specified parameters. -
Method Summary
Modifier and TypeMethodDescriptionGet all the PianoKeys.getKey
(int pitch) Get the PianoKey for specified pitch.Get the PianoKey that correspond to a specific point.Get the rectangle enclosing all the keys.int
getPressedVelocity
(int pitch) Get the pressed velocity of a specific key.getRange()
float
float
void
Overridden to paint the out of range indicators.void
reset()
Set all keys unpressed, remove all marks.void
setEnabled
(boolean b) void
Set the marked status of specified key.void
setPressed
(int pitch, int velocity, Color pressedKeyColor) Set the pressed status of specified key.final void
setRange
(KeyboardRange kbdRange) Set the keyboard size.void
setReleased
(int pitch) Release the specified key.void
setScaleFactor
(float factorX, float factorY) Make the component smaller or larger.toString()
boolean
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, setUI, updateUI
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintImmediately, paintImmediately, print, printAll, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, contains, createImage, createImage, createVolatileImage, createVolatileImage, dispatchEvent, enable, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, transferFocus, transferFocusBackward, transferFocusUpCycle
-
Constructor Details
-
KeyboardComponent
public KeyboardComponent()Create an horizontal component with 88 notes and Orientation.DOWN, and no out of range indicators. -
KeyboardComponent
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
-
toString
-
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
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
Get the rectangle enclosing all the keys.- Returns:
- The Rectangle position is relative to this KeyboardComponent.
-
getAllKeys
Get all the PianoKeys.- Returns:
-
getBlackKeys
-
getWhiteKeys
-
getKey
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 classJComponent
-
reset
public void reset()Set all keys unpressed, remove all marks. -
setReleased
public void setReleased(int pitch) Release the specified key.- Parameters:
pitch
-
-
setPressed
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
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
Get the PianoKey that correspond to a specific point.- Parameters:
p
- A Point object relative to this component.- Returns:
- Can be null.
-
paintComponent
Overridden to paint the out of range indicators.- Parameters:
g
-
-
getMinimumSize
- Overrides:
getMinimumSize
in classJComponent
-
getPreferredSize
- Overrides:
getPreferredSize
in classJComponent
-