Class PianoKey
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
org.jjazz.instrumentcomponents.keyboard.api.PianoKey
- All Implemented Interfaces:
ImageObserver
,MenuContainer
,Serializable
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:
-
Nested Class Summary
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
Nested classes/interfaces inherited from class java.awt.Component
Component.BaselineResizeBehavior
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Standard White Key Height in DOWN orientation.static final int
Minimum White Key Height in DOWN orientation..static final int
Standard White Key Width in DOWN orientation.static final int
Minimum White Key Width in DOWN orientation..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
ConstructorDescriptionPianoKey
(int p) Create a standard Pianokey with Orientation.DOWN.PianoKey
(int p, boolean leftMost, boolean rightMost, KeyboardComponent.Orientation orientation) Construct a piano key for a specified pitch. -
Method Summary
Modifier and TypeMethodDescriptionboolean
contains
(int x, int y) Determine whether the key polygon contains a specified point x,y.static Color
getAdaptedColor
(Color c, int midiValue) Change the color c according to midiValue.getColorProperty
(String key) int
Return the relative X position of the next key, ie from left to right in DOWN orientation.int
getPitch()
getText()
int
If velocity is > 0 then it means the key is being pressed.boolean
Convenience method, same as getVelocity() > 0.boolean
boolean
final boolean
void
Draw the key.void
release()
Convenience method, same as setPressed(0).void
setColorProperty
(String key, Color c) void
Add a mark on the note using the specified color.void
setPressed
(int v, Color pressedKeyColor) Set the key in the "pressed" state.void
setPressedWhiteKeyColor
(Color pressedKeyColor) Set the pressed color for a white key.void
Set this key released (velocity is set to 0).void
setSelected
(boolean b) void
setShowVelocityColor
(boolean showVelocityColor) Set if pressed color is adjusted depending on note velocity.final void
setSizeRelativeToWhiteKeyRef
(int wwRef, int whRef) Change the size of the key from a reference rectangular white key size.void
Show an horizontal text on the note.toString()
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, 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, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUI, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, 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, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI
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, getAccessibleContext, 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
-
Field Details
-
WW
public static final int WWStandard White Key Width in DOWN orientation.- See Also:
-
WH
public static final int WHStandard White Key Height in DOWN orientation.- See Also:
-
WW_MIN
public static final int WW_MINMinimum White Key Width in DOWN orientation..- See Also:
-
WH_MIN
public static final int WH_MINMinimum 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
-
setColorProperty
-
getText
-
setText
Show an horizontal text on the note.- Parameters:
text
- Shorter is better! If null no text is shown.
-
isWhiteKey
public final boolean isWhiteKey() -
setMarked
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
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
Draw the key.- Overrides:
paint
in classJComponent
- Parameters:
g
- The Graphics context in which to draw.
-
getAdaptedColor
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 classJComponent
- 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
- Returns:
- Based on getPressedWhiteKeyColor()
-
getPressedWhiteKeyColor
- Returns:
- the pressedWhiteKeyColor
-
getMarkedColor
- Returns:
-
setPressedWhiteKeyColor
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
-