如何将数字作为连接的一部分时移动
问题描述:
我正在使用Draw2d库(不使用GEF)处理小型图形编辑器。一个要求是,你可以通过拖动鼠标来移动图形。只要数字之间没有(折线)连接,这就可以正常工作。当我添加连接时,所有渲染都正确,但不可能移动数字。如何将数字作为连接的一部分时移动
下面是一个代码示例,说明此问题:
public class Main {
public static void main(String[] args) {
Display d = new Display();
final Shell shell = new Shell(d);
shell.setSize(400, 400);
shell.setText("Draw2d Test");
LightweightSystem lws = new LightweightSystem(shell);
Figure contents = new Figure();
XYLayout contentsLayout = new XYLayout();
contents.setLayoutManager(contentsLayout);
// create figures
Figure f1 = new TestFigure("Test 1");
Figure f2 = new TestFigure("Test 2");
MouseManager mm = new MouseManager();
// register mouse listeners
f1.addMouseMotionListener(mm);
f1.addMouseListener(mm);
f2.addMouseMotionListener(mm);
f2.addMouseListener(mm);
// set constraints to layout manager
contentsLayout.setConstraint(f1, new Rectangle(10, 10, -1, -1));
contentsLayout.setConstraint(f2, new Rectangle(200, 200, -1, -1));
// add to layout manager
contents.add(f1);
contents.add(f2);
// add connection
// When uncommenting these lines, dragging works fine
PolylineConnection c = new PolylineConnection();
c.setSourceAnchor(new ChopboxAnchor(f1));
c.setTargetAnchor(new ChopboxAnchor(f2));
c.setConnectionRouter(new ManhattanConnectionRouter());
contents.add(c);
lws.setContents(contents);
shell.open();
while (!shell.isDisposed()) {
while (!d.readAndDispatch()) {
d.sleep();
}
}
}
}
class MouseManager implements MouseMotionListener, MouseListener {
Figure selection;
private Point lastDragLocation;
@Override
public void mousePressed(MouseEvent me) {
System.out.println("mouse pressed");
selection = (Figure) me.getSource();
}
@Override
public void mouseReleased(MouseEvent me) {
System.out.println("mouse released");
selection = null;
lastDragLocation = null;
}
@Override
public void mouseDragged(MouseEvent me) {
if (lastDragLocation != null && selection != null) {
int offsetX = me.getLocation().x - lastDragLocation.x;
int offsetY = me.getLocation().y - lastDragLocation.y;
int newX = selection.getLocation().x + offsetX;
int newY = selection.getLocation().y + offsetY;
System.out.println(String.format("NewX: %d, NewY: %d", newX, newY));
selection.setBounds(selection.getBounds().getTranslated(offsetX,
offsetY));
}
lastDragLocation = me.getLocation();
}
// [removed empty implementations of the interface for this post]
}
class TestFigure extends RectangleFigure {
public Color classColor;
public TestFigure(String name) {
ToolbarLayout layout = new ToolbarLayout();
setLayoutManager(layout);
setOpaque(true);
classColor = new Color(null, 255, 255, 206);
setBackgroundColor(classColor);
Label lbl_name = new Label(name);
add(lbl_name);
}
@Override
protected void finalize() throws Throwable {
classColor.dispose();
super.finalize();
}
}
有没有人有一个想法如何使拖动时可能存在(它时并不需要渲染的拖动两个数字之间的连接连接)?
答
两个问题:
- 在
mouseDragged
功能要更改Figure
的界限,而不是改变在父容器图的约束。 - 您不重新验证父项。
我做了如下的变化和它的工作原理:
public void mouseDragged(MouseEvent me) {
if(lastDragLocation != null && selection != null) {
int offsetX = me.getLocation().x - lastDragLocation.x;
int offsetY = me.getLocation().y - lastDragLocation.y;
int newX = selection.getLocation().x + offsetX;
int newY = selection.getLocation().y + offsetY;
System.out.println(String.format("NewX: %d, NewY: %d", newX, newY));
// selection.setBounds(selection.getBounds().getTranslated(offsetX, offsetY)); <-- this does not work
selection.getParent().getLayoutManager()
.setConstraint(selection, selection.getBounds().getTranslated(offsetX, offsetY));
selection.getParent().revalidate();
}
lastDragLocation = me.getLocation();
}
但我仍然认为这是与实施问题,因为如果你把鼠标移动速度太快不知为何,你能设法让开图,它停止移动。我会做的是听父母图中的鼠标,捕捉鼠标开始移动到内部图形的顶部(使用父母Figure.findFigureAt()
),然后随着鼠标移动移动内部图形。