Class BlurLayerUI
- All Implemented Interfaces:
Serializable
A custom LayerUI that can apply a blur effect to a JPanel
and display an optional text message over it. While blurred,
the panel will not respond to mouse or keyboard events.
USAGE: Create a JLayer with an instance of this BlurLayerUI, wrapping the JPanel you want to blur. Call setBlurred(true) to enable the blur effect, and setBlurred(false) to disable it. Alternatively, use blurOut() and blurIn() for animated transitions.
JPanel contentPanel = ...;
BlurLayerUI blurLayerUI = new BlurLayerUI();
JLayer<JPanel> layeredPanel = new JLayer<>(contentPanel, blurLayerUI);
containerPanel.add(layeredPanel);
// Set your blur options, or live with the defaults:
blurLayerUI.setOverlayText("Please wait...");
blurLayerUI.setBlurIntensity(BlurLayerUI.BlurIntensity.STRONG);
// Then, in a button handler or whatever:
blurLayerUI.setBlurred(true); // to enable blur instantly
blurLayerUI.setBlurred(false); // to disable blur instantly
// Or use animated blur:
blurLayerUI.blurOut(() -> {
// Callback after blur completes
});
blurLayerUI.blurIn(() -> {
// Callback after unblur completes
});
Side note: To give credit where credit is due, claude.ai wrote the blur algorithm. I've looked at that code and I have no idea how it works :-/
- Since:
- swing-extras 2.7
- Author:
- scorbo2 with claude.ai
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumBlur intensity presets controlling the blur kernel size. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final AnimationDurationstatic final AnimationSpeedstatic final Colorstatic final BlurLayerUI.BlurIntensitystatic final Colorstatic final intstatic final intstatic final int -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new BlurLayerUI instance with no blur applied initially and no overlay text. -
Method Summary
Modifier and TypeMethodDescriptionvoidPerforms a "blur in" animation, gradually removing the blur effect.voidPerforms a "blur out" animation, gradually applying the blur effect.Gets the configured animation duration.Gets the configured animation speed.Returns the current blur intensity setting.Returns the color used for the blur overlay.Returns the text that will be overlaid on the blurred panel.Returns the overlay text color.intReturns the overlay text font point size.voidbooleanReturns whether the panel is currently blurred.voidpaint(Graphics g, JComponent c) protected voidprocessKeyEvent(KeyEvent e, JLayer<? extends JPanel> l) protected voidprocessMouseEvent(MouseEvent e, JLayer<? extends JPanel> l) protected voidprocessMouseMotionEvent(MouseEvent e, JLayer<? extends JPanel> l) setAnimationDuration(AnimationDuration animationDuration) Sets the animation duration for blur transitions.setAnimationSpeed(AnimationSpeed animationSpeed) Sets the animation speed for blur transitions.setBlurIntensity(BlurLayerUI.BlurIntensity intensity) Sets the blur intensity preset.voidsetBlurOverlayColor(Color blurOverlayColor) Sets the color used for the blur overlay.voidsetBlurred(boolean blurred) Sets whether the panel should be blurred instantly (without animation).setOverlayText(String overlayText) Sets optional text to overlay on top of the panel when it is blurred.voidsetOverlayTextColor(Color overlayTextColor) Sets the overlay text color.voidsetOverlayTextSize(int overlayTextSize) Sets the overlay text font point size.voidMethods inherited from class javax.swing.plaf.LayerUI
addPropertyChangeListener, addPropertyChangeListener, applyPropertyChange, doLayout, eventDispatched, firePropertyChange, getBaseline, getBaselineResizeBehavior, getMaximumSize, getMinimumSize, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, imageUpdate, paintImmediately, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, removePropertyChangeListener, removePropertyChangeListener, updateUIMethods inherited from class javax.swing.plaf.ComponentUI
contains, createUI, getAccessibleChild, getAccessibleChildrenCount, update
-
Field Details
-
DEFAULT_INTENSITY
-
DEFAULT_BLUR_OVERLAY_COLOR
-
DEFAULT_TEXT_SIZE
public static final int DEFAULT_TEXT_SIZE- See Also:
-
TEXT_MINIMUM_SIZE
public static final int TEXT_MINIMUM_SIZE- See Also:
-
TEXT_MAXIMUM_SIZE
public static final int TEXT_MAXIMUM_SIZE- See Also:
-
DEFAULT_TEXT_COLOR
-
DEFAULT_ANIMATION_DURATION
-
DEFAULT_ANIMATION_SPEED
-
-
Constructor Details
-
BlurLayerUI
public BlurLayerUI()Creates a new BlurLayerUI instance with no blur applied initially and no overlay text.
-
-
Method Details
-
setBlurred
public void setBlurred(boolean blurred) Sets whether the panel should be blurred instantly (without animation). For animated blur transitions, use blurOut() and blurIn() instead. If an animation is currently running, it will be stopped and cleaned up. -
isBlurred
public boolean isBlurred()Returns whether the panel is currently blurred. -
getAnimationDuration
Gets the configured animation duration. -
setAnimationDuration
Sets the animation duration for blur transitions.- Parameters:
animationDuration- the animation duration to use; must not benull- Returns:
- this
BlurLayerUIinstance for chaining - Throws:
IllegalArgumentException- ifanimationDurationisnull
-
getAnimationSpeed
Gets the configured animation speed. -
setAnimationSpeed
Sets the animation speed for blur transitions.- Parameters:
animationSpeed- the animation speed to use; must not benull- Returns:
- this
BlurLayerUIinstance for method chaining - Throws:
IllegalArgumentException- ifanimationSpeedisnull
-
getBlurOverlayColor
Returns the color used for the blur overlay. -
setBlurOverlayColor
Sets the color used for the blur overlay. The alpha value of the supplied color is ignored and a default semi-transparent alpha is used instead. -
getOverlayText
Returns the text that will be overlaid on the blurred panel. -
setOverlayText
Sets optional text to overlay on top of the panel when it is blurred. If null, no text will be overlaid. -
getOverlayTextSize
public int getOverlayTextSize()Returns the overlay text font point size. -
setOverlayTextSize
public void setOverlayTextSize(int overlayTextSize) Sets the overlay text font point size. The input size must be any value between TEXT_MINIMUM_SIZE and TEXT_MAXIMUM_SIZE, otherwise an IllegalArgumentException will be thrown. -
getOverlayTextColor
Returns the overlay text color. -
setOverlayTextColor
Sets the overlay text color. -
getBlurIntensity
Returns the current blur intensity setting. -
setBlurIntensity
Sets the blur intensity preset. -
blurOut
Performs a "blur out" animation, gradually applying the blur effect. Optionally, you can provide a Runnable that will be executed when the blur animation completes.- Parameters:
onComplete- An optional Runnable to invoke when the blur completes (may be null).
-
blurIn
Performs a "blur in" animation, gradually removing the blur effect. Optionally, you can provide a Runnable that will be executed when the blur removal animation completes.- Parameters:
onComplete- An optional Runnable to invoke when the unblur completes (may be null).
-
paint
-
installUI
-
uninstallUI
- Overrides:
uninstallUIin classLayerUI<JPanel>
-
processMouseEvent
- Overrides:
processMouseEventin classLayerUI<JPanel>
-
processMouseMotionEvent
- Overrides:
processMouseMotionEventin classLayerUI<JPanel>
-
processKeyEvent
- Overrides:
processKeyEventin classLayerUI<JPanel>
-