Class AbstractPainter<T>
- Type Parameters:
T- an optional configuration parameter
- All Implemented Interfaces:
Painter<T>
- Direct Known Subclasses:
AttributionPainter,CompoundPainter,WaypointPainter
public abstract class AbstractPainter<T> extends AbstractBean implements Painter<T>
A convenient base class from which concrete Painter implementations may
extend. It extends AbstractBean as a convenience for
adding property change notification support. In addition, AbstractPainter
provides 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 the visibility property.
Subclasses of AbstractPainter generally need only override the
doPaint(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 Painter that
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. -
Constructor Summary
Constructors Constructor Description AbstractPainter()Creates a new instance of AbstractPainter.AbstractPainter(boolean cacheable)Creates a new instance of AbstractPainter. -
Method Summary
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.protected booleanisDirty()Ye olde dirty bit.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.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.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
-
AbstractPainter
public AbstractPainter()Creates a new instance of AbstractPainter. -
AbstractPainter
public AbstractPainter(boolean cacheable)Creates a new instance of AbstractPainter.- Parameters:
cacheable- indicates if this painter should be cacheable
-
-
Method Details
-
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
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
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- the cache flag
-
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.
-
validate
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- the object to validate
-
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.
-
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
Subclasses must implement this method and perform custom painting operations here.- Parameters:
width- the widthheight- the heightg- The Graphics2D object in which to paintobject- an optional configuration parameter
-
paint
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.
-