摆动步进式光标移动

发布于 2024-12-29 04:01:45 字数 194 浏览 2 评论 0原文

我有 java swing 国际象棋应用程序。光标具有自定义视图 - 矩形,大小适合整个单元格。我需要光标仅在整个单元格上移动。不在一个细胞的限制之内。这个问题有一些典型的解决方案吗?或者也许可以使用标准java功能设置步进式光标移动?

在此处输入图像描述

I have java swing chess application. Cursor has custom view - rectangle, sized to fit whole cell. And I need cursor moving only over whole cell. Not in the limits of one cell. Is there some typical solutions for this problem? Or maybe it is possible to set with standard java capabilities step-type cursor moving?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女皇必胜 2025-01-05 04:01:45

我不会实现某种“步进”光标。相反,我会完全隐藏光标并以编程方式突出显示当前单元格。


下面是“输出”此屏幕截图的完整示例:

screenshot

public class StepComponent extends JComponent implements MouseMotionListener {
    private Point point = new Point(0, 0);

    public StepComponent() {
        setCursor(Toolkit.getDefaultToolkit().createCustomCursor(
                new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB), 
                new Point(0, 0), "blank cursor"));
        addMouseMotionListener(this);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        int x = 0, y = 0;
        while (x < getWidth()) { g.drawLine(x, 0, x, getHeight()); x += 10; }
        while (y < getHeight()) { g.drawLine(0, y, getWidth(), y); y += 10; }
        if (point != null)
            g.fillRect(point.x, point.y, 10, 10);
    }
    @Override public void mouseDragged(MouseEvent e) { update(e.getPoint()); }
    @Override public void mouseMoved(MouseEvent e) { update(e.getPoint()); }

    private void update(Point p) {
        Point point = new Point(10 * (p.x / 10), 10 * (p.y / 10));
        if (!this.point.equals(point)) {
            Rectangle changed = new Rectangle(this.point,new Dimension(10,10));
            this.point = point;
            changed.add(new Rectangle(this.point, new Dimension(10, 10)));
            repaint(changed);
        }
    }
}

和一些测试代码:

public static void main(String[] args) {

    JFrame frame = new JFrame("Test");

    frame.add(new StepComponent());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);
    frame.setVisible(true);
}

I wouldn't implement some kind of "stepping" cursor. Instead I would hide the cursor completly and highlight the current cell programmatically.


Full example below that "outputs" this screenshot:

screenshot

public class StepComponent extends JComponent implements MouseMotionListener {
    private Point point = new Point(0, 0);

    public StepComponent() {
        setCursor(Toolkit.getDefaultToolkit().createCustomCursor(
                new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB), 
                new Point(0, 0), "blank cursor"));
        addMouseMotionListener(this);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        int x = 0, y = 0;
        while (x < getWidth()) { g.drawLine(x, 0, x, getHeight()); x += 10; }
        while (y < getHeight()) { g.drawLine(0, y, getWidth(), y); y += 10; }
        if (point != null)
            g.fillRect(point.x, point.y, 10, 10);
    }
    @Override public void mouseDragged(MouseEvent e) { update(e.getPoint()); }
    @Override public void mouseMoved(MouseEvent e) { update(e.getPoint()); }

    private void update(Point p) {
        Point point = new Point(10 * (p.x / 10), 10 * (p.y / 10));
        if (!this.point.equals(point)) {
            Rectangle changed = new Rectangle(this.point,new Dimension(10,10));
            this.point = point;
            changed.add(new Rectangle(this.point, new Dimension(10, 10)));
            repaint(changed);
        }
    }
}

And some test code:

public static void main(String[] args) {

    JFrame frame = new JFrame("Test");

    frame.add(new StepComponent());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);
    frame.setVisible(true);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文