This is more of a conceptual question, so it's hard to post a small workable code sample. But, I have a class that overrides paintComponent
here:
public abstract class BasePanel extends JPanel {
...
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
this.standardDraw(drawObjects,g2);
}
}
Basically, I'd like this to be the "standard way" this base panel draws if paintComponent
is not overridden in a derived class. So, I have a derived class called AspectRatioPanel
which I'd like to re-specify how it draws things:
public class AspectRatioPanel extends BasePanel {
...
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
// Get ViewPort Bounding Box to clip
BoundingBox viewPortBoundingBox = this.getViewPortBoundingBox();
// Clip to viewport
g2.setClip((int)viewPortBoundingBox.topLeft.getX(),(int)viewPortBoundingBox.topLeft.getY(),(int)viewPortBoundingBox.getWidth(),(int)viewPortBoundingBox.getHeight());
this.standardDraw(drawObjectsBuf,g2);
}
}
The problem I'm having is the call super.paintComponent(g)
in the derived class. I intend for it to be calling paintComponent
in JComponent
, but it is going through BasePanel
first. Is there a better way to approach this problem? I could delete the paintComponent
method in BasePanel
, but having a standard way of drawing things is useful for me. I also can't seem to call JComponent.paintComponent
directly since it's protected
. Is there a solution for this? Also, am I doing something conceptually wrong?