使用鼠标点击最近的一对点

时间:2014-07-16 02:40:00

标签: java swing mouselistener brute-force closest-points

使用蛮力分配到最近的一对点。

我似乎拥有我需要的大部分内容 - 框架出现,圆圈出现在我点击的位置,最近的对似乎正确突出显示,我说“似乎”因为这是我的问题所在。我只需要在任何给定时间突出显示两个圆圈(最近的一对),但以前最近的一对不会取消选择/取消高亮。

我希望这是我忽略的简单事项。我已经有一段时间了,所以很可能我的思绪和眼睛太累了,无法选择这个问题。希望,一双新鲜的眼睛会对我有所帮助。

非常感谢任何帮助。

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;

public class AssignmentFour extends JFrame {

private final PointPanel canvas = new PointPanel();

public AssignmentFour() {
    JPanel panel = new JPanel();

    this.add(canvas, BorderLayout.CENTER);
    this.add(panel, BorderLayout.SOUTH);
    add(new JLabel("<html> Click anywhere within the panel."
            + "<BR> The two closest points will be highlighted.<BR>"
            + "Click the 'x' to exit program. </html>"), BorderLayout.SOUTH);

}//AssignmentFour method

public static void main(String[] args) {
    AssignmentFour frame = new AssignmentFour();
    frame.setTitle("Closest points");
    frame.setLocationRelativeTo(null); // Center the frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 600);
    frame.setVisible(true);
}//main

class PointPanel extends JPanel {

    private int xCoordinate = 200;
    private int yCoordinate = 200;
    private int radius = 5;
    private Graphics g;
    double minDist = 999999999;
    ArrayList<Point> points = new ArrayList<>();
    Point ClosestPoint1, ClosestPoint2;

    public final double distBetweenPoints(int x, int y, int a, int b) {
        double theDist = 0.0;

        theDist = Math.sqrt(Math.pow(a - x, 2) + Math.pow(y - b, 2));

        return theDist;
    }//distBetweenPoints method

    public PointPanel() {
        addMouseListener(new MouseAdapter() {

            @Override
            public void mouseReleased(MouseEvent e) {
                xCoordinate = e.getX();
                yCoordinate = e.getY();
                Point newCoord = e.getPoint();
                points.add(newCoord);
                g = getGraphics();
                g.setColor(Color.RED);
                g.drawOval(xCoordinate, yCoordinate, radius, radius);
                //g.dispose();
                if (points.size() >= 2) {
                    for (int i = 0; i < points.size(); i++) {
                        for (int j = i + 1; j < points.size(); j++) {
                            Point p = points.get(i);
                            Point q = points.get(j);
                            int x1 = p.x;
                            int y1 = p.y;
                            int a1 = q.x;
                            int b1 = q.y;

                            if (distBetweenPoints(x1, y1, a1, b1) < minDist) {
                                minDist = distBetweenPoints(x1, y1, a1, b1);
                                ClosestPoint1 = p;
                                ClosestPoint2 = q;

                            }//inner if 
                            else {
                            }//else
                        }//for 'j'
                    }// for 'i'
                    int x2 = ClosestPoint1.x;
                    int y2 = ClosestPoint1.y;
                    int a2 = ClosestPoint2.x;
                    int b2 = ClosestPoint2.y;

                    g.fillOval(x2, y2, radius, radius);
                    g.fillOval(a2, b2, radius, radius);
                }//if
            }//mouseReleased
        }//MouseListener
        );
    }//pointPanel
}//point panel extend

}//assignmentfour class

1 个答案:

答案 0 :(得分:4)

请勿使用getGraphics执行 自定义绘画,这不是在Swing中完成绘画的方式

相反,覆盖JPanel s paintComponent方法并将自定义绘画放入其中

请查看Performing Custom Painting了解详情