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

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

问题描述

...在图像中,做自己的[XY]一些计算坐标。

... 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.

可你的名字我的包/类,我可以为了做任务用?的code已经做它也欢迎参考。

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.

感谢您!

推荐答案

这里的问题有三个方面:

The issue here is three-fold:

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

要实现以上几点将是子类 的JP​​anel ,并提供这些功能。

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

1。显示在面板的背景图像。

首先,作为一个的JP​​anel 没有显示默认背景图像的一种方式,必须有一种方法来保持在的JPanel ,然后绘制,面板本身,它可以通过<一个执行上href="http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#paintComponent%28java.awt.Graphics%29"><$c$c>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.

要实现这一目标的一种方法是在其中持有到图像类的字段画:

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 关联的 MyPanel 键,可以用来进行图形操作的对象。

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

2。在发现该鼠标点击的点。

其次,为了检索在该鼠标点击的点,人们可以分配一个<一href="http://java.sun.com/javase/6/docs/api/java/awt/event/MouseListener.html"><$c$c>MouseListener MyPanel 。在下面的例子中,一个匿名内部类扩展<一href="http://java.sun.com/javase/6/docs/api/java/awt/event/MouseAdapter.html"><$c$c>MouseAdapter用于最小化编写额外的code:

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。如何绘制面板上的一个点。

第三,为了找到一个点处的鼠标单击,人们可以从<一个获得它href="http://java.sun.com/javase/6/docs/api/java/awt/event/MouseEvent.html"><$c$c>MouseEvent这是从<一个传入的对象href="http://java.sun.com/javase/6/docs/api/java/awt/event/MouseAdapter.html#mouseClicked%28java.awt.event.MouseEvent%29"><$c$c>mouseClicked方法:

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

虽然上面的code未经过测试,它应该是一个起点。

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

例如,如果多个点需要被拉伸,也许有一个名单,其中;点&GT; 持有点,并提请各的 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.

如果额外的处理需要单击鼠标时,额外的code可被添加到的mouseClicked 的方法来进行。

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

其他资源:

  • Lesson: Performing Custom Painting
  • Painting in AWT and Swing
  • How to Write a Mouse Listener

谢谢zedoo的指出在拨打电话到 super.paintComponent方法应该重写时,被执行的意见的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天全站免登陆