DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

SimpleGraphicEditor In SWT

08.30.2006
| 5608 views |
  • submit to reddit
        import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class SimpleGraphicEditor {

	private Display display;
	private Shell shell;
	private Canvas canvas;
	private Image imageBuffer;

	public SimpleGraphicEditor() {
		display = new Display();
		shell = new Shell(display);
		
		createGUI();
		
		shell.open();
		
		while(!shell.isDisposed()) {
			if(!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
	
	private void createGUI() {
		shell.setLayout(new FillLayout());
		
		canvas = new Canvas(shell, SWT.BORDER);
		canvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				e.gc.drawImage(imageBuffer, 0, 0, 500, 500, 0, 0, 500, 500);
			}});
		imageBuffer = new Image(display, 500, 500);
		Listener listener = new Listener() {
			private boolean isDrawing;
			private GC gc = new GC(imageBuffer);
			private int oldX = 0;
			private int oldY = 0;
			private int newX = 0;
			private int newY = 0;
			private boolean isDeleting;

			public void handleEvent(Event event) {
//				System.err.println("type=" + event.type);
				switch(event.type) {
				case SWT.MouseDown:
//					System.err.println("button down");
					switch(event.button) {
					case 1:
						isDrawing = true;
						break;
					case 2:
						System.err.println("button2 down");
						break;
					case 3:
						isDeleting = true;
						break;
					}
					break;
				case SWT.MouseUp:
//					System.err.println("button up");
					switch(event.button) {
					case 1:
						isDrawing = false;
						break;
					case 2:
						System.err.println("button2 up");
						break;
					case 3:
						isDeleting = false;
						break;
					}
					break;
				case SWT.MouseMove:
					newX = event.x;
					newY = event.y;
//					System.err.println("mouse moved");
					if(isDrawing) {
						gc.drawLine(oldX, oldY, newX, newY);
						int x = (oldX < newX) ? oldX : newX;
						int y = (oldY < newY) ? oldY : newY;
						int width  = (oldX < newX) ? (newX-oldX) : (oldX - newX);
						int height = (oldY < newY) ? (newY-oldY) : (oldY - newY);
						if(width < 20) width = 20;
						if(height < 20) height = 20;
						canvas.redraw(x, y, width, height, true);
					} else if(isDeleting) {
//						Color c = gc.getBackground();
//						gc.setBackground(gc.getForeground());
						gc.fillRectangle(newX - 15, newY - 15, 30, 30);
						canvas.redraw(newX - 15, newY - 15, 30, 30, true);
//						gc.setBackground(c);
					}
					
					break;
				}
				
				oldX = event.x;
				oldY = event.y;
			}
		};
		canvas.addListener(SWT.MouseDown, listener);
		canvas.addListener(SWT.MouseUp, listener);
		canvas.addListener(SWT.MouseMove, listener);
	}

	public static void main(String[] args) {
		new SimpleGraphicEditor();
	}
}