使用蛮力将作业分配给最接近的一对点。

我似乎拥有了我所需要的大部分东西-框架出现了,单击的地方出现了圆圈,最接近的对似乎正确地突出了,我说“似乎”,因为这是我的问题所在。在任何给定时间,我只需要突出显示两个圆圈(最接近的对),但是先前的最接近的对不会取消选择/取消突出显示。

我希望这是我忽略的简单事情。我已经有一段时间了,所以很可能我的头脑和眼睛太累了以至于无法解决这个问题。希望新鲜的眼睛对我有好处。

任何帮助是极大的赞赏。

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

最佳答案

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

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

查看Performing Custom Painting了解更多详细信息

09-27 23:03