Take the 2-minute tour ×

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!


paintComponent code

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

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

    canvasBounds = new Rectangle2D.Double(0, 0, width, height);

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

    List<DrawableShape> svgShapes = model.getDrawableShapes();
    for(DrawableShape shape : shapeList) {
share|improve this question
Could you please show your code for paintComponent() –  WATTO Studios May 16 '12 at 14:40
I've added the code. –  BenC May 16 '12 at 14:48
What you are trying to achieve make me think of this Oracle tutorial : docs.oracle.com/javase/tutorial/uiswing/components/… . Maybe it can help you. –  alain.janinm May 16 '12 at 16:10
For better help sooner, post an SSCCE. (Note: SSCCE!='your code'.) –  Andrew Thompson May 16 '12 at 20:54
add comment

3 Answers

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...

share|improve this answer
add comment

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;


        for( Component cRootPane : rootPane.getComponents())


             if( cRootPane instanceof JLayeredPane)


                JLayeredPane cLayerPanels = (JLayeredPane) cRootPane;


                for( Component cLayerPanel : cLayerPanels.getComponents() )


                    if( cLayerPanel instanceof JPanel)


                        JPanel cPanels = (JPanel) cLayerPanel;


                        for( Component cPanel : cPanels.getComponents() )


                            if( cPanel instanceof JPanel)


                                JPanel cPanels2 = (JPanel) cPanel;


                                 mask = getBounds();

                                for( Component cPanel2 : cPanels2.getComponents() )













share|improve this answer
add comment
up vote 1 down vote accepted

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!

share|improve this answer
add comment

Your Answer


