java - 使用 paintThumb 我已经让我的箭头在 0 或 100 时从 JSlider 上掉下来,如何解决这个问题?

标签 java swing paint jslider

图片说明了一切。我画了一个新的 Thumb,它在高于 95 或低于 5 时离开 JSlider 区域。我尝试填充 Track 但没有成功。

alt text

有人有什么建议吗?

这是我的代码。我相信我的问题是在 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()
    }

}

最佳答案

覆盖 getThumbSize() ,返回拇指边界矩形的尺寸。如果不对称,您应该考虑 slider 的 orientation .

附录:顺便说一句,拇指的顶部并没有“脱落”;它被剪掉了。参见 Painting in AWT and Swing有关剪辑矩形的更多信息。

关于java - 使用 paintThumb 我已经让我的箭头在 0 或 100 时从 JSlider 上掉下来,如何解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2962480/

相关文章:

java - 如何从形状中减去图像的面积

java - servlet 中 "response.setContentType("text/html")"的用途是什么

java - 为什么 Java 编译器在将通配符与 extends 一起使用时甚至限制向列表中添加正确类型的元素

Java Swing JTable 排序

java - 如何使用 java swing 在 jlist 中的现有元素下添加新元素?

android - 如何在 android 中为绘画创建这种类型的画笔

java - 使用 Java 处理单词缩略的有效方法是什么?

java - 如何在 Java 中将一个 Swing 组件的图形图像复制到另一个 Swing 组件

java - 如何在 Java swing 中将文件或文件文件夹作为输出?

c# - 如何以数学方式绘制 WPF 形状?