Draw2d 拖拽 Drag and Drop

关键字: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的大小位置了。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值