java swt图形处理程序

鹏割 2016-10-17 10:12:58

如图,用的是SWTImageCanvas,里面基本功能都实现了,但是画板里面只能处理一张图片,有没有什么好的办法,能够在一个画板里显示多张图片,并且滚动条保持作用,就像pdf阅读器那样,求大家帮忙啊,谢谢
...全文
198 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
鹏割 2016-10-17
  • 打赏
  • 举报
回复
[code=java/** * Get the image data. (for future use only) * @return image data of canvas */ public ImageData getImageData() { return sourceImage.getImageData(); } /** * Reset the image data and update the image * @param data image data to be set */ public void setImageData(ImageData data) { if (sourceImage != null) sourceImage.dispose(); if (data != null) sourceImage = new Image(getDisplay(), data); syncScrollBars(); } /** * Fit the image onto the canvas */ public void fitCanvas() { if (sourceImage == null) return; Rectangle imageBound = sourceImage.getBounds(); Rectangle destRect = getClientArea(); double sx = (double) destRect.width / (double) imageBound.width; double sy = (double) destRect.height / (double) imageBound.height; double s = Math.min(sx, sy); double dx = 0.5 * destRect.width; double dy = 0.5 * destRect.height; centerZoom(dx, dy, s, new AffineTransform()); } /** * Show the image with the original size */ public void showOriginal() { if (sourceImage == null) return; transform = new AffineTransform(); syncScrollBars(); } /** * Perform a zooming operation centered on the given point * (dx, dy) and using the given scale factor. * The given AffineTransform instance is preconcatenated. * @param dx center x * @param dy center y * @param scale zoom rate * @param af original affinetransform */ public void centerZoom( double dx, double dy, double scale, AffineTransform af) { af.preConcatenate(AffineTransform.getTranslateInstance(-dx, -dy)); af.preConcatenate(AffineTransform.getScaleInstance(scale, scale)); af.preConcatenate(AffineTransform.getTranslateInstance(dx, dy)); transform = af; syncScrollBars(); } /** * Zoom in around the center of client Area. */ public void zoomIn() { if (sourceImage == null) return; Rectangle rect = getClientArea(); int w = rect.width, h = rect.height; double dx = ((double) w) / 2; double dy = ((double) h) / 2; centerZoom(dx, dy, ZOOMIN_RATE, transform); } /** * Zoom out around the center of client Area. */ public void zoomOut() { if (sourceImage == null) return; Rectangle rect = getClientArea(); int w = rect.width, h = rect.height; double dx = ((double) w) / 2; double dy = ((double) h) / 2; centerZoom(dx, dy, ZOOMOUT_RATE, transform); } /** * Reset zoom and centre. */ public void resetZoom() { if (sourceImage == null) return; Rectangle rect = getClientArea(); int w = rect.width, h = rect.height; double dx = ((double) w) / 2; double dy = ((double) h) / 2; double scale = 1.0/transform.getScaleX(); centerZoom(dx, dy, scale, transform); } public AffineTransform getTransform() { return transform; } @Override public void mouseScrolled(MouseEvent e) { if ((e.stateMask & SWT.MOD1) == 0) { //No CTRL ScrollBar scrollBar = getVerticalBar(); scrollBar.setSelection(scrollBar.getSelection()-e.count*10); scrollVertically(scrollBar); } } @Override public void mouseDoubleClick(MouseEvent e) { } @Override public void mouseDown(MouseEvent e) { mouseReferencePoint = new Point(e.x, e.y); } @Override public void mouseUp(MouseEvent e) { mouseReferencePoint = null; } @Override public void mouseMove(MouseEvent e) { if (sourceImage == null) return; Point p = mouseReferencePoint; if (p != null) { //Scroll AffineTransform af = transform; double dx = e.x-mouseReferencePoint.x; double dy = e.y-mouseReferencePoint.y; af.preConcatenate(AffineTransform.getTranslateInstance(dx, dy)); transform = af; syncScrollBars(); mouseReferencePoint.x = e.x; mouseReferencePoint.y = e.y; } } } ][/code]
鹏割 2016-10-17
  • 打赏
  • 举报
回复
/**
 * A scrollable image canvas that extends org.eclipse.swt.graphics.Canvas.
 * <p/>
 * It requires Eclipse (version >= 2.1) on Win32/win32; Linux/gtk; MacOSX/carbon.
 * <p/>
 * This implementation using the pure SWT, no UI AWT package is used. For 
 * convenience, I put everything into one class. However, the best way to
 * implement this is to use inheritance to create multiple hierarchies.
 * 
 * @author Chengdong Li: cli4@uky.edu
 * @author Christian Clausner: Added panning by mouse dragging
 */
public class SWTImageCanvas extends Canvas implements MouseWheelListener, MouseListener, MouseMoveListener {
	/* zooming rates in x and y direction are equal.*/
	final float ZOOMIN_RATE = 1.1f; /* zoomin rate */
	final float ZOOMOUT_RATE = 0.9f; /* zoomout rate */
	private Image sourceImage; /* original image */
	private Image screenImage; /* screen image */
	private AffineTransform transform = new AffineTransform(); //支持滚动和缩放
	private Point mouseReferencePoint = null;
	private Color grey;

	private String currentDir=""; /* remembering file open directory */

	public SWTImageCanvas(final Composite parent) {
		this(parent, SWT.NULL);
		grey = new Color(parent.getDisplay(), 85, 85, 85);
	}

	/**
	 * Constructor for ScrollableCanvas.
	 * @param parent the parent of this control.
	 * @param style the style of this control.
	 */
	public SWTImageCanvas(final Composite parent, int style) {
		super( parent, style|SWT.BORDER|SWT.V_SCROLL|SWT.H_SCROLL
				            | SWT.NO_BACKGROUND);
		grey = new Color(parent.getDisplay(), 85, 85, 85);
		addControlListener(new ControlAdapter() { /* resize listener. */
			public void controlResized(ControlEvent event) {
				syncScrollBars();
			}
		});
		addPaintListener(new PaintListener() { /* paint listener. */
			public void paintControl(final PaintEvent event) {
				paint(event.gc);
			}
		});
		initScrollBars();
		addMouseListener(this);
		addMouseMoveListener(this);
	}

	/**
	 * Dispose the garbage here
	 */
	public void dispose() {
		if (sourceImage != null && !sourceImage.isDisposed()) {
			sourceImage.dispose();
		}
		if (screenImage != null && !screenImage.isDisposed()) {
			screenImage.dispose();
		}
		grey.dispose();
	}

	
	/* Paint function */
	private void paint(GC gc) {
		Rectangle clientRect = getClientArea(); /* Canvas' painting area */
		if (sourceImage != null) {
			Rectangle imageRect =
				SWT2Dutil.inverseTransformRect(transform, clientRect);
			int gap = 2; /* find a better start point to render */
			imageRect.x -= gap; imageRect.y -= gap;
			imageRect.width += 2 * gap; imageRect.height += 2 * gap;

			Rectangle imageBound = sourceImage.getBounds();
			imageRect = imageRect.intersection(imageBound);
			Rectangle destRect = SWT2Dutil.transformRect(transform, imageRect);

			if (screenImage != null)
				screenImage.dispose();
			screenImage =
				new Image(getDisplay(), clientRect.width, clientRect.height);
			GC newGC = new GC(screenImage);
			newGC.setClipping(clientRect);
			
			//Background
			newGC.setBackground(grey);
			newGC.fillRectangle(0, 0, destRect.x, clientRect.height);
			newGC.fillRectangle(clientRect.width-destRect.x, 0, destRect.x, clientRect.height);
			newGC.fillRectangle(0, 0, clientRect.width, destRect.y);
			newGC.fillRectangle(0, clientRect.height-destRect.y, clientRect.width, destRect.y);
			
			//Document image
			newGC.drawImage(
				sourceImage,
				imageRect.x,
				imageRect.y,
				imageRect.width,
				imageRect.height,
				destRect.x,
				destRect.y,
				destRect.width,
				destRect.height);
			newGC.dispose();

			gc.drawImage(screenImage, 0, 0);
		} else {
			gc.setClipping(clientRect);
			gc.fillRectangle(clientRect);
			initScrollBars();
		}
	}

	/* Initalize the scrollbar and register listeners. */
	private void initScrollBars() {
		ScrollBar horizontal = getHorizontalBar();
		horizontal.setEnabled(false);
		horizontal.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				scrollHorizontally((ScrollBar) event.widget);
			}
		});
		ScrollBar vertical = getVerticalBar();
		vertical.setEnabled(false);
		vertical.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				scrollVertically((ScrollBar) event.widget);
			}
		});
	}

	/* Scroll horizontally */
	private void scrollHorizontally(ScrollBar scrollBar) {
		if (sourceImage == null)
			return;
		AffineTransform af = transform;
		double tx = af.getTranslateX();
		double select = -scrollBar.getSelection();
		af.preConcatenate(AffineTransform.getTranslateInstance(select - tx, 0));
		transform = af;
		syncScrollBars();
	}

	/* Scroll vertically */
	private void scrollVertically(ScrollBar scrollBar) {
		if (sourceImage == null)
			return;

		AffineTransform af = transform;
		double ty = af.getTranslateY();
		double select = -scrollBar.getSelection();
		af.preConcatenate(AffineTransform.getTranslateInstance(0, select - ty));
		transform = af;
		syncScrollBars();
	}

	/**
	 * Source image getter.
	 * @return sourceImage.
	 */
	public Image getSourceImage() {
		return sourceImage;
	}

	/**
	 * Synchronize the scrollbar with the image. If the transform is out
	 * of range, it will correct it. This function considers only following
	 * factors :<b> transform, image size, client area</b>.
	 */
	public void syncScrollBars() {
		if (sourceImage == null) {
			redraw();
			return;
		}

		AffineTransform af = transform;
		double sx = af.getScaleX(), sy = af.getScaleY();
		double tx = af.getTranslateX(), ty = af.getTranslateY();
		if (tx > 0) tx = 0;
		if (ty > 0) ty = 0;

		ScrollBar horizontal = getHorizontalBar();
		horizontal.setIncrement((int) (getClientArea().width / 100));
		horizontal.setPageIncrement(getClientArea().width);
		Rectangle imageBound = sourceImage.getBounds();
		int cw = getClientArea().width, ch = getClientArea().height;
		if (imageBound.width * sx > cw) { /* image is wider than client area */
			horizontal.setMaximum((int) (imageBound.width * sx));
			horizontal.setEnabled(true);
			if (((int) - tx) > horizontal.getMaximum() - cw)
				tx = -horizontal.getMaximum() + cw;
		} else { /* image is narrower than client area */
			horizontal.setEnabled(false);
			tx = (cw - imageBound.width * sx) / 2; //center if too small.
		}
		horizontal.setSelection((int) (-tx));
		horizontal.setThumb((int) (getClientArea().width));

		ScrollBar vertical = getVerticalBar();
		vertical.setIncrement((int) (getClientArea().height / 100));
		vertical.setPageIncrement((int) (getClientArea().height));
		if (imageBound.height * sy > ch) { /* image is higher than client area */
			vertical.setMaximum((int) (imageBound.height * sy));
			vertical.setEnabled(true);
			if (((int) - ty) > vertical.getMaximum() - ch)
				ty = -vertical.getMaximum() + ch;
		} else { /* image is less higher than client area */
			vertical.setEnabled(false);
			ty = (ch - imageBound.height * sy) / 2; //center if too small.
		}
		vertical.setSelection((int) (-ty));
		vertical.setThumb((int) (getClientArea().height));

		/* update transform. */
		af = AffineTransform.getScaleInstance(sx, sy);
		af.preConcatenate(AffineTransform.getTranslateInstance(tx, ty));
		transform = af;

		redraw();
	}

	/**
	 * Reload image from a file
	 * @param filename image file
	 * @return swt image created from image file
	 */
	public Image loadImage(String filename) {
		if (sourceImage != null && !sourceImage.isDisposed()) {
			sourceImage.dispose();
			sourceImage = null;
		}
		sourceImage = new Image(getDisplay(), filename);
		showOriginal();
		return sourceImage;
	}

	/**
	 * Call back funtion of button "open". Will open a file dialog, and choose
	 * the image file. It supports image formats supported by Eclipse.
	 */
	public void onFileOpen() {
		FileDialog fileChooser = new FileDialog(getShell(), SWT.OPEN);
		fileChooser.setText("Open image file");
		fileChooser.setFilterPath(currentDir);
		fileChooser.setFilterExtensions(
			new String[] { "*.gif; *.jpg; *.png; *.ico; *.bmp" });
		fileChooser.setFilterNames(
			new String[] { "SWT image" + " (gif, jpeg, png, ico, bmp)" });
		String filename = fileChooser.open();
		if (filename != null){
			loadImage(filename);
			currentDir = fileChooser.getFilterPath();
		}
	}
	
	public void setSourceImage(Image image) {
		setSourceImage(image, true);
	}
	
	public void setSourceImage(Image image, boolean resetZoomAndScrollbars)
	{
		if (sourceImage != null && !sourceImage.isDisposed()) {
			sourceImage.dispose();
			sourceImage = null;
		}
		sourceImage = image; 
		if (resetZoomAndScrollbars)
			showOriginal();
		else
			syncScrollBars();
	}
以上是SWTImageCanvas源码

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