Class AbstractPainter<T>
- java.lang.Object
-
- org.jdesktop.beans.AbstractBean
-
- org.jdesktop.swingx.painter.AbstractPainter<T>
-
- All Implemented Interfaces:
Painter<T>
- Direct Known Subclasses:
AbstractLayoutPainter,BusyPainter,CheckerboardPainter,CompoundPainter,GlossPainter,JXMultiSplitPane.DividerPainter,PinstripePainter,TextCrossingPainter
public abstract class AbstractPainter<T> extends AbstractBean implements Painter<T>
A convenient base class from which concrete
Painterimplementations may extend. It extendsAbstractBeanas a convenience for adding property change notification support. In addition,AbstractPainterprovides subclasses with the ability to cacheable painting operations, configure the drawing surface with common settings (such as antialiasing and interpolation), and toggle whether a subclass paints or not via thevisibilityproperty.Subclasses of
AbstractPaintergenerally need only override thedoPaint(Graphics2D, Object, int, int)method. If a subclass requires more control over whether caching is enabled, or for configuring the graphics state, then it may override the appropriate protected methods to interpose its own behavior.For example, here is the doPaint method of a simple
Painterthat paints an opaque rectangle:public void doPaint(Graphics2D g, T obj, int width, int height) { g.setPaint(Color.BLUE); g.fillRect(0, 0, width, height); }
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAbstractPainter.InterpolationAn enum representing the possible interpolation values of Bicubic, Bilinear, and Nearest Neighbor.
-
Field Summary
Fields Modifier and Type Field Description private booleanantialiasingprivate booleancacheableprivate booleancacheClearedprivate java.lang.ref.SoftReference<java.awt.image.BufferedImage>cachedImageThe cached image, if shouldUseCache() returns trueprivate booleandirtyprivate java.awt.image.BufferedImageOp[]filtersprivate booleaninPaintContextprivate AbstractPainter.Interpolationinterpolationprivate booleanvisible
-
Constructor Summary
Constructors Constructor Description AbstractPainter()Creates a new instance of AbstractPainter.AbstractPainter(boolean cacheable)Creates a new instance of AbstractPainter.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidclearCache()Call this method to clear the cacheable.protected voidconfigureGraphics(java.awt.Graphics2D g)This method is called by thepaintmethod prior to any drawing operations to configure the drawing surface.protected abstract voiddoPaint(java.awt.Graphics2D g, T object, int width, int height)Subclasses must implement this method and perform custom painting operations here.java.awt.image.BufferedImageOp[]getFilters()A defensive copy of the Effects to apply to the results of the AbstractPainter's painting operation.AbstractPainter.InterpolationgetInterpolation()Gets the current interpolation setting.booleanisAntialiasing()Returns if antialiasing is turned on or not.booleanisCacheable()Gets whether thisAbstractPaintercan be cached as an image.(package private) booleanisCacheCleared()Only made package private for testing.protected booleanisDirty()Ye olde dirty bit.(package private) booleanisInPaintContext()booleanisVisible()Gets the visible property.voidpaint(java.awt.Graphics2D g, T obj, int width, int height)Renders to the givenGraphics2Dobject.voidsetAntialiasing(boolean value)Sets the antialiasing setting.voidsetCacheable(boolean cacheable)Sets whether thisAbstractPaintercan be cached as an image.protected voidsetDirty(boolean d)Sets the dirty bit.voidsetFilters(java.awt.image.BufferedImageOp... effects)A convenience method for specifying the filters to use based on BufferedImageOps.(package private) voidsetInPaintContext(boolean inPaintContext)voidsetInterpolation(AbstractPainter.Interpolation value)Sets a new value for the interpolation setting.voidsetVisible(boolean visible)Sets the visible property.protected booleanshouldUseCache()Returns true if the painter should use caching.protected voidvalidate(T object)Called to allowPaintersubclasses a chance to see if any state in the given object has changed from the last paint operation.-
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 Detail
-
cachedImage
private transient java.lang.ref.SoftReference<java.awt.image.BufferedImage> cachedImage
The cached image, if shouldUseCache() returns true
-
cacheCleared
private boolean cacheCleared
-
cacheable
private boolean cacheable
-
dirty
private boolean dirty
-
filters
private java.awt.image.BufferedImageOp[] filters
-
antialiasing
private boolean antialiasing
-
interpolation
private AbstractPainter.Interpolation interpolation
-
visible
private boolean visible
-
inPaintContext
private boolean inPaintContext
-
-
Method Detail
-
getFilters
public final java.awt.image.BufferedImageOp[] getFilters()
A defensive copy of the Effects to apply to the results of the AbstractPainter's painting operation. The array may be empty but it will never be null.- Returns:
- the array of filters applied to this painter
-
setFilters
public void setFilters(java.awt.image.BufferedImageOp... effects)
A convenience method for specifying the filters to use based on BufferedImageOps. These will each be individually wrapped by an ImageFilter and then setFilters(Effect... filters) will be called with the resulting array
- Parameters:
effects- the BufferedImageOps to wrap as filters
-
isAntialiasing
public boolean isAntialiasing()
Returns if antialiasing is turned on or not. The default value is true. This is a bound property.- Returns:
- the current antialiasing setting
-
setAntialiasing
public void setAntialiasing(boolean value)
Sets the antialiasing setting. This is a bound property.- Parameters:
value- the new antialiasing setting
-
getInterpolation
public AbstractPainter.Interpolation getInterpolation()
Gets the current interpolation setting. This property determines if interpolation will be used when drawing scaled images. @see java.awt.RenderingHints.KEY_INTERPOLATION.- Returns:
- the current interpolation setting
-
setInterpolation
public void setInterpolation(AbstractPainter.Interpolation value)
Sets a new value for the interpolation setting. This setting determines if interpolation should be used when drawing scaled images. @see java.awt.RenderingHints.KEY_INTERPOLATION.- Parameters:
value- the new interpolation setting
-
isVisible
public boolean isVisible()
Gets the visible property. This controls if the painter should paint itself. It is true by default. Setting visible to false is good when you want to temporarily turn off a painter. An example of this is a painter that you only use when a button is highlighted.- Returns:
- current value of visible property
-
setVisible
public void setVisible(boolean visible)
Sets the visible property. This controls if the painter should paint itself. It is true by default. Setting visible to false is good when you want to temporarily turn off a painter. An example of this is a painter that you only use when a button is highlighted.
- Parameters:
visible- New value of visible property.
-
isCacheable
public boolean isCacheable()
Gets whether this
AbstractPaintercan be cached as an image. If caching is enabled, then it is the responsibility of the developer to invalidate the painter (viaclearCache()) if external state has changed in such a way that the painter is invalidated and needs to be repainted.- Returns:
- whether this is cacheable
-
setCacheable
public void setCacheable(boolean cacheable)
Sets whether this
AbstractPaintercan be cached as an image. If true, this is treated as a hint. That is, a cacheable may or may not be used. TheshouldUseCache()method actually determines whether the cacheable is used. However, if false, then this is treated as an absolute value. That is, no cacheable will be used.If set to false, then #clearCache is called to free system resources.
- Parameters:
cacheable-
-
clearCache
public void clearCache()
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.
-
isCacheCleared
boolean isCacheCleared()
Only made package private for testing. Don't call this method outside of this class! This is NOT a bound property
-
validate
protected void validate(T object)
Called to allow
Paintersubclasses a chance to see if any state in the given object has changed from the last paint operation. If it has, then thePainterhas a chance to mark itself as dirty, thus causing a repaint, even if cached.- Parameters:
object-
-
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.- Returns:
- true if the painter state has changed and the painter needs to be repainted.
-
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.- Parameters:
d- whether thisPainteris dirty.
-
isInPaintContext
boolean isInPaintContext()
-
setInPaintContext
void setInPaintContext(boolean inPaintContext)
-
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
Painterhas intelligent rules regarding painting times, and can more accurately indicate whether it should be cached, it could implement that logic in this method.- Returns:
- whether or not a cache should be used
-
configureGraphics
protected void configureGraphics(java.awt.Graphics2D g)
This 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.
- Parameters:
g- the graphics surface to configure. This will never be null.- See Also:
paint(Graphics2D, Object, int, int)
-
doPaint
protected abstract void doPaint(java.awt.Graphics2D g, T object, int width, int height)Subclasses must implement this method and perform custom painting operations here.- Parameters:
width-height-g- The Graphics2D object in which to paintobject-
-
paint
public final void paint(java.awt.Graphics2D g, T obj, int width, int height)Description copied from interface:PainterRenders to the given
Graphics2Dobject. Implementations of this method may modify state on theGraphics2D, and are not required to restore that state upon completion. In most cases, it is recommended that the caller pass in a scratch graphics object. TheGraphics2Dmust never be null.State on the graphics object may be honored by the
paintmethod, but may not be. For instance, setting the antialiasing rendering hint on the graphics may or may not be respected by thePainterimplementation.The supplied object parameter acts as an optional configuration argument. For example, it could be of type
Component. APainterthat expected it could then read state from thatComponentand use the state for painting. For example, an implementation may read the backgroundColor and use that.Generally, to enhance reusability, most standard
Painters ignore this parameter. They can thus be reused in any context. Theobjectmay be null. Implementations must not throw a NullPointerException if the object parameter is null.Finally, the
widthandheightarguments specify the width and height that thePaintershould paint into. More specifically, the specified width and height instruct the painter that it should paint fully within this width and height. Any specified clip on thegparam will further constrain the region.For example, suppose I have a
Painterimplementation that draws a gradient. The gradient goes from white to black. It "stretches" to fill the painted region. Thus, if I use thisPainterto paint a 500 x 500 region, the far left would be black, the far right would be white, and a smooth gradient would be painted between. I could then, without modification, reuse thePainterto paint a region that is 20x20 in size. This region would also be black on the left, white on the right, and a smooth gradient painted between.
-
-