运行了draw2d的一个例子,有两个节点和一条连线,程序中能够实现鼠标拖动节点的功能,但运行时却没有效果,请各位牛人帮我看一下,是不是程序本身有问题??
多谢
程序如下:
import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.MidpointLocator;
import org.eclipse.draw2d.MouseEvent;
import org.eclipse.draw2d.MouseListener;
import org.eclipse.draw2d.MouseMotionListener;
import org.eclipse.draw2d.PolygonDecoration;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class example {
public static void main(String args[]){
Shell shell=new Shell();
shell.setSize(350, 360);
shell.open();
shell.setText("demo");
LightweightSystem lws=new LightweightSystem(shell);
IFigure panel=new Figure();
lws.setContents(panel);
RectangleFigure
node1=new RectangleFigure(),
node2=new RectangleFigure();
node1.setBackgroundColor(ColorConstants.red);
node1.setBounds(new Rectangle(30,30,64,36));
node2.setBounds(new Rectangle(100,100,64,36));
PolylineConnection conn=new PolylineConnection();
conn.setSourceAnchor(new ChopboxAnchor(node1));
conn.setTargetAnchor(new ChopboxAnchor(node2));
conn.setTargetDecoration(new PolygonDecoration());
Label label=new Label("Midpoint");
label.setOpaque(true);
label.setBackgroundColor(ColorConstants.buttonLightest);
label.setBorder(new LineBorder());
conn.add(label,new MidpointLocator(conn, 0));
panel.add(node1);
panel.add(node2);
panel.add(conn);
new Dragger(node1);
new Dragger(node2);
Display display=Display.getDefault();
while(!shell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
}
static class Dragger extends MouseMotionListener.Stub implements MouseListener{
public Dragger(IFigure figure){
figure.addMouseMotionListener(this);
figure.addMouseListener(this);
}
Point last;
public void mouseReleased(MouseEvent e){
last=null;
}
public void mouseClicked(MouseEvent e){
}
public void mouseDoubleClicked(MouseEvent e){
System.out.println("c");
}
public void mousePressed(MouseEvent e){
last=e.getLocation();
e.consume();
System.out.println("a");
}
public void mouseDragger(MouseEvent e){
// if(last==null)
// return;
System.out.println("b");
Point p=e.getLocation();
Dimension delta=p.getDifference(last);
last=p;
Figure f=((Figure)e.getSource());
f.setBounds(f.getBounds().getTranslated(delta.width, delta.height));
}
};
}
中间增加的输出都是测试的,我鼠标拖动时发现程序根本就没有进入mousedragger方法,不知道为何
多谢
程序如下:
import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.MidpointLocator;
import org.eclipse.draw2d.MouseEvent;
import org.eclipse.draw2d.MouseListener;
import org.eclipse.draw2d.MouseMotionListener;
import org.eclipse.draw2d.PolygonDecoration;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class example {
public static void main(String args[]){
Shell shell=new Shell();
shell.setSize(350, 360);
shell.open();
shell.setText("demo");
LightweightSystem lws=new LightweightSystem(shell);
IFigure panel=new Figure();
lws.setContents(panel);
RectangleFigure
node1=new RectangleFigure(),
node2=new RectangleFigure();
node1.setBackgroundColor(ColorConstants.red);
node1.setBounds(new Rectangle(30,30,64,36));
node2.setBounds(new Rectangle(100,100,64,36));
PolylineConnection conn=new PolylineConnection();
conn.setSourceAnchor(new ChopboxAnchor(node1));
conn.setTargetAnchor(new ChopboxAnchor(node2));
conn.setTargetDecoration(new PolygonDecoration());
Label label=new Label("Midpoint");
label.setOpaque(true);
label.setBackgroundColor(ColorConstants.buttonLightest);
label.setBorder(new LineBorder());
conn.add(label,new MidpointLocator(conn, 0));
panel.add(node1);
panel.add(node2);
panel.add(conn);
new Dragger(node1);
new Dragger(node2);
Display display=Display.getDefault();
while(!shell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
}
static class Dragger extends MouseMotionListener.Stub implements MouseListener{
public Dragger(IFigure figure){
figure.addMouseMotionListener(this);
figure.addMouseListener(this);
}
Point last;
public void mouseReleased(MouseEvent e){
last=null;
}
public void mouseClicked(MouseEvent e){
}
public void mouseDoubleClicked(MouseEvent e){
System.out.println("c");
}
public void mousePressed(MouseEvent e){
last=e.getLocation();
e.consume();
System.out.println("a");
}
public void mouseDragger(MouseEvent e){
// if(last==null)
// return;
System.out.println("b");
Point p=e.getLocation();
Dimension delta=p.getDifference(last);
last=p;
Figure f=((Figure)e.getSource());
f.setBounds(f.getBounds().getTranslated(delta.width, delta.height));
}
};
}
中间增加的输出都是测试的,我鼠标拖动时发现程序根本就没有进入mousedragger方法,不知道为何