2012-05-16 28 views
2

我有一個問題,因爲圖片中所示的JPanel在JScrollPane中,繪製了滾動條

Drawing overlays scrollbars

我添加的程序如下:

的JFrame - >查看面板 - > JTabbedPane中 - > JPanel(我的畫布)

我在paintComponent中繪製我的繪圖,並在最後調用revalidate()。幫助將不勝感激!

編輯:

的paintComponent代碼

public void paintComponent(Graphics g) { 
    super.paintComponent(g);   
    Graphics2D g2d = (Graphics2D) g; 

    //Obtain document size 
    double width = model.getWidth(); 
      double height = model.getHeight(); 

    canvasBounds = new Rectangle2D.Double(0, 0, width, height); 
    g2d.setColor(Color.WHITE); 
    g2d.fill(canvasBounds); 

    Dimension size = new Dimension(); 
    size.setSize(canvasBounds.getWidth() * zoomFactor, canvasBounds.getHeight() * zoomFactor); 
    this.setPreferredSize(size); 

    g2d.setClip(canvasBounds); 
    List<DrawableShape> svgShapes = model.getDrawableShapes(); 
    for(DrawableShape shape : shapeList) { 
     shape.draw(g2d);    
    }   
    revalidate(); 
} 
+0

你可以請你展示你的代碼爲'paintComponent()' – wattostudios

+0

我已經添加了代碼。 –

+0

您試圖達到的目標讓我想到這個Oracle教程:http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html#update。也許它可以幫助你。 –

回答

1

我的問題解決了。我必須將canvasBounds上的剪輯與g2d.getClipBounds()進行比較。由於我的canvasBounds剪輯比給定的g2d大得多,因此它在滾動條上繪製。

謝謝你的幫助!

1

它看起來像是不服從你的clip的邊界。也許它在shape.draw()方法中被更改?

爲什麼不嘗試創建一個新的圖形對象傳遞給shape.draw()而不是你的setClip()。事情是這樣的......

Graphics2D newG = (Graphics2D)g.create(0, 0, width, height); 

,然後改變你的代碼,這...

shape.draw(newG); 
1

我的事情你應該儘量讓你的幀的JRootPane對象,遍歷到您的組件級別和retreieve您的組件與getBounds()綁定,然後將此Rectangles添加到剪貼蒙版。這樣你的油漆就會看起來像它背後的組件,而不是它的上面。

最近我想出了同樣的問題。我這樣解決:

Rectangle mask = null; 


for(Component c : getComponents()) 

{ 

    if(c instanceof JRootPane) 

    { 

     JRootPane rootPane = (JRootPane) c; 

     rootPane.setDoubleBuffered(true); 

     for(Component cRootPane : rootPane.getComponents()) 

     { 

      if(cRootPane instanceof JLayeredPane) 

      { 

       JLayeredPane cLayerPanels = (JLayeredPane) cRootPane; 

       cLayerPanels.setDoubleBuffered(true); 

       for(Component cLayerPanel : cLayerPanels.getComponents()) 

       { 

        if(cLayerPanel instanceof JPanel) 

        { 

         JPanel cPanels = (JPanel) cLayerPanel; 

         cPanels.setDoubleBuffered(true); 



         for(Component cPanel : cPanels.getComponents()) 

         { 

          if(cPanel instanceof JPanel) 

          { 

           JPanel cPanels2 = (JPanel) cPanel; 

           cPanels2.setDoubleBuffered(true); 

           mask = getBounds(); 

           for(Component cPanel2 : cPanels2.getComponents()) 

           { 

            mask.union(cPanel2.getBounds()); 

            cPanel2.paint(cPanel2.getGraphics()); 

           } 

          } 

         } 

        } 

       } 

      } 

     } 

    } 

} 

getGraphics().setClip(mask);