2014-09-19 48 views
1

這更多的是一個概念性問題,所以很難發佈一個小的可用代碼示例。但是,我有一個類,這裏​​覆蓋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。有沒有解決方案?另外,我在做一些概念上的錯誤?

+0

我想知道你是否想要實現一個共享接口,並且可能共享相同的組合組件以允許共享行爲,而不是在這裏使用繼承。 – 2014-09-19 14:14:47

+0

你可能想看看[這個問題](http://stackoverflow.com/questions/586363/why-is-super-super-method-not-allowed-in-java)。 – Rob 2014-09-19 14:21:56

+0

另一種方式,如果面板在其他方面是相同的,只是有一個實現,有可能工作在縱橫比保存模式。 – kiheru 2014-09-19 14:24:37

回答

4

也許我誤解你的問題,但我會單獨的標準和風俗畫

public abstract class BasePanel extends JPanel { 

...  

    @Override 
    protected void paintComponent(Graphics g) { 
    super.paintComponent(g);  
    provideCustomPainting(g); 
    } 

    protected void provideCustomPainting(Graphics g) { 
    Graphics2D g2 = (Graphics2D)g; 
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
         RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); 

    this.standardDraw(drawObjects,g2); 
    } 
} 

public class AspectRatioPanel extends BasePanel { 
    protected void provideCustomPainting(Graphics 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); 
    } 
} 
+0

''也許我誤解了你的問題,......「 - 不,這個答案看上去對我來說很重要。 100+ – 2014-09-19 14:25:55

+0

這將工作,我會繼續並接受它。我想知道我能否以更好的方式處理它。謝謝! – Justin 2014-09-19 14:36:21

1

你可以簡單地呼叫super.paintComponent方法(圖形);通過覆蓋paintComponent(),您有3個選項:

a。)在方法開始時調用super;您可以在超級課堂上繪製的代碼上繪製代碼塗料。

b。)根本不要調用super - 您的paintComponent需要確保它能夠繪製所需的所有東西;如果組件是不透明的,那意味着您需要繪製組件佔據的整個區域。 c)以方便的方式致電超級會員;無論在超級中畫什麼,都會按照您所說的順序依次「分層」。只有超級方法不繪製整個區域時纔有意義。

如果硬要在這兩者之間使用由特定類的繼承層次爲super.paintComponent方法的的paintComponent,無論繼承層次的,也可能這就是:

BasePanel extends JPanel { 
    protected final defaultPaintComponent(Graphics g) { 
     super.paintComponent(g); 
    } 
} 

子類可以稱之爲「defaultPaintComponent」,而不是super.paintComponent,從而繞過定義的層次結構之間的任何實現類(我建議最終聲明它以防止意外覆蓋)。

+0

我很欣賞這些建議。這是我將不得不更多考慮的事情。 – Justin 2014-09-19 14:46:23