org.jdesktop.jxlayer.plaf
Class AbstractBufferedLayerUI<V extends javax.swing.JComponent>

java.lang.Object
  extended by javax.swing.plaf.ComponentUI
      extended by org.jdesktop.jxlayer.plaf.LayerUI<V>
          extended by org.jdesktop.jxlayer.plaf.AbstractLayerUI<V>
              extended by org.jdesktop.jxlayer.plaf.AbstractBufferedLayerUI<V>
All Implemented Interfaces:
java.beans.PropertyChangeListener, java.io.Serializable, java.util.EventListener
Direct Known Subclasses:
BufferedLayerUI, DebugRepaintingUI, LockableUI, SpotLightUI

public class AbstractBufferedLayerUI<V extends javax.swing.JComponent>
extends AbstractLayerUI<V>
implements java.beans.PropertyChangeListener

The AbstractBufferedLayerUI paitns the JXLayer to the BufferedImage and then paints this image to the Graphics passed to its paint method.

The main feature provided by AbstractBufferedLayerUI is the ability to apply various LayerEffects to its content,
the most popular effect is the BufferedImageOpEffect which uses BufferedImageOp to filter the buffer

This class introduces the isIncrementalUpdate(JXLayer) method.

If it returns false and AbstractLayerUI.isDirty() returns false and the cached image exists and matches the size of painted JXLayer then the existing image will be used during the painting. It helps to skip unnecessary painting and save a lot of time, especially if BufferedImageOpEffects are used. If isIncrementalUpdate(JXLayer) returns true the cache image will be updated on every painting.

For custom painting, override AbstractLayerUI.paintLayer(Graphics2D,JXLayer) as usual.

If you want to apply Effects, override getLayerEffects(JXLayer) methods or use more flexible BufferedLayerUI.

Note: The AbstractBufferedLayerUI is not shareable and can be set to single JXLayer instance. The current JXLayer can be obtained with getLayer() method

See Also:
LayerEffect, BufferedImageOpEffect, BufferedLayerUI, isIncrementalUpdate(JXLayer), Serialized Form

Constructor Summary
AbstractBufferedLayerUI()
           
 
Method Summary
protected  void applyLayerEffects(JXLayer<? extends V> l, java.awt.Shape clip)
          Iterates through the current array of LayerEffects and applies each enabled one to the buffer.
protected  java.awt.image.BufferedImage createBuffer(int width, int height)
          Creates the BufferedImage to be used as the cached image.
protected  java.awt.image.BufferedImage getBuffer()
          Returns the current cached image.
 JXLayer<? extends V> getLayer()
          The JXLayer this AbstractBufferedLayerUI is set to.
protected  LayerEffect[] getLayerEffects(JXLayer<? extends V> l)
          Returns the array of LayerEffect to be used during painting of this JXLayer, the default implementation returns constant empty array.
 void installUI(javax.swing.JComponent c)
          Configures the JXLayer this LayerUI is set to.

This implementation saves the passed JXLayer instance and checks that it set to one layer only

protected  boolean isBufferContentValid(JXLayer<? extends V> l, java.awt.Shape clip)
          Defines if the cached image has the valid content for the current painting painting operation and there is no need to repaint it.
protected  boolean isBufferFormatValid(JXLayer<? extends V> l)
          Defines if the cached image has the valid format for the current painting painting operation and there is no need to recreate it.
protected  boolean isIncrementalUpdate(JXLayer<? extends V> l)
          If this method returns false and AbstractLayerUI.isDirty returns false and the cached image exists and matches the size of painted JXLayer then the existing image will be used during the painting.
 void paint(java.awt.Graphics g, javax.swing.JComponent c)
          Paints the specified component. Subclasses should override this method and use the specified Graphics object to render the content of the component.

Note: It is rarely necessary to override this method, for custom painting override AbstractLayerUI.paintLayer(Graphics2D,JXLayer) instead

