Class BlurLayerUI

java.lang.Object
javax.swing.plaf.ComponentUI
javax.swing.plaf.LayerUI<JPanel>
ca.corbett.extras.image.animation.BlurLayerUI
All Implemented Interfaces:
Serializable

public class BlurLayerUI extends LayerUI<JPanel>
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:
  • Field Details

    • DEFAULT_INTENSITY

      public static final BlurLayerUI.BlurIntensity DEFAULT_INTENSITY
    • DEFAULT_BLUR_OVERLAY_COLOR

      public static final Color 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

      public static final Color DEFAULT_TEXT_COLOR
    • DEFAULT_ANIMATION_DURATION

      public static final AnimationDuration DEFAULT_ANIMATION_DURATION
    • DEFAULT_ANIMATION_SPEED

      public static final AnimationSpeed 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

      public AnimationDuration getAnimationDuration()
      Gets the configured animation duration.
    • setAnimationDuration

      public BlurLayerUI setAnimationDuration(AnimationDuration animationDuration)
      Sets the animation duration for blur transitions.
      Parameters:
      animationDuration - the animation duration to use; must not be null
      Returns:
      this BlurLayerUI instance for chaining
      Throws:
      IllegalArgumentException - if animationDuration is null
    • getAnimationSpeed

      public AnimationSpeed getAnimationSpeed()
      Gets the configured animation speed.
    • setAnimationSpeed

      public BlurLayerUI setAnimationSpeed(AnimationSpeed animationSpeed)
      Sets the animation speed for blur transitions.
      Parameters:
      animationSpeed - the animation speed to use; must not be null
      Returns:
      this BlurLayerUI instance for method chaining
      Throws:
      IllegalArgumentException - if animationSpeed is null
    • getBlurOverlayColor

      public Color getBlurOverlayColor()
      Returns the color used for the blur overlay.
    • setBlurOverlayColor

      public void setBlurOverlayColor(Color blurOverlayColor)
      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

      public String getOverlayText()
      Returns the text that will be overlaid on the blurred panel.
    • setOverlayText

      public BlurLayerUI setOverlayText(String overlayText)
      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

      public Color getOverlayTextColor()
      Returns the overlay text color.
    • setOverlayTextColor

      public void setOverlayTextColor(Color overlayTextColor)
      Sets the overlay text color.
    • getBlurIntensity

      public BlurLayerUI.BlurIntensity getBlurIntensity()
      Returns the current blur intensity setting.
    • setBlurIntensity

      public BlurLayerUI setBlurIntensity(BlurLayerUI.BlurIntensity intensity)
      Sets the blur intensity preset.
    • blurOut

      public void blurOut(Runnable onComplete)
      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

      public void blurIn(Runnable onComplete)
      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

      public void paint(Graphics g, JComponent c)
      Overrides:
      paint in class LayerUI<JPanel>
    • installUI

      public void installUI(JComponent c)
      Overrides:
      installUI in class LayerUI<JPanel>
    • uninstallUI

      public void uninstallUI(JComponent c)
      Overrides:
      uninstallUI in class LayerUI<JPanel>
    • processMouseEvent

      protected void processMouseEvent(MouseEvent e, JLayer<? extends JPanel> l)
      Overrides:
      processMouseEvent in class LayerUI<JPanel>
    • processMouseMotionEvent

      protected void processMouseMotionEvent(MouseEvent e, JLayer<? extends JPanel> l)
      Overrides:
      processMouseMotionEvent in class LayerUI<JPanel>
    • processKeyEvent

      protected void processKeyEvent(KeyEvent e, JLayer<? extends JPanel> l)
      Overrides:
      processKeyEvent in class LayerUI<JPanel>