2013-05-14 5 views
4

J'ai une barre de défilement personnalisée que je peins à la fois le pouce et la piste de la barre de défilement. Mais lors du défilement il garde certaines lignes comme ci-dessous dont je ne veux pas. :Problème de peinture Swing

enter image description here

code peinture est ci-dessous:

protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) { 
    Graphics2D g2d = (Graphics2D) g; 

    GradientPaint gradient = new GradientPaint(new Point(thumbBounds.x, thumbBounds.y), gray, new   Point(thumbBounds.x + width, thumbBounds.y), white); 

    g.setColor(white); 
    g.fillRoundRect(thumbBounds.x + 1, thumbBounds.y + 1, thumbBounds.width - 3, thumbBounds.height - 1, 2, 2); 

    g2d.setPaint(gradient); 
    g2d.fillRoundRect(thumbBounds.x + 2, thumbBounds.y + 2, thumbBounds.width - 4, thumbBounds.height - 3, 3, 3); 

    //Draw middle lines: 
    if ((getMinimumThumbSize().height + 10) < thumbBounds.height) { 
     g.setColor(new Color(167, 167, 167)); 
     int w = ((thumbBounds.width > 16) ? 8 : (int) ((thumbBounds.width/2.0) + 0.5)); 
     int x = (thumbBounds.width > 0) ? (thumbBounds.x + ((thumbBounds.width - w)/2) - 1) : thumbBounds.x; 

     g.drawLine(x, (thumbBounds.y + (thumbBounds.height/2) - 3), (x + w), (thumbBounds.y + (thumbBounds.height/2) - 3)); 
     g.drawLine(x, (thumbBounds.y + (thumbBounds.height/2) - 1), (x + w), (thumbBounds.y + (thumbBounds.height/2) - 1)); 
     g.drawLine(x, (thumbBounds.y + (thumbBounds.height/2) + 1), (x + w), (thumbBounds.y + (thumbBounds.height/2) + 1)); 
    } 

    g.setColor(color_1); 
    g.drawRoundRect(thumbBounds.x, thumbBounds.y, thumbBounds.width - 2, thumbBounds.height, 2, 2); 
} 
+3

Pour une meilleure aide plus tôt, postez un [SSCCE] (http://sscce.org/). –

+2

Je suppose que vous devez utiliser '' g.drawRoundRect (..., thumbBounds.height - 1, 2, 2) '' au lieu de '' g.drawRoundRect (..., thumbBounds.height, 2, 2) ' '. – aterai

+0

Cela a fonctionné. . ! Merci aterai .. Puis-je connaître la raison évidente pour cela? –

Répondre

0

Vous devez utiliser

g.drawRoundRect(..., thumbBounds.height - 1, 2, 2) 

au lieu de

g.drawRoundRect(..., thumbBounds.height, 2, 2) 

Les bords inférieurs du drawRoundRect(x,y,width,height,arcW,arcH) sont à y + height, mais les bords inférieurs du repaint(thumbBounds) sont à thumbBounds.y + thumbBounds.height - 1. Donc, la ligne 1px reste libre de repeindre.