Draw2d 拖拽 Drag and Drop
  sAAkk3Vxfaa8 2023年11月02日 43 0


关键字:Draw2d 拖拽 Drag and Drop

 

public class Scroller2 {
	IFigure getRootFigure() {
		Panel panel = new Panel();
		panel.setLayoutManager(new XYLayout());
		RectangleFigure rFigure = new RectangleFigure();
		rFigure.setSize(55,55);
		rFigure.setBackgroundColor(ColorConstants.green);
		new Dnd(rFigure);
		panel.add(rFigure);
		return panel;
	}

	public static void main(String args[]) {
		Display display = Display.getDefault();
		Shell shell = new Shell();  
		shell.setSize(400, 300);
		shell.open();
		shell.setText("ScrollPane Example");
		LightweightSystem lws = new LightweightSystem(shell);
		lws.setContents(new Scroller2().getRootFigure());
		while (!shell.isDisposed ()) {
			if (!display.readAndDispatch ())
				display.sleep ();
		}
	}
}
class Dnd extends MouseMotionListener.Stub implements MouseListener {
	Point start;
	public Dnd(IFigure figure) 	{
		figure.addMouseMotionListener(this);
		figure.addMouseListener(this);
	}
	public void mouseReleased(MouseEvent e){
		Figure f = ((Figure)e.getSource());
		f.setCursor(null);
	}
	public void mouseClicked(MouseEvent e){}
	public void mouseDoubleClicked(MouseEvent e){}
	public void mousePressed(MouseEvent e) {
		Figure f = ((Figure)e.getSource());
		f.setCursor(Display.getCurrent().getSystemCursor(SWT.CURSOR_SIZEALL));
		start = e.getLocation();
	}
	public void mouseDragged(MouseEvent e) {
		if(start == null) {
			return;
		}
		Point p = e.getLocation();
		Dimension d = p.getDifference(start);
		start = p;
		Figure f = ((Figure)e.getSource());
		f.setBounds(f.getBounds().getTranslated(d.width, d.height));
	}
}

 

 


 

 

关于拖拽有个奇怪的问题:

有连线 + add(IFigure figure, Object constraint)的方式加的图形。不好拖拽,可能因为constraint。

public class HelloWorld2 {
	public static void main(String args[]) {
		Shell shell = new Shell();
		shell.setText("Draw2d Hello World");
		shell.setSize(300, 300);
		shell.open();

		// create content 4 shell.
		createContent4Shell(shell);

		while (!shell.isDisposed ()) {
			if (!Display.getDefault().readAndDispatch ())
				Display.getDefault().sleep ();
		}
	}

	private static void createContent4Shell(Shell shell) {
		Panel rootFigure = new Panel();
		rootFigure.setLayoutManager(new XYLayout());

		IFigure figure1 = new Ellipse();
		Ellipse figure2 = new Ellipse();

		// --------------------------------------------------------
		// add connection
		PolylineConnection connection = new PolylineConnection();
		connection.setSourceAnchor(new ChopboxAnchor(figure1));
		connection.setTargetAnchor(new EllipseAnchor(figure2));

		rootFigure.add(connection);
		
//		figure1.setBounds(new Rectangle(10,10,60,30));
//		figure2.setBounds(new Rectangle(170,170,90,90));
//		rootFigure.add(figure1);
//		rootFigure.add(figure2);
		
		rootFigure.add(figure2,new Rectangle(170,170,90,90));
		rootFigure.add(figure1,new Rectangle(10,10,60,30));
		
		new Dnd(figure1);
		new Dnd(figure2);

		LightweightSystem lws = new LightweightSystem(shell);
		lws.setContents(rootFigure);
	}
}

class Dnd extends MouseMotionListener.Stub implements MouseListener{
	Point start;
	public Dnd(IFigure figure) {
		figure.addMouseMotionListener(this);
		figure.addMouseListener(this);
	}
	public void mouseReleased(MouseEvent e) {
		Figure f = ((Figure)e.getSource());
		f.setCursor(null);
	}
	public void mouseClicked(MouseEvent e) {}
	public void mouseDoubleClicked(MouseEvent e) {}
	public void mousePressed(MouseEvent e) {
		Figure f = ((Figure)e.getSource());
		f.setCursor(Display.getCurrent().getSystemCursor(SWT.CURSOR_SIZEALL));
		start = e.getLocation();
	}
	public void mouseDragged(MouseEvent e) {
		if(start == null) {
			return;
		}
		Point p = e.getLocation();
		Dimension d = p.getDifference(start);
		start = p;
		Figure f = ((Figure)e.getSource());
		f.setBounds(f.getBounds().getTranslated(d.width, d.height));
	}
}

 

 


 

 

但改为:

 

figure1.setBounds(new Rectangle(10,10,60,30));
		figure2.setBounds(new Rectangle(170,170,90,90));
		rootFigure.add(figure1);
		rootFigure.add(figure2);
		
//		rootFigure.add(figure2,new Rectangle(170,170,90,90));
//		rootFigure.add(figure1,new Rectangle(10,10,60,30));

 就可以拖动。可能是因为约束的原因。
 

 

看一下源代码可以看出区别,

public final void add(IFigure figure) {
		add(figure, null, -1);
	}

 而

public final void add(IFigure figure, Object constraint) {
		add(figure, constraint, -1);
	}

 也就说约束的方式就定死了figure的大小位置了。 

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
sAAkk3Vxfaa8