简体   繁体   English

拖放多个/不同的组件

[英]Drag and Drop Multiple/Different Components

This the code of my program. 这是我的程序代码。 can you give some tips to make the other rectangles can move like the 1st one 你可以提供一些技巧,让其他矩形像第一个一样移动

Note: The 1st Rectangle is the only one can be drag and drop 注意:第一个矩形是唯一可以拖放的矩形

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


    }
}

All kinds of help will be appreciated Thanks :) 各种帮助将不胜感激:)

The first Rectangle is the only one that can be dragged because you have hard coded logic in your class that always refers to the first Rectangle. 第一个Rectangle是唯一可以拖动的,因为你的类中有硬编码逻辑,它总是引用第一个Rectangle。

You need to change the whole design of your class: 你需要改变你班级的整个设计:

  1. Don't hard code the rectangles. 不要硬编码矩形。 You will need an ArrayList to keep track of all the Rectangles and the color of each Rectangle 您将需要一个ArrayList来跟踪所有Rectangle和每个Rectangle的颜色

  2. In you MouseListener code you will then need to iterate through this List to find out which Rectangle you clicked on by using the contains(...) method of the Rectangle and the mouse point from the MouseEvent. 在您的MouseListener代码中,您将需要遍历此List以通过使用Rectangle的contains(...)方法和MouseEvent中的鼠标点来找出您单击的Rectangle。

  3. Once you find the Rectangle that was clicked you will then need to change your code to do the dragging on this Rectangle, not the hard coded "and" variable that you are using now. 一旦找到单击的Rectangle,您将需要更改代码以对此Rectangle进行拖动,而不是现在使用的硬编码“和”变量。

  4. The paintComponent() logic will need to be changed to iterate through the ArrayList to paint each Rectangle. 需要更改paintComponent()逻辑以迭代ArrayList以绘制每个Rectangle。 You should get this part of your code restructured first before you attempt to fix the dragging problem. 在尝试修复拖动问题之前,应首先重新构建此部分代码。 You may want to check out Playing With Shapes for some ideas of how to paint Shape objects from an ArrayList. 您可能需要查看“ 使用形状”以了解如何从ArrayList中绘制Shape对象的一些想法。

There are probably other problems but hopefully that will get you started in the right direction. 可能还有其他问题,但希望这会让你开始朝着正确的方向前进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM