4

I have a custom scrollbarUI which I paint both thumb and track of scrollbar. But when scrolling it keeps some lines like below which I don't want. :

enter image description here

Painting code is below:

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);
}
Tim B
  • 40,716
  • 16
  • 83
  • 128
Priyan Perera
  • 469
  • 1
  • 8
  • 13
  • 3
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson May 14 '13 at 04:55
  • 2
    I guess you need to use ``g.drawRoundRect(..., thumbBounds.height - 1, 2, 2)`` instead of ``g.drawRoundRect(..., thumbBounds.height, 2, 2)``. – aterai May 14 '13 at 05:35
  • That worked. . .! Thanks aterai.. May I know the clear reason for that? – Priyan Perera May 14 '13 at 05:56
  • 3
    The bottom edges of the ``drawRoundRect(x,y,width,height,arcW,arcH)`` are at ``y + height``, but the bottom edges of the ``repaint(thumbBounds)`` are at ``thumbBounds.y + thumbBounds.height - 1``. So 1px line remain free of repaint. – aterai May 14 '13 at 06:51

1 Answers1

0

You need to use

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

instead of

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

The bottom edges of the drawRoundRect(x,y,width,height,arcW,arcH) are at y + height, but the bottom edges of the repaint(thumbBounds) are at thumbBounds.y + thumbBounds.height - 1. So 1px line remain free of repaint.

Tim B
  • 40,716
  • 16
  • 83
  • 128