Class CompoundPainter<T>
- Type Parameters:
T- an optional configuration parameter
- All Implemented Interfaces:
Painter<T>
public class CompoundPainter<T> extends AbstractPainter<T>
A Painter implementation composed of an array of Painters.
CompoundPainter provides a means for combining several individual
Painters, or groups of them, into one logical unit. Each of the
Painters 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 classes/interfaces inherited from class org.jxmapviewer.painter.AbstractPainter
AbstractPainter.Interpolation -
Constructor Summary
Constructors Constructor Description CompoundPainter()Creates a new instance of CompoundPainterCompoundPainter(java.util.List<? extends Painter<T>> painters)Convenience constructor for creating a CompoundPainter for a list of painters.CompoundPainter(Painter<T>... painters)Convenience constructor for creating a CompoundPainter for an array of painters. -
Method Summary
Modifier and Type Method Description voidaddPainter(Painter<T> painter)Adds a painter to the queue of paintersvoidclearCache()Clears the cache of thisPainter, and all childPainters.voidclearLocalCache()Clears the cache of this painter only, and not of any of the children.protected voidconfigureGraphics(java.awt.Graphics2D g)This method is called by thepaintmethod prior to any drawing operations to configure the drawing surface.protected voiddoPaint(java.awt.Graphics2D g, T component, int width, int height)Subclasses must implement this method and perform custom painting operations here.java.util.Collection<Painter<T>>getPainters()Gets the array of painters used by this CompoundPainterjava.awt.geom.AffineTransformgetTransform()Gets the current transform applied to all painters in this CompoundPainter.booleanisCheckingDirtyChildPainters()Used byisDirty()to check if the childPainters should be checked for theirdirtyflag as part of processing.
Default value is:true
This should be set tofalseif the cacheable state of the childPainters are different from each other.booleanisClipPreserved()Indicates if the clip produced by any painter is left set once it finishes painting.protected booleanisDirty()ThisCompoundPainteris dirty if it, or (optionally) any of its children, are dirty.voidremovePainter(Painter<T> painter)Removes a painter from the queue of paintersvoidsetCheckingDirtyChildPainters(boolean b)Set the flag used byisDirty()to check if the childPainters should be checked for theirdirtyflag as part of processing.voidsetClipPreserved(boolean shouldRestoreState)Sets if the clip should be preserved.protected voidsetDirty(boolean d)Sets the dirty bit.voidsetPainters(java.util.List<? extends Painter<T>> painters)Sets the array of Painters to use.voidsetPainters(Painter<T>... painters)Sets the array of Painters to use.voidsetTransform(java.awt.geom.AffineTransform transform)Set a transform to be applied to all painters contained in this CompoundPainterprotected booleanshouldUseCache()Returns true if the painter should use caching.protected voidvalidate(T object)Iterates over all childPainters and gives them a chance to validate themselves.Methods inherited from class org.jxmapviewer.painter.AbstractPainter
getFilters, getInterpolation, isAntialiasing, isCacheable, isVisible, paint, setAntialiasing, setCacheable, setFilters, setInterpolation, setVisibleMethods inherited from class org.jxmapviewer.beans.AbstractBean
addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, addVetoableChangeListener, clone, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, fireVetoableChange, getPropertyChangeListeners, getPropertyChangeListeners, getVetoableChangeListeners, getVetoableChangeListeners, hasPropertyChangeListeners, hasVetoableChangeListeners, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, removeVetoableChangeListener
-
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
-
CompoundPainter
Convenience constructor for creating a CompoundPainter for a list of painters. A defensive copy of the given array is made, so that future modification to the list 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
-
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
-
addPainter
Adds a painter to the queue of painters- Parameters:
painter- the painter that is added
-
removePainter
Removes a painter from the queue of painters- Parameters:
painter- the painter that is added
-
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(boolean)
-
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:
isClipPreserved()
-
getTransform
public java.awt.geom.AffineTransform getTransform()Gets the current transform applied to all painters in this CompoundPainter. May be null.- Returns:
- the current AffineTransform
-
setTransform
public void setTransform(java.awt.geom.AffineTransform transform)Set a transform to be applied to all painters contained in this CompoundPainter- Parameters:
transform- a new AffineTransform
-
validate
Iterates over all child
Painters and gives them a chance to validate themselves. If any of the child painters are dirty, then thisCompoundPaintermarks itself as dirty.- Overrides:
validatein classAbstractPainter<T>- Parameters:
object- the object to validate
-
isCheckingDirtyChildPainters
public boolean isCheckingDirtyChildPainters()Used byisDirty()to check if the childPainters should be checked for theirdirtyflag as part of processing.
Default value is:true
This should be set tofalseif the cacheable state of the childPainters are different from each other. This will allow the cacheable ==truePainters to keep their cached image during regular repaints. In this case, client code should callclearCache()manually when the cacheablePainters should be updated.- Returns:
- the dirty check flag
- See Also:
isDirty()
-
setCheckingDirtyChildPainters
public void setCheckingDirtyChildPainters(boolean b)Set the flag used byisDirty()to check if the childPainters should be checked for theirdirtyflag as part of processing.- Parameters:
b- the dirty check flag- See Also:
isCheckingDirtyChildPainters(),isDirty()
-
isDirty
protected boolean isDirty()This
CompoundPainteris dirty if it, or (optionally) any of its children, are dirty. If the super implementation returnstrue, we returntrue. Otherwise, ifisCheckingDirtyChildPainters()istrue, we iterate over all childPainters and query them to see if they are dirty. If so, thentrueis returned. Otherwise, we returnfalse.- Overrides:
isDirtyin classAbstractPainter<T>- Returns:
- true if the painter state has changed and the painter needs to be repainted.
- See Also:
isCheckingDirtyChildPainters()
-
setDirty
protected void setDirty(boolean d)Description copied from class:AbstractPainterSets the dirty bit. If true, then the painter is considered dirty, and the cache will be cleared. This property is bound.- Overrides:
setDirtyin classAbstractPainter<T>- Parameters:
d- whether thisPainteris 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- Overrides:
clearCachein classAbstractPainter<T>
-
clearLocalCache
public void clearLocalCache()Clears the cache of this painter only, and not of any of the children.
-
doPaint
Description copied from class:AbstractPainterSubclasses must implement this method and perform custom painting operations here.- Specified by:
doPaintin classAbstractPainter<T>- Parameters:
g- The Graphics2D object in which to paintcomponent- an optional configuration parameterwidth- the widthheight- the height
-
configureGraphics
protected void configureGraphics(java.awt.Graphics2D g)Description copied from class:AbstractPainterThis method is called by the
paintmethod 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:
configureGraphicsin classAbstractPainter<T>- Parameters:
g- the graphics surface to configure. This will never be null.- See Also:
AbstractPainter.paint(Graphics2D, Object, int, int)
-
shouldUseCache
protected boolean shouldUseCache()Description copied from class:AbstractPainterReturns true if the painter should use caching. This method allows subclasses to specify the heuristics regarding whether to cache or not. If a
Painterhas intelligent rules regarding painting times, and can more accurately indicate whether it should be cached, it could implement that logic in this method.- Overrides:
shouldUseCachein classAbstractPainter<T>- Returns:
- whether or not a cache should be used
-