gpt4 book ai didi

java - 拖放多个/不同的组件

转载 作者:行者123 更新时间:2023-12-02 06:32:11 25 4
gpt4 key购买 nike

这是我的程序的代码。你能给一些技巧让其他矩形可以像第一个矩形一样移动

注意:第一个矩形是唯一可以拖放的

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

public class GraphicDragAndDrop extends JPanel {

Rectangle and = new Rectangle(5,5,75,75);
Rectangle or = new Rectangle(5,105,75,75);
Rectangle xnor = new Rectangle(5,205,75,75);
Rectangle nand = new Rectangle(5,305,75,75);
Rectangle xor = new Rectangle(5,405,75,75);
Rectangle inverter = new Rectangle(5,505,75,75);

protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.blue);
g2.draw(and);

Graphics2D g3 = (Graphics2D)g;
g3.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g3.setPaint(Color.red);
g3.draw(or);

Graphics2D g4 = (Graphics2D)g;
g4.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g4.setPaint(Color.blue);
g4.draw(xnor);

Graphics2D g5 = (Graphics2D)g;
g5.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g5.setPaint(Color.red);
g5.draw(nand);

Graphics2D g6 = (Graphics2D)g;
g6.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g6.setPaint(Color.blue);
g6.draw(xor);


Graphics2D g7 = (Graphics2D)g;
g7.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g7.setPaint(Color.red);
g7.draw(inverter);
}

public void setRect(int x, int y) {

and.setLocation(x, y);
repaint();

}

public static void main(String[] args) {
GraphicDragAndDrop test = new GraphicDragAndDrop();
new GraphicDragController(test);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(test);
f.setSize(400,400);
f.setLocation(100,100);
f.setVisible(true);
}
}

class GraphicDragController extends MouseInputAdapter {
GraphicDragAndDrop component;
Point offset = new Point();
boolean dragging = false;

public GraphicDragController(GraphicDragAndDrop gdad) {
component = gdad;
component.addMouseListener(this);
component.addMouseMotionListener(this);
}

public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
Rectangle r = component.and;
Rectangle a = component.or;
if(r.contains(p)) {
offset.x = p.x - r.x;
offset.y = p.y - r.y;
dragging = true;
}
}
public void mouseReleased(MouseEvent e) {
dragging = false;
}

public void mouseDragged(MouseEvent e) {
if(dragging) {
int x = e.getX() - offset.x;
int y = e.getY() - offset.y;
component.setRect(x, y);
}


}
}

各种帮助将不胜感激,谢谢:)

最佳答案

第一个矩形是唯一可以拖动的,因为您的类中具有始终引用第一个矩形的硬编码逻辑。

您需要更改类的整体设计:

  1. 不要对矩形进行硬编码。您将需要一个 ArrayList 来跟踪所有矩形以及每个矩形的颜色

  2. 在 MouseListener 代码中,您需要循环访问此列表,以使用 Rectangle 的 contains(...) 方法和 MouseEvent 中的鼠标点来找出您单击了哪个 Rectangle。

  3. 找到被单击的矩形后,您将需要更改代码以在该矩形上进行拖动,而不是您现在使用的硬编码“和”变量。

    <
  4. 需要更改 paintComponent() 逻辑,以迭代 ArrayList 来绘制每个矩形。在尝试解决拖动问题之前,您应该首先重构这部分代码。您可能想查看Playing With Shapes了解如何从 ArrayList 绘制 Shape 对象的一些想法。

可能还有其他问题,但希望这能让您朝着正确的方向开始。

关于java - 拖放多个/不同的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19969495/

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