Ben C.
Ben C.

Reputation: 1178

Jpanel in a JScrollPane, drawing over scroll bars

I have a problem as shown in the picture

Drawing overlays scrollbars

My procedure of adding is as follows:

JFrame -> View Panel -> JTabbedPane -> JPanel (My Canvas)

I draw my drawings inside paintComponent, and calls revalidate() at the very end. Help would be highly appreciated!

Edit:

paintComponent code

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

Upvotes: 2

Views: 1136

Answers (3)

Ben C.
Ben C.

Reputation: 1178

My issue was solved. I had to compare the clipping on my canvasBounds with the g2d.getClipBounds(). Since my canvasBounds clip was much bigger than what was g2d given, it drew at the scroll bar.

Thank you your help!

Upvotes: 1

Leandro Pezzente
Leandro Pezzente

Reputation: 41

I thing you should try to get your frames JRootPane object , iterate down to your Components Level and retreieve your components bounds with getBounds() , and then add this Rectangles to your clipping mask. That way your paint will look like its behind your components and not above it.

Recently I came up with the same problem. I solved this way :

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

Upvotes: 1

wattostudios
wattostudios

Reputation: 8764

It looks like something is not obeying the bounds of your clip. Maybe it is being changed in the shape.draw() method?

Why don't you try creating a new Graphics object to pass to shape.draw(), instead of your setClip(). Something like this...

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

And then change your code to this...

shape.draw(newG);

Upvotes: 1

Related Questions