SWT ScrolledComposite Smooth Scrolling

lumo

Top Contributor
Hallo,

ich versuche gerade das scrollen einer ScrolledComposite so zu implementieren wies auch in photoshop ist, um ein bild hin und her zu schieben...

das schieben geht so weit ganz gut, aber irgendwie ruckelt das tierisch...

wenn jemand weiss, was ich da falsch mache, bzw wie man das richtig macht...

hab ein kleines lauffähiges testprogramm geschrieben und unten angehängt.
ihr müsst nur noch den pfad zu einer großen bilddatei angeben

hier der code, oder unten als download:

Java:
package org.javaforum;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;

public class Sample {

	protected Shell shell;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Sample window = new Sample();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(450, 300);
		shell.setText("SWT Application");
		shell.setLayout(new GridLayout(1, false));
		
		ScrolledComposite sc = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		sc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		sc.setExpandHorizontal(true);
		sc.setExpandVertical(true);
		
		MoveMap moveMap = new MoveMap(sc, SWT.NONE);
		sc.setContent(moveMap);
		sc.setMinSize(moveMap.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		
		final Image image = new Image(Display.getDefault(), "Path to large image file here");
		
		moveMap.addPaintListener(new PaintListener() {

			@Override
			public void paintControl(PaintEvent e) {
				e.gc.drawImage(image, 0, 0);
			}
		});
		sc.setContent(moveMap);
		// Set the minimum size
		sc.setMinSize(image.getBounds().width,
				image.getBounds().height);

	}

}

Java:
package org.javaforum;

import lumo.utils.CursorCache;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;

public class MoveMap extends Composite implements MouseListener,
		MouseMoveListener {

	private boolean moveMap = false;
	private Point startPos = new Point(0, 0);
	private Point startOrigin = new Point(0, 0);

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public MoveMap(Composite parent, int style) {
		super(parent, style);
		super.addMouseListener(this);
		super.addMouseMoveListener(this);
		setCursor(CursorCache.getCursor(SWT.CURSOR_CROSS));
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	@Override
	public void mouseDoubleClick(MouseEvent e) {
	}

	@Override
	public void mouseDown(MouseEvent e) {
		setCursor(CursorCache.getCursor(SWT.CURSOR_SIZEALL));
		moveMap = e.button == 2; // middle mouse button aka mousewheel
//		System.out.println("movemap = " + moveMap);
		startPos.x = e.x;
		startPos.y = e.y;
		startOrigin = ((ScrolledComposite) getParent()).getOrigin();
	}

	@Override
	public void mouseUp(MouseEvent e) {
		moveMap = false;
		setCursor(CursorCache.getCursor(SWT.CURSOR_CROSS));
	}

	@Override
	public void mouseMove(MouseEvent e) {
		if (moveMap) {
			Point moved = new Point(startPos.x - e.x, startPos.y - e.y);
			moved.x += startOrigin.x;
			moved.y += startOrigin.y;
			ScrolledComposite sc = (ScrolledComposite) getParent();
			sc.setOrigin(moved);
			// middlemouse
		}
	}
}

lumo
 

Anhänge

  • javaforum.zip
    1,7 KB · Aufrufe: 5

lumo

Top Contributor
habs mittlerweile gelöst!
falls noch jemand das problem hat... hier der lösungscode (sogar mit pfeiltasten ;) )
Java:
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;

public class MoveMap extends Composite implements MouseListener,
		MouseMoveListener, KeyListener {

	private boolean moveMap = false;
	private Point startPos = new Point(0, 0);
	private Point startOrigin = new Point(0, 0);

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public MoveMap(Composite parent, int style) {
		super(parent, style);
		super.addMouseListener(this);
		super.addMouseMoveListener(this);
		super.addKeyListener(this);
		setCursor(CursorCache.getCursor(SWT.CURSOR_CROSS));
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	@Override
	public void mouseDoubleClick(MouseEvent e) {
	}

	@Override
	public void mouseDown(MouseEvent e) {
		moveMap = e.button == 2; // middle mouse button aka mousewheel
		if (moveMap) {
			setCursor(CursorCache.getCursor(SWT.CURSOR_SIZEALL));
			startPos.x = e.x;
			startPos.y = e.y;
			startOrigin = ((ScrolledComposite) getParent()).getOrigin();
		}
	}

	@Override
	public void mouseUp(MouseEvent e) {
		moveMap = false;
		setCursor(CursorCache.getCursor(SWT.CURSOR_CROSS));
	}

	@Override
	public void mouseMove(MouseEvent e) {
		if (moveMap) {
			move(startPos.x - e.x, startPos.y - e.y);
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		switch (e.keyCode) {
		case SWT.ARROW_DOWN:
			move(0, 5);
			break;
		case SWT.ARROW_UP:
			move(0, -5);
			break;
		case SWT.ARROW_LEFT:
			move(-5, 0);
			break;
		case SWT.ARROW_RIGHT:
			move(5, 0);
			break;

		default:
			break;
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
	}

	public void move(int x, int y) {
		ScrolledComposite sc = (ScrolledComposite) getParent();
		Point targetOrigin = sc.getOrigin();
		targetOrigin.x += x;
		targetOrigin.y += y;
		sc.setOrigin(targetOrigin);
	}
}
 

Ähnliche Java Themen


Oben