2

The picture explains it all. I've painted a new Thumb and it goes off the JSlider area when at higher than 95 or below 5. I've tried padding the Track with no success.

alt text

Does anyone have any tips?

Here is my code. I believe my issue is fine turning the size of the thumb under the getThumbSize override.

private static class MySliderUI extends BasicSliderUI {

    private int thumbHeight = 22;
    private int thumbWidth = 22;

    public MySliderUI(JSlider b) {
        super(b);
        // this.thumbHeight = slider.getHeight();
        // this.thumbWidth = slider.getHeight();
    }

    @Override
    protected Dimension getThumbSize() {
        return new Dimension(thumbHeight, thumbWidth);
    }

    @Override
    public void paintTrack(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        Rectangle r = trackRect;
        r.width = 40; // (int) (0.15 * r.getHeight());

        float[] dist = { 0.1f, 0.5f, 0.9f };
        Color[] colors = { new Color(34, 177, 76), new Color(255, 242, 0),
                new Color(237, 28, 36) };
        Point2D start = new Point2D.Float(r.x, r.y);
        Point2D end = new Point2D.Float(r.x, r.y + r.height);
        LinearGradientPaint p = new LinearGradientPaint(start, end, dist,
                colors);
        g2d.setPaint(p);
        g2d.fill(r);
    }

    @Override
    public void paintThumb(Graphics g) {

        Graphics2D g1 = (Graphics2D) g;

        // Make a triangle - Arrow on Meter
        int[] x = new int[3];
        int[] y = new int[3];
        int n; // count of points

        // Set Points for Arrow
        Integer arrowX = thumbRect.x;
        Integer arrowY = thumbRect.y;
        x[0] = arrowX - 25;
        x[1] = arrowX - 25;
        x[2] = arrowX - 2;
        y[0] = arrowY - 17; // Top Left
        y[1] = arrowY + 27; // Bottom Left
        y[2] = arrowY + 5; // Tip of Arrow
        n = 3; // Number of points, 3 because its a triangle

        // Draw Arrow Border
        Polygon myTriShadow = new Polygon(x, y, n); // a triangle
        g1.setPaint(Color.black);
        g1.fill(myTriShadow);

        // Set Points for Arrow Board
        x[0] = x[0] + 2;
        x[1] = x[1] + 2;
        x[2] = x[2] - 3;
        y[0] = y[0] + 5;
        y[1] = y[1] - 5;
        y[2] = y[2];

        // Color colorMeter = robot.getPixelColor(x[2]+10, y[2]);

        // Draw Arrow
        Polygon myTri = new Polygon(x, y, n); // a triangle
        // Color colr = new Color();
        g1.setPaint(Color.yellow);
        g1.fill(myTri);

        // super.paintThumb(g); // replace with your fill()
    }

}
Community
  • 1
  • 1
Landmine
  • 1,759
  • 6
  • 39
  • 60
  • Yet again you asked a question we can't help you with (I see you deleted the last one). You have written custom code and you obviously have a bug in the code. What do you expect us to do about it since you haven't posted a SSCCE (http://sscce.org)? We don't know what you have changed. If you post a SSCCE that shows the old code and highlights what you have changed, then maybe someone will be generous enought to look at the code. Otherwise we are not mind readers and can't guess what you have done. – camickr Jun 03 '10 at 00:22
  • I thank you for your help, but it seems that others are able to "Read my mind" and post information and push me in a direction to help me. – Landmine Jun 03 '10 at 06:16
  • @Landmine: As a fan of the http://sscce.org/, I would not discount camickr's suggestion too quickly. Just creating one offers considerable insight and occasionally reveals an immediate solution. Very nice gradient, BTW. – trashgod Jun 03 '10 at 13:33
  • I'm very new to Java and SF, so I'm sorry if I'm not posting the ideal way, I'll work on that to better help SF help me. – Landmine Jun 03 '10 at 16:57
  • @Landmine: Excellent! I'd used `RadialGradientPaint` but overlooked `LinearGradientPaint`; thanks. Here's some elementary code for scaling and rotating a polygon you may enjoy. http://sites.google.com/site/drjohnbmatthews/point-in-polygon – trashgod Jun 04 '10 at 02:09

1 Answers1

2

Override getThumbSize(), returning the dimensions of the bounding rectangle of your thumb. If not symmetric, you should account for the slider's orientation.

Addendum: As an aside, the top of the thumb didn't "fall off"; it was clipped. See Painting in AWT and Swing for more about the clip rectangle.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045