This method configures the passed Graphics with help of the AbstractLayerUI.configureGraphics(Graphics2D,JXLayer) method, then calls paintLayer(Graphics2D,JXLayer) and resets the "dirty bit" at the end.

This method paints the paitns the JXLayer to the BufferedImage and then paints this image to the passed Graphics.

 void propertyChange(java.beans.PropertyChangeEvent evt)
          This method is public as an implementation side effect.
protected  void setBuffer(java.awt.image.BufferedImage buffer)
          Sets the current cached image.
 void uninstallUI(javax.swing.JComponent c)
          
 void updateUI(JXLayer<? extends V> l)
          Mark AbstractBufferedLayerUI as dirty if the LookAndFeel was changed
 
Methods inherited from class org.jdesktop.jxlayer.plaf.AbstractLayerUI
configureGraphics, eventDispatched, getClip, getComposite, getLayerEventMask, getRenderingHints, getTransform, handlePropertyChangeEvent, isDirty, paintLayer, processFocusEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, setDirty
 
Methods inherited from class org.jdesktop.jxlayer.plaf.LayerUI
addPropertyChangeListener, addPropertyChangeListener, firePropertyChange, getPreferredScrollableViewportSize, getPropertyChangeListeners, getPropertyChangeListeners, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getScrollableUnitIncrement, removePropertyChangeListener, removePropertyChangeListener
 
Methods inherited from class javax.swing.plaf.ComponentUI
contains, createUI, getAccessibleChild, getAccessibleChildrenCount, getMaximumSize, getMinimumSize, getPreferredSize, update
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractBufferedLayerUI

public AbstractBufferedLayerUI()
Method Detail

installUI

public void installUI(javax.swing.JComponent c)
Configures the JXLayer this LayerUI is set to. The default implementation registers the LayerUI as a property change listener for the passed JXLayer

This implementation saves the passed JXLayer instance and checks that it set to one layer only

Overrides:
installUI in class AbstractLayerUI<V extends javax.swing.JComponent>
Parameters:
c - the JXLayer where this UI delegate is being installed
Throws:
java.lang.IllegalStateException - if this BufferedLayerUI is set to multiple JXLayers
See Also:
uninstallUI(JComponent), getLayer()

uninstallUI

public void uninstallUI(javax.swing.JComponent c)

Overrides:
uninstallUI in class AbstractLayerUI<V extends javax.swing.JComponent>

propertyChange

public void propertyChange(java.beans.PropertyChangeEvent evt)
This method is public as an implementation side effect. AbstractBufferedLayerUI listens property changes of its JXLayer and marks itself as dirty if the JXLayer's view component has been changed.

Specified by:
propertyChange in interface java.beans.PropertyChangeListener
Parameters:
evt - the PropertyChangeEvent
See Also:
JXLayer.setView(Component), AbstractLayerUI.setDirty(boolean)

updateUI

public void updateUI(JXLayer<? extends V> l)
Mark AbstractBufferedLayerUI as dirty if the LookAndFeel was changed

Overrides:
updateUI in class LayerUI<V extends javax.swing.JComponent>
Parameters:
l - the JXLayer this AbstractBufferedLayerUI is set to

getLayer

public JXLayer<? extends V> getLayer()
The JXLayer this AbstractBufferedLayerUI is set to.

Returns:
the JXLayer this AbstractBufferedLayerUI is set to

getBuffer

protected java.awt.image.BufferedImage getBuffer()
Returns the current cached image.

The implementation of this LayerUI may use SoftReference to keep this image, so this method may return null at any time.

However it is guaranteed that is safe to call this method inside AbstractLayerUI.paintLayer(Graphics2D,JXLayer) method, because a strong reference to the buffer is kept during painting process and you'll get the actual BufferedImage which you are free to use withing paintLayer.

Returns:
the current cached image.
See Also:
setBuffer(BufferedImage)

