SWT中使用Opengl画圆的例子

import org.eclipse.opengl.GL;
import org.eclipse.opengl.GLU;
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.graphics.Rectangle;
import org.eclipse.swt.opengl.GLCanvas;

public class MouseHandler implements MouseListener, MouseMoveListener {
	
	private boolean isMouseDown; //鼠标是否按下
	private Point beginPoint;
	private Point endPoint;
	
	private double degtorads = 3.14159265 / 180.0;
	
	private Window window;
	
	public MouseHandler(Window window) {
		this.window = window;
	}
	
	public void mouseDoubleClick(MouseEvent e) {
		//Nothing to do
	}
	
	public void mouseDown(MouseEvent e) {
		if(e.button == 1) {
			isMouseDown = true;
			beginPoint = screenToMode( new Point(e.x, e.y) );
		}
	}
	
	public void mouseUp(MouseEvent e) {
		isMouseDown = false;
	}
	
	public void mouseMove(MouseEvent e) {
		
		if(isMouseDown) {
			
			GLCanvas canvas = window.canvas;
			endPoint = screenToMode( new Point(e.x, e.y) );
			
			GL.glClear(GL.GL_COLOR_BUFFER_BIT);
			GL.glBegin(GL.GL_LINE_STRIP);
					
			double r = Math.sqrt((endPoint.x - beginPoint.x) * (endPoint.x - beginPoint.x) +
					(endPoint.y - beginPoint.y) * (endPoint.y - beginPoint.y));
			
			for(float theta = 0.0f; theta <= 360; theta += 1.0) {
				GL.glVertex2d(beginPoint.x + r * Math.cos(theta * degtorads), 
						beginPoint.y + r * Math.sin(theta * degtorads));
			}
	
			GL.glEnd();
		
			canvas.swapBuffers();
		}
	}
	
	public Point screenToMode(Point point) {
		Rectangle rect = window.canvas.getClientArea();
		return new Point(point.x - rect.width / 2, (rect.height - point.y) - rect.height / 2);
	}
}
 
import org.eclipse.opengl.GL;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.opengl.GLCanvas;
import org.eclipse.swt.opengl.GLData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;


public class Window {
	
	private static Window instance = null;
	
	public Display display;
	public Shell shell;
	public GLCanvas canvas;

	private Window() {
		display = new Display();
		shell = new Shell(display);
		
		createContents(shell);
	}
	
	public void createContents(Composite composite) {
		composite.setLayout(new FillLayout());
		GLData data = new GLData();
		data.doubleBuffer = true;
		canvas = new GLCanvas(composite, SWT.NONE, data);
		canvas.setCurrent();
		
		canvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				update();
			}
		}); 
		
		canvas.addControlListener(new ControlAdapter() {
			public void controlResized(ControlEvent e) {
				update();
			}
		});
		
		MouseHandler mh = new MouseHandler(this);
		canvas.addMouseListener(mh);
		canvas.addMouseMoveListener(mh);
		
	}
	
	public void mainLoop() {
		while(!shell.isDisposed()) {
			if(!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
	
	public void dispose() {
		display.dispose();
	}
	
	public void show() {
		shell.open();
		
		mainLoop();
		dispose();
	}
	
	public void update() {
		Rectangle rect = canvas.getClientArea();
		
		GL.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
		GL.glColor3f(0.0f, 0.0f, 1.0f);
		GL.glMatrixMode(GL.GL_PROJECTION);
		GL.glLoadIdentity();
		
		GL.glOrtho(-rect.width / 2, rect.width / 2, -rect.height / 2, rect.height / 2, -1.0, 1.0);
		GL.glViewport(0, 0, rect.width, rect.height);
		
		GL.glClear(GL.GL_COLOR_BUFFER_BIT);
		
		canvas.swapBuffers();
	}
	
	public static Window getInstance() {
		if(instance == null) {
			instance = new Window();
		}
		
		return instance;
	}
}
 
public class Test {

	public static void main(String[] args) {
		Window window = Window.getInstance();
		window.show();
	}
}

 上面的程序是用鼠标画圆的例子,

你可能感兴趣的:(eclipse,360)