我想实现一个“手工工具”,用户可以使用鼠标滚动/移动某些东西。此功能可从各种程序中获知,例如Gimp。如果用户按下鼠标左键,则应激活该工具。如果现在移动鼠标,则还应将基础对象 - 比例移动到鼠标移动。因此,如果用户按下鼠标并通过例如移动它来移动鼠标。 10px,底层对象也应移动10px。我认为它应该适用于我下面的代码,但它无法正常工作。基础ScrolledComposite
的移动太快了。
以下代码已准备就绪,因此您可以直接使用它。
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class HandTool {
public static boolean moveGraphActive = false;
public static Point moveStartPos = new Point(0, 0);
public static ScrolledComposite sc;
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
sc = new ScrolledComposite(shell, SWT.V_SCROLL);
Text text = new Text(sc, SWT.MULTI | SWT.READ_ONLY);
String multiLineText = "";
for (int i = 0; i < 30; i++) {
multiLineText += "Hello World SWT " + i + System.lineSeparator();
}
text.setText(multiLineText);
text.setEnabled(false);
text.pack();
sc.setContent(text);
sc.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(final MouseEvent e) {
moveGraphActive = e.button == 1;
if (moveGraphActive) {
moveStartPos.x = e.x;
moveStartPos.y = e.y;
}
}
@Override
public void mouseUp(final MouseEvent e) {
moveGraphActive = false;
}
});
sc.addMouseMoveListener(new MouseMoveListener() {
@Override
public void mouseMove(final MouseEvent e) {
if (moveGraphActive) {
// FIXME the movement of the canvas should be proportional
// to the mouse
// movement
final int newX = sc.getOrigin().x + moveStartPos.x - e.x;
final int newY = sc.getOrigin().y + moveStartPos.y - e.y;
sc.setOrigin(newX, newY);
}
}
});
shell.setSize(500, 150);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
答案 0 :(得分:2)
你几乎做对了,只有一个小错误。
final int newX = sc.getOrigin().x + moveStartPos.x - e.x;
final int newY = sc.getOrigin().y + moveStartPos.y - e.y;
应该是
final int newX = startOrigin.x + moveStartPos.x - e.x;
final int newY = startOrigin.y + moveStartPos.y - e.y;
其中startOrigin
是您开始拖动时ScrolledComposite
的来源。我已经更新了你的代码:
private static boolean moveGraphActive = false;
private static Point moveStartPos = new Point(0, 0);
private static ScrolledComposite sc;
private static Point startOrigin;
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
sc = new ScrolledComposite(shell, SWT.V_SCROLL);
Text text = new Text(sc, SWT.MULTI | SWT.READ_ONLY);
String multiLineText = "";
for (int i = 0; i < 30; i++)
{
multiLineText += "Hello World SWT " + i + System.lineSeparator();
}
text.setText(multiLineText);
text.setEnabled(false);
text.pack();
sc.setContent(text);
sc.addMouseListener(new MouseAdapter()
{
@Override
public void mouseDown(final MouseEvent e)
{
moveGraphActive = e.button == 1;
if (moveGraphActive)
{
moveStartPos.x = e.x;
moveStartPos.y = e.y;
startOrigin = sc.getOrigin();
}
}
@Override
public void mouseUp(final MouseEvent e)
{
moveGraphActive = false;
startOrigin = null;
}
});
sc.addMouseMoveListener(new MouseMoveListener()
{
@Override
public void mouseMove(final MouseEvent e)
{
if (moveGraphActive)
{
// FIXED
final int newX = startOrigin.x + moveStartPos.x - e.x;
final int newY = startOrigin.y + moveStartPos.y - e.y;
sc.setOrigin(newX, newY);
}
}
});
shell.setSize(500, 150);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}