2

我有一个问题,如图所示

绘图覆盖滚动条

我的添加过程如下:

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

我在paintComponent 中绘制我的绘图,并在最后调用revalidate()。帮助将不胜感激!

编辑:

油漆组件代码

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

3 回答 3

1

看起来有些东西不符合您的clip. 也许它正在改变shape.draw()方法?

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

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

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

shape.draw(newG);
于 2012-05-16T14:47:10.020 回答
1

我认为您应该尝试获取框架 JRootPane 对象,向下迭代到您的组件级别并使用 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);
于 2012-05-16T15:11:48.810 回答
1

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

谢谢你的帮助!

于 2012-05-17T01:55:26.837 回答