這更多的是一個概念性問題,所以很難發佈一個小的可用代碼示例。但是,我有一個類,這裏覆蓋paintComponent
:重寫paintComponent兩次
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);
}
}
基本上,我想這是「標準方式」這個基地吸引面板如果paintComponent
沒有在派生類中重寫。所以,我有一個派生類稱爲AspectRatioPanel
,我想重新指定它是如何得出的事情:
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);
}
}
我遇到的問題是在派生類中調用super.paintComponent(g)
。我打算在JComponent
中撥打paintComponent
,但首先要經過BasePanel
。有沒有更好的方法來解決這個問題?我可以刪除BasePanel
中的paintComponent
方法,但使用標準繪圖方法對我很有用。我也無法直接呼叫JComponent.paintComponent
,因爲它是protected
。有沒有解決方案?另外,我在做一些概念上的錯誤?
我想知道你是否想要實現一個共享接口,並且可能共享相同的組合組件以允許共享行爲,而不是在這裏使用繼承。 – 2014-09-19 14:14:47
你可能想看看[這個問題](http://stackoverflow.com/questions/586363/why-is-super-super-method-not-allowed-in-java)。 – Rob 2014-09-19 14:21:56
另一種方式,如果面板在其他方面是相同的,只是有一個實現,有可能工作在縱橫比保存模式。 – kiheru 2014-09-19 14:24:37