![](/img/trans.png)
[英]Eclipse plugin development - drag and drop between components in multiple plugins
[英]Drag and Drop Multiple/Different Components
這是我的程序代碼。 你可以提供一些技巧,讓其他矩形像第一個一樣移動
注意:第一個矩形是唯一可以拖放的矩形
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);
}
}
}
各種幫助將不勝感激:)
第一個Rectangle是唯一可以拖動的,因為你的類中有硬編碼邏輯,它總是引用第一個Rectangle。
你需要改變你班級的整個設計:
不要硬編碼矩形。 您將需要一個ArrayList來跟蹤所有Rectangle和每個Rectangle的顏色
在您的MouseListener代碼中,您將需要遍歷此List以通過使用Rectangle的contains(...)方法和MouseEvent中的鼠標點來找出您單擊的Rectangle。
一旦找到單擊的Rectangle,您將需要更改代碼以對此Rectangle進行拖動,而不是現在使用的硬編碼“和”變量。
需要更改paintComponent()
邏輯以迭代ArrayList以繪制每個Rectangle。 在嘗試修復拖動問題之前,應首先重新構建此部分代碼。 您可能需要查看“ 使用形狀”以了解如何從ArrayList中繪制Shape對象的一些想法。
可能還有其他問題,但希望這會讓你開始朝着正確的方向前進。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.