setBuffer

protected void setBuffer(java.awt.image.BufferedImage buffer)
Sets the current cached image.

Parameters:
buffer - the BufferedImage to be used as the cache
See Also:
getBuffer()

getLayerEffects

protected LayerEffect[] getLayerEffects(JXLayer<? extends V> l)
Returns the array of LayerEffect to be used during painting of this JXLayer, the default implementation returns constant empty array.

Parameters:
l - the JXLayer being painted
Returns:
the array of LayerEffect to be used during painting of the JXLayer

isIncrementalUpdate

protected boolean isIncrementalUpdate(JXLayer<? extends V> l)
If this method returns false and AbstractLayerUI.isDirty returns false and the cached image exists and matches the size of painted JXLayer then the existing image will be used during the painting.

It helps to skip unnecessary painting and save a lot of time, especially if BufferedImageOpEffects are used.
If this method returns true the cache image will be updated on every painting.

The default implementation returns true

Parameters:
l - the JXLayer being painted
Returns:
true if the cache image should be updated on every painting, otherwise returns false
See Also:
getBuffer()

paint

public void paint(java.awt.Graphics g,
                  javax.swing.JComponent c)
Paints the specified component. Subclasses should override this method and use the specified Graphics object to render the content of the component.

Note: It is rarely necessary to override this method, for custom painting override AbstractLayerUI.paintLayer(Graphics2D,JXLayer) instead

This method configures the passed Graphics with help of the AbstractLayerUI.configureGraphics(Graphics2D,JXLayer) method, then calls paintLayer(Graphics2D,JXLayer) and resets the "dirty bit" at the end.

This method paints the paitns the JXLayer to the BufferedImage and then paints this image to the passed Graphics.

It also manages the state of the existing cached image and applies the existing LayerEffects to the image.

Overrides:
paint in class AbstractLayerUI<V extends javax.swing.JComponent>
Parameters:
g - the Graphics context in which to paint;
c - the component being painted; it can be safely cast to the JXLayer<V>
See Also:
AbstractLayerUI.paintLayer(Graphics2D,JXLayer), isBufferFormatValid(JXLayer), isBufferContentValid(JXLayer,Shape), getLayerEffects(JXLayer)

isBufferFormatValid

protected boolean isBufferFormatValid(JXLayer<? extends V> l)
Defines if the cached image has the valid format for the current painting painting operation and there is no need to recreate it.

The default implementation returns true if the cached image is not null and its size matches the size of the JXLayer being painted, otherwise it returns true.

Parameters:
l - the JXLayer being painted
Returns:
true if the format of existing cache image is valid, otherwise returns false
See Also:
getBuffer()

isBufferContentValid

protected boolean isBufferContentValid(JXLayer<? extends V> l,
                                       java.awt.Shape clip)
Defines if the cached image has the valid content for the current painting painting operation and there is no need to repaint it.

The default implementation returns true if this AbstractBufferedLayerUI hasn't been marked as dirty and incremental update is disabled.

Parameters:
l - the JXLayer being painted
clip - the current clipping shape
Returns:
true if the content of existing cache image is valid, otherwise returns false
See Also:
AbstractLayerUI.isDirty(), isIncrementalUpdate(JXLayer)

createBuffer

protected java.awt.image.BufferedImage createBuffer(int width,
                                                    int height)
Creates the BufferedImage to be used as the cached image. This method must never return null.

Parameters:
width - the width of the image
height - the height of the image
Returns:
an off-screen BufferedImage, which can be used for double buffering.

applyLayerEffects

protected void applyLayerEffects(JXLayer<? extends V> l,
                                 java.awt.Shape clip)
Iterates through the current array of LayerEffects and applies each enabled one to the buffer.

Parameters:
l - the JXLayer being painted
clip - the current clipping shape
See Also:
getLayerEffects(JXLayer), LayerEffect.isEnabled()