gpt4 book ai didi

java - Java中的拖拽线

转载 作者:行者123 更新时间:2023-12-02 09:10:59 24 4
gpt4 key购买 nike

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

}
}

最佳答案

您“可能”有几种方法可以做到这一点,但对我来说,我会尝试将两条线组合成一个形状,这样您就可以计算它们的粘合框,然后进行简单的 HitTest

例如...

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

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

关于java - Java中的拖拽线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59418429/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com