Hi,
I’ve just spent the last few hours browsing trough Java’s class library to see if I could figure out how it all (or at least parts of it…) works. I would like to override the getGraphics() method in java.awt.Canvas (inherrited from Component) to return a Graphics object from my buffer strategy instead. Under which conditions can this be done. I.e. what calls do I have to make in my override method so everything (graphics wise) still works as expected?
Here is the source code from java.awt.Component:
/**
* Creates a graphics context for this component. This method will
* return <code>null</code> if this component is currently not
* displayable.
* @return a graphics context for this component, or <code>null</code>
* if it has none
* @see #paint
* @since JDK1.0
*/
public Graphics getGraphics()
{
if (peer instanceof LightweightPeer)
{
// This is for a lightweight component, need to
// translate coordinate spaces and clip relative
// to the parent.
if (parent == null)
return null;
Graphics g = parent.getGraphics();
if (g == null)
return null;
if (g instanceof ConstrainableGraphics)
{
((ConstrainableGraphics) g).constrain(x, y, width, height);
}
else
{
g.translate(x, y);
g.setClip(0, 0, width, height);
}
g.setFont(getFont());
return g;
}
else
{
ComponentPeer peer = this.peer;
return (peer != null) ? peer.getGraphics() : null;
}
}
If both Component and Canvas can be said not to be LightweightPeer:s we can rule out most of the code. Neither of the two implement the LightweightPeer interface. If it isn’t some internal stuff of course.
Best Regards,
Johan Tibell