如何在JavaSwing中在给定的坐标上创建点图像?

sigwle7e  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(113)

我正在准备一个程序来演示quickhull算法的工作逻辑。我的问题是这个。首先,用户输入几个点坐标。然后运行quickhull算法并计算convexhull。我想要的是,我想创建一个“点”的图片,我从互联网上下载的点,用户进入。我该怎么做?如果你能帮助我,我将不胜感激。
这是我的gui类(包括main方法):

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.EventQueue;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;
import javax.swing.JLabel;

public class GUI extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GUI frame = new GUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public GUI() {
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 1097, 618);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JPanel panel = new JPanel();
        panel.setBounds(0, 0, 1112, 608);
        contentPane.add(panel);
        panel.setLayout(null);

        JLabel lblNewLabel = new JLabel("");
        Image img = new ImageIcon(this.getClass().getResource("/Adsız.png")).getImage();
        lblNewLabel.setIcon(new ImageIcon(img));
        lblNewLabel.setBounds(0, -23, 1112, 608);
        panel.add(lblNewLabel);
        panel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Graphics g = panel.getGraphics();

                System.out.println("Quick Hull Test");
                Scanner sc = new Scanner(System.in);

                System.out.println("Enter the number of points");
                int N = sc.nextInt();

                ArrayList<Point> points = new ArrayList<>();
                System.out.println("Enter the coordinates of each points: <x> <y>");

                for (int i = 0; i < N; i++) {
                    int x = sc.nextInt();
                    int y = sc.nextInt();

                    x+=550;
                    y+=80;

                    Point e1 = new Point(x, y);
                    points.add(i, e1);
                }

                Stack<Point> points1 = new Stack<>();

                QuickHull qh = new QuickHull();
                ArrayList<Point> p = qh.quickHull(points);
                System.out.println("The points in the Convex hull using Quick Hull are: ");
                for (Point point : p) {
                    int trueX = point.x - 550;
                    int trueY = point.y - 80;
                    System.out.println("(" + trueX + ", " + trueY + ")");
                }

                for (Point point : p)
                    points1.add(point);

                for (int i = 0; i < points1.size(); i++) {
                    Point first = points1.peek();
                    while(points1.size() != 0) {
                        Point one = points1.pop();
                        if(points1.size() != 0) {
                            Point two = points1.peek();
                            g.drawLine(one.x, one.y, two.x, two.y);                     
                        }
                        else {
                            g.drawLine(one.x, one.y, first.x, first.y);
                        }
                    }                   
                }

                sc.close();
            }
        });

    }
}

下面是我的quickhull算法类:

import java.awt.*;
import java.util.ArrayList;

public class QuickHull {

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public ArrayList<Point> quickHull(ArrayList<Point> points) {
        ArrayList<Point> convexHull = new ArrayList<>();
        if (points.size() < 3)
            return (ArrayList) points.clone();

        int minPoint = -1, maxPoint = -1;
        int minX = Integer.MAX_VALUE;
        int maxX = Integer.MIN_VALUE;

        for (int i = 0; i < points.size(); i++) {

            if (points.get(i).x < minX) {
                minX = points.get(i).x;
                minPoint = i;
            }

            if (points.get(i).x > maxX) {
                maxX = points.get(i).x;
                maxPoint = i;
            }
        }

        Point A = points.get(minPoint);
        Point B = points.get(maxPoint);

        convexHull.add(A);
        convexHull.add(B);

        points.remove(A);
        points.remove(B);

        ArrayList<Point> leftSet = new ArrayList<>();
        ArrayList<Point> rightSet = new ArrayList<>();

        for (Point p : points) {
            if (pointLocation(A, B, p) == -1)
                leftSet.add(p);

            else if (pointLocation(A, B, p) == 1)
                rightSet.add(p);
        }

        hullSet(A, B, rightSet, convexHull);
        hullSet(B, A, leftSet, convexHull);

        return convexHull;
    }

    public int distance(Point A, Point B, Point C) {
        int ABx = B.x - A.x;
        int ABy = B.y - A.y;

        int num = ABx * (A.y - C.y) - ABy * (A.x - C.x);

        if (num < 0)
            num = -num;

        return num;
    }

    public void hullSet(Point A, Point B, ArrayList<Point> set, ArrayList<Point> hull) {
        int insertPosition = hull.indexOf(B);

        if (set.size() == 0)
            return;

        if (set.size() == 1) {
            Point p = set.get(0);
            set.remove(p);
            hull.add(insertPosition, p);
            return;
        }

        int dist = Integer.MIN_VALUE;
        int furthestPoint = -1;

        for (int i = 0; i < set.size(); i++) {
            Point p = set.get(i);
            int distance = distance(A, B, p);

            if (distance > dist) {
                dist = distance;
                furthestPoint = i;
            }
        }

        Point P = set.get(furthestPoint);
        set.remove(furthestPoint);
        hull.add(insertPosition, P);

        ArrayList<Point> leftSetAP = new ArrayList<>();

        for (Point M : set) {
            if (pointLocation(A, P, M) == 1) {
                leftSetAP.add(M);
            }
        }

        ArrayList<Point> leftSetPB = new ArrayList<>();

        for (Point M : set) {
            if (pointLocation(P, B, M) == 1) {
                leftSetPB.add(M);
            }
        }

        hullSet(A, P, leftSetAP, hull);
        hullSet(P, B, leftSetPB, hull);
    }

    public int pointLocation(Point A, Point B, Point P) {
        int cp1 = (B.x - A.x) * (P.y - A.y) - (B.y - A.y) * (P.x - A.x);

        return Integer.compare(cp1, 0);
    }    
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题