天天看点

Eclipse下的SWT的OpenGL开发(配置、实例及源码)正弦波

前面两个博文都是用java的awt开发jogl,但是现在想用swt开发,配置就不行了;

查了很多,说要eclipse安装OpenGL插件才可以,也下载了,解压到了plugin文件夹内,但是添加依赖时候死活找不到OpenGL插件,也就import不到GL、GLU两个类,只有放弃了(如果你能够正常添加OpenGL依赖请在下面留言告诉大家方法)。(发现一个新的解决方法:Eclipse--File--import--Plug-ins and Fragments--Directory--选择解压得到的文件夹--把两个文件夹add过来--finish,这样插件依赖的时候可以找到插件org.eclipse.opengl)

没办法,我暂且这样做的:把解压插件得到的两个文件夹中的org.eclipse.opengl_0.5.0文件夹里的ogl.jar文件,直接复制到项目工程中新建文件夹lib中,然后右键build path,这样也可以运行,虽然这不是插件应该依赖的方式。。。。。。。。。。。。。。另外还需要把org.eclipse.opengl.win32.x86_0.5.0文件夹里的gl-0500.dll文件复制到C盘windows/system32下面(其实放在PATH环境变量里面的任一个文件夹下都是可以的:如jdk的bin文件夹里也可以)

实例源码:

新建一个插件工程:这是一个画正弦波和余弦的代码,能够调节频率

1、建一个类Point

package swt.opengl.sin;

public class Point {
	private double pointX;
	private double pointY;
	
	public Point(double x, double y) {
		pointX = x;
		pointY = y;
	}
	
	public double getPointX() {
		return pointX;
	}
	/*public void setPointX(double pointX) {
		this.pointX = pointX;
	}*/
	
	public double getPointY() {
		return pointY;
	}
	
	/*public void setPointY(double pointY) {
		this.pointY = pointY;
	}*/
	
}
           

2、建一个类PointVector

package swt.opengl.sin;

import java.util.Vector;

public class PointVector {
	private static Vector<Point>  points = new Vector<Point>();

	public static Vector<Point> getPoints() {
		return points;
	}
	
	public static void addPoint(Point point) {
		points.add(point);
	}
	
	public static Point getPoint(int i) {
		Point point = points.get(i);
		return point;
	}
}
           

3、建一个类Curve2

package swt.opengl.sin;

import java.util.Vector;

import org.eclipse.opengl.GL;
import org.eclipse.opengl.GLU;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ViewForm;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.opengl.GLCanvas;
import org.eclipse.swt.opengl.GLData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

public class Curve2 {

	private GLCanvas canvas;

	private Composite composite;

	private ToolBar toolBar;

	private Composite controlComposite;

	private ToolItem frequencyToolItem;

	private Menu frequencyMenu;

	private ViewForm viewForm;

	private Shell shell;

	private static double d = Math.PI * 0;

	private Text frequency;

	private String wave = "sin";

	private int sleep = 100;

	private Image image;

	static Display display;

