Java 中的拖线

Dragging lines in Java

I created two lines in the middle of the screen which is a positive sign, I would like to drag the positive sign only when I click on it, but it only works when I click anywhere on the screen, I want the sign to be dragged only when I click on it and not when I click on anywhere else on the screen. I would like anyone to help me with what I can do to make this work. This is the code below

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;

public class Main extends JPanel {
    public static void main(String args[]) throws Exception {
        JFrame f = new JFrame("Shapes");
        f.setSize(600, 600);
        f.setLocation(300, 300);

        f.setResizable(true);
        JPanel p = new JPanel() {
            Point pointStart = null;
            Point pointEnd = null;
            int x2 = 250;
            int y2 = 175;


            private Shape lineMp = null;
            private Shape lineMn = null;
            {

                lineMn = new Line2D.Double(235,175,265,175);
                lineMp = new Line2D.Double(250,160,250,190);

                Point newPoint = new Point();

                MouseAdapter mouseAdapter = new MouseAdapter() {
                    private Point prevPoint;
                    @Override
                    public void mousePressed(MouseEvent e) {
                        prevPoint = e.getPoint();
                        System.out.println("Prev Point=" + prevPoint.toString());
                        repaint();
                    }
                    @Override
                    public void mouseDragged(MouseEvent e) {
                        int dx = 0;
                        int dy = 0;
                        dx = (int) (prevPoint.x - e.getPoint().getX());
                        dy = (int) (prevPoint.y - e.getPoint().getY());
                        Line2D shapeMn = (Line2D) lineMn;
                        Line2D shapeMp = (Line2D) lineMp;


                        int nx2 = (int) (shapeMn.getX2() - dx);
                        int ny2 = (int) (shapeMn.getY2() - dy);

                        int px2 = (int) (shapeMp.getX2() - dx);
                        int py2 = (int) (shapeMp.getY2() - dy);

                        int x1 = (int) (shapeMn.getX1() - dx);
                        int y1 = (int) (shapeMn.getY1() - dy);

                        int px1 = (int) (shapeMp.getX1() - dx);
                        int py1 = (int) (shapeMp.getY1() - dy);

                        Point startPointMn = new Point(x1, y1);
                        Point endPointMn = new Point(nx2, ny2);
                        Point startPointMp = new Point(px1, py1);
                        Point endPointMp = new Point(px2, py2);

                        Point endPoint = new Point(x2, y2);
                        if (shapeMn != null) {
                            shapeMn.setLine(startPointMn, endPointMn);
                            prevPoint = e.getPoint();
                            repaint();
                        }
                        if (shapeMp != null) {
                            shapeMp.setLine(startPointMp, endPointMp);
                            prevPoint = e.getPoint();
                            repaint();
                        }
                    }
                    @Override
                    public void mouseReleased(MouseEvent e) {
                        repaint();
                    }
                };
                addMouseListener(mouseAdapter);
                addMouseMotionListener(mouseAdapter);
            }

            public void paint(Graphics g) {
                super.paint(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setStroke(new java.awt.BasicStroke(2));
                g2d.drawRect(100,100,300,150);
            }

            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setPaint(Color.BLUE);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
                if (lineMn != null){
                    g2d.draw(lineMn);
                    g2d.draw(lineMp);
                    }
            }};
        f.add(p);
        p.setLayout(null);
        f.setVisible(true);

    }
}

您可以尝试使用图像而不是线条并使用 WASD。我的游戏使用那个。

ourFrame.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}

@Override
public void keyPressed(KeyEvent e) {
    System.out.println("Key pressed code=" + e.getKeyCode() + ", char=" + e.getKeyChar());
    push = e.getKeyCode();

    if(push == 87){//w
         if(y > -9){//blocker
         picLabel.setBounds(x, y, 80, 40);
         y  = y + -10;
        // System.out.println("Cubie is going up at, " + x + "," + y);

    }}
    if(push == 65){//a
         if(x > -29){//blocker
         picLabel.setBounds(x, y, 80, 40);
         x  = x + -10;
        // System.out.println("Cubie is going left at, " + x + "," + y);
    }}
    if(push == 83){//s
         if(y < 421){//blocker
         picLabel.setBounds(x, y, 80, 40);
         y  = y + 10;
        // System.out.println("Cubie is going down at, " + x + "," + y);
    }}
    if(push == 68){//d
         if(x < 621){//blocker
         picLabel.setBounds(x, y, 80, 40);

        //System.out.println("Cubie is going right at, " + x + "," + y);
         x  = x + 10;
    }}

有几种方法 "might" 可以做到这一点,但对我来说,我会尝试将两条线组合成一个形状,这样您就可以计算出它们的连接框,然后执行一个简单的命中测试

例如...

@Override
public void mouseDragged(MouseEvent e) {

    GeneralPath gp = new GeneralPath();
    gp.append(lineMn, false);
    gp.append(lineMp, false);

    Point p = e.getPoint();

    if (!gp.getBounds().contains(p)) {
        return;
    }

现在,请注意,"this" 实施效率不高。相反,我会尽早将线条组合成一个形状,然后继续移动它。