J'ai paintThumb utilisant fait tomber ma flèche hors du JSlider quand à 0 ou 100, comment cela peut-il être fixé?

StackOverflow https://stackoverflow.com/questions/2962480

  •  23-10-2019
  •  | 
  •  

Question

L'image explique tout. J'ai peint un nouveau pouce et il va hors de la zone JSlider lorsqu'au plus de 95 ou inférieur à 5. J'ai essayé padding la piste sans succès.

text alt

Quelqu'un at-il des conseils?

Voici mon code. Je crois que ma question est très bien tourner la taille du pouce sous la commande prioritaire getThumbSize.

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

}
Était-ce utile?

La solution

Priorité de curseur orientation .

Addendum: En aparté, le haut du pouce ne pas « tomber »; il a été coupé. Voir Peinture dans AWT et swing pour en savoir plus sur le rectangle clip .

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top