使用 Swing,我想画几个点 [英] Using Swing, I want to draw a couple of points

查看:22
本文介绍了使用 Swing,我想画几个点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

... 在图像中并对其 [x y] 坐标进行一些计算.

... in an image and do some calculation on their [x y] coordinates.

我的第一个想法是使用图像作为 JPanel 的背景,然后注册点,但我不确定是否有办法在 JPanel 上标记这些点.还有我不熟悉的绘图库,但我不确定是否可以将它们与 Swing 结合起来.

My first idea was to use an image as the background of a JPanel and then register the points, but I'm not sure there will be a way to mark these on the JPanel. There is also the Drawing library, which I'm unfamiliar with, but I'm not sure if I can combine these with Swing.

你能给我命名我可以用来完成任务的包/类吗?也欢迎参考已经这样做的代码.

Can you name me the packages/classes I can use in order to do the task? References of code that already does it are also welcome.

谢谢!

推荐答案

这里的问题有三点:

  1. 需要有一种方法来显示背景图像.
  2. 必须能够找到点击鼠标的点.
  3. 一定有办法在面板上绘制点.

实现上述几点的一种方法是将 JPanel 并提供这些功能.

One way to accomplish the above points would be to subclass a JPanel and provide those functionalities.

1.在面板中显示背景图片.

首先,由于JPanel默认没有显示背景图片的方式,所以必须有办法在JPanel中保存图片,然后在面板上绘制它,这可以通过 paintComponent 方法.

Firstly, as a JPanel does not have a way of displaying a background image by default, there must be a way to hold an image in the JPanel, and then draw that on the panel itself, which can be performed via the paintComponent method.

实现此目的的一种方法是在类中有一个字段,该字段保留要绘制的 Image:

One way to accomplish this is to have a field in the class which holds on to an Image to draw:

class MyPanel extends JPanel {
    // Background image. Initialize appropriately.
    Image backgroundImage;

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        // Draw background image each time the panel is repainted.
        g.drawImage(backgroundImage, 0, 0, null);
    }
}

图形<paintComponent 中的/a> 对象与 MyPanel 相关联,可用于执行图形操作.

The Graphics object in paintComponent is associated with the MyPanel and can be used to perform graphics operations.

2.找到点击鼠标的点.

其次,为了检索鼠标被点击的点,可以分配一个 MouseListenerMyPanel.在以下示例中,一个匿名内部类扩展了 MouseAdapter 用于尽量减少编写额外的代码:

Secondly, in order to retrieve the point at which the mouse was clicked, one could assign a MouseListener to the MyPanel. In the following example, an anonymous inner class extending the MouseAdapter is used to minimize writing extra code:

class MyPanel extends JPanel {
    // Background image. Initialize appropriately.
    Image backgroundImage;

    public MyPanel() {
         // Add a MouseListener which processes mouse clicks.
         this.addMouseListener(new MouseAdapter() {
             public void mouseClicked(MouseEvent e) {
                 // Process mouse-click.
             }
         })
    }

    // paintComponents method here.
}

鼠标点击时需要进行的处理可以包含在mouseClicked方法中.

Processing that needs to be performed when the mouse is clicked can be included in the mouseClicked method.

3.如何在面板上绘制一个点.

第三,为了找到鼠标被点击的一个点,可以从MouseEventmouseClicked 方法:

Thirdly, in order to find one point at which the mouse was clicked, one can obtain it from the MouseEvent object that was passed in from the mouseClicked method:

class MyPanel extends JPanel {
    // Background image. Initialize appropriately.
    Image backgroundImage;
    Point pointClicked;

    public MyPanel() {
         // Add a MouseListener which processes mouse clicks.
         this.addMouseListener(new MouseAdapter() {
             public void mouseClicked(MouseEvent e) {
                 // Retrieve the point at which the mouse was clicked.
                 pointClicked = e.getPoint();

                 // Repaint the panel.
                 this.repaint();
             }
         })
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        // Draw background image each time the panel is repainted.
        g.drawImage(backgroundImage, 0, 0, null);

        // Draw a little square at where the mouse was clicked.
        g.fillRect(pointClicked.x, pointClicked.y, 1, 1);
    }
}

虽然上面的代码没有经过测试,但应该是一个起点.

Although the above code is not tested, it should be a starting point.

例如,如果需要绘制多个点,可能有一个 List 来保存这些点,并绘制每个 Point 可以在 paintComponents 方法中完成.

For example, if multiple points needs to be drawing, perhaps having a List<Point> to hold the points, and drawing each Point in the paintComponents method could be done.

如果需要在鼠标点击时进行额外的处理,可以在mouseClicked方法中添加额外的代码.

If additional processing needs to be performed when the mouse is clicked, additional code can be added to the mouseClicked method.

其他资源:

感谢zedoo 在评论中指出在重写paintComponent 方法时应该调用super.paintComponent.

Thank you to zedoo for pointing out in the comments that making a call to super.paintComponent should be performed when overriding the paintComponent method.

这篇关于使用 Swing,我想画几个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