	public Curve2(final Shell sh) {
		shell = sh;

		initShell();

		canvas.setCurrent();
		setupViewingArea();

		init();
		createControl(controlComposite);
		final Runnable timer = new Runnable() {

			@Override
			public void run() {
				if (shell.isDisposed()) {
					image.dispose();
					return;
				}
				draw();
				frequency.setText(String.valueOf(sleep));

				shell.getDisplay().timerExec(sleep, this);
			}

		};
		timer.run();
		canvas.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(final MouseEvent e) {
				System.out.println(e.x + "   " + e.y);
			}

		});
	}

	private void initShell() {
		viewForm = new ViewForm(shell, SWT.NONE);
		viewForm.setLayout(new FillLayout());
		composite = new Composite(viewForm, SWT.NONE);
		viewForm.setContent(composite);

		initToolBar();

		viewForm.setTopLeft(toolBar);
		composite.setLayout(new GridLayout(2, false));

		GridData gridData = new GridData();
		gridData.heightHint = 400;
		gridData.widthHint = 400;
		gridData.verticalAlignment = GridData.BEGINNING;
		final GLData glData = new GLData();
		glData.doubleBuffer = true;
		glData.stencilSize = 8;
		canvas = new GLCanvas(composite, SWT.NONE, glData);
		canvas.setLayout(new GridLayout());
		canvas.setLayoutData(gridData);
		canvas.setSize(400, 400);

		gridData = new GridData();
		gridData.verticalAlignment = GridData.BEGINNING;
		controlComposite = new Composite(composite, SWT.NONE);
		controlComposite.setLayout(new GridLayout());
		controlComposite.setLayoutData(gridData);
		initMenu();
	}

	private void initMenu() {

		final Menu menu = new Menu(shell, SWT.BAR);
		final MenuItem file = new MenuItem(menu, SWT.CASCADE);
		file.setText("文件");

		final Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
		final MenuItem exit = new MenuItem(fileMenu, SWT.PUSH);
		exit.setText("退出(&E)");
		exit.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(final SelectionEvent e) {
				shell.dispose();
			}
		});
		file.setMenu(fileMenu);

		final MenuItem undee = new MenuItem(menu, SWT.CASCADE);
		undee.setText("波形");

		final Menu undeeMenu = new Menu(shell, SWT.DROP_DOWN);
		final MenuItem itemSin = new MenuItem(undeeMenu, SWT.RADIO);
		itemSin.setText("正弦波");
		itemSin.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(final SelectionEvent e) {
				wave = "sin";
			}
		});
		final MenuItem itemCos = new MenuItem(undeeMenu, SWT.RADIO);
		itemCos.setText("余弦波");
		itemCos.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(final SelectionEvent e) {
				wave = "cos";
			}
		});
		final MenuItem itemElse = new MenuItem(undeeMenu, SWT.RADIO);
		itemElse.setText("三角波");
		itemElse.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(final SelectionEvent e) {
				wave = "else";
			}
		});
		undee.setMenu(undeeMenu);

		final MenuItem frequencyItem = new MenuItem(menu, SWT.CASCADE);
		frequencyItem.setText("频率");
		final Menu frequencyMenu = new Menu(shell, SWT.DROP_DOWN);
		final MenuItem itemMs1000 = new MenuItem(frequencyMenu, SWT.RADIO);
		itemMs1000.setText("1000 ms");
		addListenerCode(itemMs1000, 1000);
		final MenuItem itemMs200 = new MenuItem(frequencyMenu, SWT.RADIO);
		itemMs200.setText("200 ms");
		addListenerCode(itemMs200, 200);
		final MenuItem itemMs100 = new MenuItem(frequencyMenu, SWT.RADIO);
		itemMs100.setText("100 ms");
		addListenerCode(itemMs100, 100);
		final MenuItem itemMs50 = new MenuItem(frequencyMenu, SWT.RADIO);
		itemMs50.setText("50 ms");
		addListenerCode(itemMs50, 50);
		final MenuItem itemMs30 = new MenuItem(frequencyMenu, SWT.RADIO);
		itemMs30.setText("30 ms");
		addListenerCode(itemMs30, 30);
		final MenuItem itemMs20 = new MenuItem(frequencyMenu, SWT.RADIO);
		itemMs20.setText("20 ms");
		addListenerCode(itemMs20, 20);
		frequencyItem.setMenu(frequencyMenu);

		shell.setMenuBar(menu);
	}

	private void initToolBar() {
		image = new Image(shell.getDisplay(), "icons/1.png");
		toolBar = new ToolBar(viewForm, SWT.SHADOW_OUT);
		final ToolItem checkTypeSin = new ToolItem(toolBar, SWT.RADIO);
		checkTypeSin.setText("正弦波");
		checkTypeSin.setToolTipText("正弦波曲线");
		checkTypeSin.setImage(image);
		checkTypeSin.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(final SelectionEvent e) {
				wave = "sin";
			}

		});
		final ToolItem checkTypeCos = new ToolItem(toolBar, SWT.RADIO);
		checkTypeCos.setText("余弦波");
		checkTypeCos.setToolTipText("余弦波曲线");
		checkTypeCos.setImage(image);
		checkTypeCos.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(final SelectionEvent e) {
				wave = "cos";
			}
		});
		final ToolItem checkTypeElse = new ToolItem(toolBar, SWT.RADIO);
		checkTypeElse.setText("三角波");
		checkTypeElse.setToolTipText("三角波曲线");
		checkTypeElse.setImage(image);
		checkTypeElse.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(final SelectionEvent e) {
				wave = "else";
			}
		});
		final ToolItem sepa = new ToolItem(toolBar, SWT.SEPARATOR);
		frequencyToolItem = new ToolItem(toolBar, SWT.DROP_DOWN);
		frequencyToolItem.setText("频率");
		frequencyToolItem.setToolTipText("刷新频率");
		frequencyToolItem.setImage(image);

		frequencyMenu = new Menu(shell, SWT.POP_UP);
		final MenuItem ms1000 = new MenuItem(frequencyMenu, SWT.RADIO);
		ms1000.setText("1000 ms");
		addListenerCode(ms1000, 1000);
		final MenuItem ms200 = new MenuItem(frequencyMenu, SWT.RADIO);
		ms200.setText("200 ms");
		addListenerCode(ms200, 200);
		final MenuItem ms100 = new MenuItem(frequencyMenu, SWT.RADIO);
		ms100.setText("100 ms");
		addListenerCode(ms100, 100);
		final MenuItem ms50 = new MenuItem(frequencyMenu, SWT.RADIO);
		ms50.setText("50 ms");
		addListenerCode(ms50, 50);
		final MenuItem ms30 = new MenuItem(frequencyMenu, SWT.RADIO);
		ms30.setText("30 ms");
		addListenerCode(ms30, 30);
		final MenuItem ms20 = new MenuItem(frequencyMenu, SWT.RADIO);
		ms20.setText("20 ms");
		addListenerCode(ms20, 20);

		frequencyToolItem.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(final SelectionEvent e) {
				if (e.detail == SWT.ARROW) {
					final Rectangle rect = frequencyToolItem.getBounds();
					Point point = new Point(rect.x, rect.y + rect.height);
					point = toolBar.toDisplay(point);
					frequencyMenu.setLocation(point);
					frequencyMenu.setVisible(true);
				}
			}

		});
	}

	private void addListenerCode(final MenuItem menuItem, final int sleepi) {
		menuItem.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(final SelectionEvent e) {
				sleep = sleepi;
				frequency.setText(sleep + "");

			}
		});
	}

	private void createControl(final Composite composite) {
		final Composite objectGroup = new Composite(composite, SWT.NONE);
		final GridLayout layout = new GridLayout(3, false);
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		objectGroup.setLayout(layout);
		objectGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));

		new Label(objectGroup, SWT.NONE).setText("波形:");
		final Combo objectCombo = new Combo(objectGroup, SWT.READ_ONLY);
		GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		data.grabExcessHorizontalSpace = true;
		data.horizontalSpan = 2;
		objectCombo.setLayoutData(data);
		objectCombo.setItems(new String[] { "正弦波	", "余弦波", "三角波" });
		objectCombo.setData("正弦波", "sin");
		objectCombo.setData("余弦波", "cos");
		objectCombo.setData("三角波", "else");
		objectCombo.select(0);
		objectCombo.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(final SelectionEvent e) {
				final String type = objectCombo.getText();
				wave = (String) objectCombo.getData(type.trim());
			}

		});

		new Label(objectGroup, SWT.NONE).setText("刷新频率(ms):");
		frequency = new Text(objectGroup, SWT.BORDER);
		data = new GridData();
		data.widthHint = 30;
		frequency.setLayoutData(data);

		frequency.setText(sleep + "");
		frequency.addVerifyListener(new VerifyListener() {

			@Override
			public void verifyText(final VerifyEvent e) {
				final String inStr = e.text;
				if (inStr.length() > 0) {
					e.doit = new String("0123456789").indexOf(inStr) >= 0;
				}
			}

		});

		final Button apply = new Button(objectGroup, SWT.NONE);
		apply.setText("应用");
		apply.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(final SelectionEvent e) {
				final String str = frequency.getText();
				sleep = new Integer(str).intValue();
			}

		});

	}

	private void init() {

		GL.glClear(GL.GL_COLOR_BUFFER_BIT);
		GL.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
		GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);

		canvas.swapBuffers();
	}

	private void makePoints() {
		d = d + Math.PI / 100;

		double sinX = Math.PI * 0;
		if (wave.equals("sin")) {
			for (int i = 0; i <= 10000; i++) {
				sinX += Math.PI / 1000;

				double x = sinX;
				final double y = 4 * Math.sin(sinX + d * Math.PI);

				x = 2 * sinX / Math.PI - 6;
				final swt.opengl.sin.Point point = new swt.opengl.sin.Point(x, y);
				PointVector.addPoint(point);
			}
		} else if (wave.equals("cos")) {
			for (int i = 0; i <= 10000; i++) {
				sinX += Math.PI / 1000;

				double x = sinX;
				final double y = 4 * Math.cos(sinX + d * Math.PI);
				x = 2 * sinX / Math.PI - 6;
				final swt.opengl.sin.Point point = new swt.opengl.sin.Point(x, y);
				swt.opengl.sin.PointVector.addPoint(point);
			}
		} else {

		}
	}

	private void setupViewingArea() {
		final Rectangle rect = canvas.getClientArea();
		final int height = rect.height;
		final int width = rect.width;
		GL.glViewport(0, 0, width, height);
		GL.glMatrixMode(GL.GL_PROJECTION);
		GL.glLoadIdentity();
		final float fAspect = (float) width / (float) height;
		GLU.gluPerspective(45.0f, fAspect, 0.5f, 400.0f);
		GL.glMatrixMode(GL.GL_MODELVIEW);
		GL.glLoadIdentity();

	}

	private void drawPoint(final swt.opengl.sin.Point point) {
		GL.glPushMatrix();
		GL.glBegin(GL.GL_POINTS);
		GL.glVertex2d(point.getPointX(), point.getPointY());
		GL.glEnd();
		GL.glPopMatrix();
	}

	public void draw() {
		GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
		GL.glLoadIdentity();
		GL.glTranslatef(0.0f, 0.0f, -14.0f);

		GL.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);

		GL.glBegin(GL.GL_POINTS);
		makePoints();
		final Vector<swt.opengl.sin.Point> vp = PointVector.getPoints();
		for (int i = 0; i < vp.size(); i++) {
			drawPoint(vp.get(i));
		}
		vp.removeAllElements();

		GL.glEnd();
		canvas.swapBuffers();

	}

	/**
	 * @param args
	 */
	public static void main(final String[] args) {
		// Display display = new Display();
		display = new Display();
		final Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		final Curve2 curve = new Curve2(shell);
		shell.setText("曲线图");
		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
	}

}
           

自己在工程中添加一个文件夹,然后放一张图片重命名就可以了。

Eclipse下的SWT的OpenGL开发(配置、实例及源码)正弦波

继续阅读