⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imageresizer.java

📁 这是一个基于java编写的torrent的P2P源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.aelitis.azureus.ui.swt.utils;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Scale;
import org.eclipse.swt.widgets.Shell;
import org.gudy.azureus2.core3.util.AEThread;
import org.gudy.azureus2.core3.util.SystemTime;
import org.gudy.azureus2.ui.swt.Utils;

public class ImageResizer
{

	static private final int RESIZE_STEPS = 100000;

	static private final int MARGIN = 20;

	private int minWidth, minHeight;

	private int displayWidth, displayHeight;

	private Display display;

	private Shell parent;

	private Shell shell;

	private Cursor cursor;

	private Canvas canvas;

	private Scale scale;

	private long lastUpdate = 0l;

	private Image original;

	private int originalWidth, originalHeight;

	private Image currentImage;

	private Image overlay;

	private Image overlayDragging;

	private Image overlayNotDragging;

	private boolean done;

	private Image result;

	private float zoomRatio;

	private float minZoomRatio;

	private float maxZoomRatio;

	private Point offset;

	private Listener moveImageListener = new Listener() {

		private boolean mouseDown = false;

		private Point pointDown;

		public void handleEvent(Event event) {
			//System.out.println(event);
			switch (event.type) {
				case SWT.MouseDown:
					mouseDown = true;
					pointDown = new Point(event.x, event.y);
					overlay = overlayDragging;
					drawCurrentImage();
					break;
				case SWT.MouseUp:
					mouseDown = false;
					overlay = overlayNotDragging;
					drawCurrentImage();
					break;
				case SWT.MouseMove:
					if (!mouseDown) {
						break;
					}
					offset.x = offset.x + event.x - pointDown.x;
					offset.y = offset.y + event.y - pointDown.y;
					insureOffsetIsCorrect();

					pointDown.x = event.x;
					pointDown.y = event.y;
					drawCurrentImage();
					break;
				case SWT.MouseEnter:

					break;
				case SWT.MouseExit:

					break;
			}

		}
	};

	public ImageResizer(Display display, int width, int height, Shell parent) {
		this.parent = parent;
		this.display = display;
		this.minWidth = width;
		this.minHeight = height;
	}

	public Image resize(Image original) throws ImageResizeException {

		this.original = original;

		//If the image is too small, let's just not deal with it
		if (!checkSize(original)) {
			dispose();
			throw new ImageResizeException(
					"The image provided is too small (has to be at least " + minWidth
							+ " x " + minHeight + "), please choose a different one");
		}

		originalWidth = original.getBounds().width;
		originalHeight = original.getBounds().height;

		currentImage = new Image(display, internalResize(original,
				(int) (originalWidth * zoomRatio), (int) (originalHeight * zoomRatio)));
		offset = new Point(0, 0);

		if (minWidth != original.getBounds().width
				|| minHeight != original.getBounds().height) {

			displayWidth = minWidth + 2 * (MARGIN + 1);
			displayHeight = minHeight + 2 * (MARGIN + 1);

			overlay = overlayNotDragging = createOverlayImage((byte) 255, 0x00FFFFFF,
					(byte) 255, 0x00000000);
			overlayDragging = createOverlayImage((byte) 80, 0x00FFFFFF, (byte) 255,
					0x00FFFFFF);

			initUI();

			done = false;
			while (!done) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} else {
			result = computeResultImage();
		}

		dispose();

		return result;
	}

	private void initUI() {

		cursor = new Cursor(display, SWT.CURSOR_HAND);

		if (parent != null) {
			shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		} else {
			shell = new Shell(display, SWT.CLOSE | SWT.BORDER);
		}
		shell.setText("Thumbnail Assistant");

		Utils.setShellIcon(shell);

		shell.addListener(SWT.Close, new Listener() {
			public void handleEvent(Event event) {
				event.doit = false;
				result = null;
				done = true;
			}
		});

		FormLayout layout = new FormLayout();
		layout.marginBottom = 5;
		layout.marginTop = 5;
		layout.marginLeft = 5;
		layout.marginRight = 5;

		FormData data;
		shell.setLayout(layout);

		Label title = new Label(shell, SWT.WRAP);
		title.setText("This tool lets you preview how your thumbnail is going to look like on the Azureus Platform");

		data = new FormData();
		data.width = displayWidth;
		title.setLayoutData(data);

		canvas = new Canvas(shell, SWT.BORDER);
		canvas.setCursor(cursor);
		data = new FormData();
		data.width = displayWidth;
		data.height = displayHeight;
		data.top = new FormAttachment(title, 5);
		canvas.setLayoutData(data);

		canvas.addListener(SWT.MouseDown, moveImageListener);
		canvas.addListener(SWT.MouseUp, moveImageListener);
		canvas.addListener(SWT.MouseMove, moveImageListener);

		canvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent arg0) {
				drawCurrentImage();
			}
		});

		offset.x = (minWidth - currentImage.getBounds().width) / 2;
		offset.y = (minHeight - currentImage.getBounds().height) / 2;

		Label label = new Label(shell, SWT.WRAP);
		//The label text depends on the presence of the scale,
		//Thefore we delay the size computation as well as
		//Assiging any FormData (layout) to it see (1)

		//The Control to witch the Buttons OK and Cancel are going to be attached
		//Depends on the presence of the scale
		Control attach = label;

		if (minZoomRatio < 1) {
			scale = new Scale(shell, SWT.HORIZONTAL);
			data = new FormData();
			data.width = displayWidth;
			data.top = new FormAttachment(label, 5);
			scale.setLayoutData(data);
			scale.setMaximum((int) (RESIZE_STEPS * maxZoomRatio));
			scale.setMinimum((int) (RESIZE_STEPS * minZoomRatio));
			scale.setIncrement((int) ((maxZoomRatio - minZoomRatio) * RESIZE_STEPS / 10));
			scale.setPageIncrement((int) ((maxZoomRatio - minZoomRatio)
					* RESIZE_STEPS / 10));

			scale.addListener(SWT.Selection, new Listener() {
				public void handleEvent(Event arg0) {
					final long timestamp = SystemTime.getCurrentTime();
					lastUpdate = timestamp;

					final int position = scale.getSelection();

					AEThread t = new AEThread("") {
						public void runSupport() {
							try {
								Thread.sleep(150);
							} catch (Exception e) {
								e.printStackTrace();
							}

							if (timestamp == lastUpdate) {
								if (display != null && !display.isDisposed()) {
									display.asyncExec(new Runnable() {
										public void run() {
											refreshCurrentImage(position);
										}
									});
								}
							}
						}
					};
					t.setDaemon(true);
					t.start();

				}
			});
			attach = scale;
			label.setText("Move the image by dragging it, resize it by using the slider below");
		} else {
			label.setText("Move the image by dragging it");
		}

		// (1) Layout of the label, depending on the text in it
		int width = label.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
		if (width > displayWidth) {
			width = displayWidth;
		}

		data = new FormData();
		data.width = width;
		data.top = new FormAttachment(canvas, 5);
		data.left = new FormAttachment(canvas, 0, SWT.CENTER);
		label.setLayoutData(data);

		Button btnCancel = new Button(shell, SWT.PUSH);
		btnCancel.setText("Cancel");
		data = new FormData();
		data.width = 70;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -