Jpanel在JScrollPane中,绘制滚动条

时间:2012-05-16 14:38:38

标签: java swing custom-controls jscrollpane graphics2d

我遇到了问题,如图所示

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();
}

3 个答案:

答案 0 :(得分:1)

看起来某些东西不符合你clip的界限。也许它在shape.draw()方法中被改变了?

为什么不尝试创建一个新的Graphics对象来传递给shape.draw(),而不是setClip()。像这样......

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

然后将您的代码更改为此...

shape.draw(newG);

答案 1 :(得分:1)

我认为你应该尝试获取你的帧JRootPane对象,迭代到你的组件级别并使用getBounds()来恢复你的组件边界,然后将这个矩形添加到你的剪贴蒙版。这样你的油漆看起来就像是在你的组件后面,而不是在它上面。

最近我想出了同样的问题。我这样解决了:

    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);

答案 2 :(得分:1)

我的问题已经解决了。我不得不将canvasBounds上的剪辑与g2d.getClipBounds()进行比较。由于我的canvasBounds剪辑比给定的g2d大得多,所以它在滚动条上绘制。

谢谢你的帮助!