Class CompoundPainter<T>
- All Implemented Interfaces:
Painter<T>
- Direct Known Subclasses:
AlphaPainter
,URLPainter
A Painter
implementation composed of an array of Painter
s.
CompoundPainter
provides a means for combining several individual
Painter
s, or groups of them, into one logical unit. Each of the
Painter
s are executed in order. BufferedImageOp filter effects can
be applied to them together as a whole. The entire set of painting operations
may be cached together.
For example, if I want to create a CompoundPainter that started with a blue background, had pinstripes on it running at a 45 degree angle, and those pinstripes appeared to "fade in" from left to right, I would write the following:
Color blue = new Color(0x417DDD);
Color translucent = new Color(blue.getRed(), blue.getGreen(), blue.getBlue(), 0);
panel.setBackground(blue);
panel.setForeground(Color.LIGHT_GRAY);
GradientPaint blueToTranslucent = new GradientPaint(
new Point2D.Double(.4, 0),
blue,
new Point2D.Double(1, 0),
translucent);
MattePainter veil = new MattePainter(blueToTranslucent);
veil.setPaintStretched(true);
Painter pinstripes = new PinstripePainter(45);
Painter backgroundPainter = new RectanglePainter(this.getBackground(), null);
Painter p = new CompoundPainter(backgroundPainter, pinstripes, veil);
panel.setBackgroundPainter(p);
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class org.jdesktop.swingx.painter.AbstractPainter
AbstractPainter.Interpolation
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
private boolean
private boolean
private CompoundPainter.Handler
private Painter[]
private AffineTransform
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance of CompoundPainterCompoundPainter
(Painter... painters) Convenience constructor for creating a CompoundPainter for an array of painters. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Clears the cache of thisPainter
, and all childPainters
.void
Clears the cache of this painter only, and not of any of the children.protected void
This method is called by thepaint
method prior to any drawing operations to configure the drawing surface.protected void
doPaint
(Graphics2D g, T component, int width, int height) Subclasses must implement this method and perform custom painting operations here.final Painter[]
Gets the array of painters used by this CompoundPainterGets the current transform applied to all painters in this CompoundPainter.boolean
Used byisDirty()
to check if the childPainter
s should be checked for theirdirty
flag as part of processing.
Default value is:true
This should be set to false if the cacheable state of the childPainter
s are different from each other.boolean
Indicates if the clip produced by any painter is left set once it finishes painting.protected boolean
isDirty()
Ye olde dirty bit.void
setCheckingDirtyChildPainters
(boolean b) Set the flag used byisDirty()
to check if the childPainter
s should be checked for theirdirty
flag as part of processing.void
setClipPreserved
(boolean shouldRestoreState) Sets if the clip should be preserved.protected void
setDirty
(boolean d) Sets the dirty bit.void
setPainters
(Painter... painters) Sets the array of Painters to use.void
setTransform
(AffineTransform transform) Set a transform to be applied to all painters contained in this CompoundPainterprotected boolean
Returns true if the painter should use caching.protected void
Iterates over all childPainter
s and gives them a chance to validate themselves.Methods inherited from class org.jdesktop.swingx.painter.AbstractPainter
getFilters, getInterpolation, isAntialiasing, isCacheable, isCacheCleared, isInPaintContext, isVisible, paint, setAntialiasing, setCacheable, setFilters, setInPaintContext, setInterpolation, setVisible
Methods inherited from class org.jdesktop.beans.AbstractBean
addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, addVetoableChangeListener, clone, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, fireVetoableChange, getPropertyChangeListeners, getPropertyChangeListeners, getVetoableChangeListeners, getVetoableChangeListeners, hasPropertyChangeListeners, hasVetoableChangeListeners, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, removeVetoableChangeListener
-
Field Details
-
handler
-
painters
-
transform
-
clipPreserved
private boolean clipPreserved -
checkForDirtyChildPainters
private boolean checkForDirtyChildPainters -
clearLocalCacheOnly
private boolean clearLocalCacheOnly
-
-
Constructor Details
-
CompoundPainter
public CompoundPainter()Creates a new instance of CompoundPainter -
CompoundPainter
Convenience constructor for creating a CompoundPainter for an array of painters. A defensive copy of the given array is made, so that future modification to the array does not result in changes to the CompoundPainter.- Parameters:
painters
- array of painters, which will be painted in order
-
-
Method Details
-
setPainters
Sets the array of Painters to use. These painters will be executed in order. A null value will be treated as an empty array. To prevent unexpected behavior all values in provided array are copied to internally held array. Any changes to the original array will not be reflected.- Parameters:
painters
- array of painters, which will be painted in order
-
getPainters
Gets the array of painters used by this CompoundPainter- Returns:
- a defensive copy of the painters used by this CompoundPainter. This will never be null.
-
isClipPreserved
public boolean isClipPreserved()Indicates if the clip produced by any painter is left set once it finishes painting. Normally the clip will be reset between each painter. Setting clipPreserved to true can be used to let one painter mask other painters that come after it.- Returns:
- if the clip should be preserved
- See Also:
-
setClipPreserved
public void setClipPreserved(boolean shouldRestoreState) Sets if the clip should be preserved. Normally the clip will be reset between each painter. Setting clipPreserved to true can be used to let one painter mask other painters that come after it.- Parameters:
shouldRestoreState
- new value of the clipPreserved property- See Also:
-
getTransform
Gets the current transform applied to all painters in this CompoundPainter. May be null.- Returns:
- the current AffineTransform
-
setTransform
Set a transform to be applied to all painters contained in this CompoundPainter- Parameters:
transform
- a new AffineTransform
-
validate
Iterates over all child
Painter
s and gives them a chance to validate themselves. If any of the child painters are dirty, then thisCompoundPainter
marks itself as dirty.Called to allow
Painter
subclasses a chance to see if any state in the given object has changed from the last paint operation. If it has, then thePainter
has a chance to mark itself as dirty, thus causing a repaint, even if cached.- Overrides:
validate
in classAbstractPainter<T>
- Parameters:
object
-
-
isCheckingDirtyChildPainters
public boolean isCheckingDirtyChildPainters()Used byisDirty()
to check if the childPainter
s should be checked for theirdirty
flag as part of processing.
Default value is:true
This should be set to false if the cacheable state of the childPainter
s are different from each other. This will allow the cacheable ==true
Painter
s to keep their cached image during regular repaints. In this case, client code should callclearCache()
manually when the cacheablePainter
s should be updated.- See Also:
-
setCheckingDirtyChildPainters
public void setCheckingDirtyChildPainters(boolean b) Set the flag used byisDirty()
to check if the childPainter
s should be checked for theirdirty
flag as part of processing.- See Also:
-
isDirty
protected boolean isDirty()Ye olde dirty bit. If true, then the painter is considered dirty and in need of being repainted. This is a bound property.- Overrides:
isDirty
in classAbstractPainter<T>
- Returns:
- true if the painter state has changed and the painter needs to be repainted.
- See Also:
-
setDirty
protected void setDirty(boolean d) Sets the dirty bit. If true, then the painter is considered dirty, and the cache will be cleared. This property is bound.- Overrides:
setDirty
in classAbstractPainter<T>
- Parameters:
d
- whether thisPainter
is dirty.
-
clearCache
public void clearCache()Clears the cache of this
Painter
, and all childPainters
. This is done to ensure that resources are collected, even if clearCache is called by some framework or other code that doesn't realize this is a CompoundPainter.Call #clearLocalCache if you only want to clear the cache of this
CompoundPainter
Call this method to clear the cacheable. This may be called whether there is a cacheable being used or not. If cleared, on the next call to
paint
, the painting routines will be called.SubclassesIf overridden in subclasses, you must call super.clearCache, or physical resources (such as an Image) may leak.
- Overrides:
clearCache
in classAbstractPainter<T>
-
clearLocalCache
public void clearLocalCache()Clears the cache of this painter only, and not of any of the children.
-
doPaint
Subclasses must implement this method and perform custom painting operations here.- Specified by:
doPaint
in classAbstractPainter<T>
- Parameters:
g
- The Graphics2D object in which to paintcomponent
-width
-height
-
-
configureGraphics
This method is called by the
paint
method prior to any drawing operations to configure the drawing surface. The default implementation sets the rendering hints that have been specified for thisAbstractPainter
.This method can be overridden by subclasses to modify the drawing surface before any painting happens.
- Overrides:
configureGraphics
in classAbstractPainter<T>
- Parameters:
g
- the graphics surface to configure. This will never be null.- See Also:
-
shouldUseCache
protected boolean shouldUseCache()Returns true if the painter should use caching. This method allows subclasses to specify the heuristics regarding whether to cache or not. If a
Painter
has intelligent rules regarding painting times, and can more accurately indicate whether it should be cached, it could implement that logic in this method.- Overrides:
shouldUseCache
in classAbstractPainter<T>
- Returns:
- whether or not a cache should be used
-