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

2023-12-14 17:48

本文主要是介绍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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic void widgetSelected(final SelectionEvent e) {wave = "sin";}});final MenuItem itemCos = new MenuItem(undeeMenu, SWT.RADIO);itemCos.setText("余弦波");itemCos.addSelectionListener(new SelectionAdapter() {@Overridepublic void widgetSelected(final SelectionEvent e) {wave = "cos";}});final MenuItem itemElse = new MenuItem(undeeMenu, SWT.RADIO);itemElse.setText("三角波");itemElse.addSelectionListener(new SelectionAdapter() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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开发(配置、实例及源码)正弦波的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/493452

相关文章

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Debian系和Redhat系防火墙配置方式

《Debian系和Redhat系防火墙配置方式》文章对比了Debian系UFW和Redhat系Firewalld防火墙的安装、启用禁用、端口管理、规则查看及注意事项,强调SSH端口需开放、规则持久化,... 目录Debian系UFW防火墙1. 安装2. 启用与禁用3. 基本命令4. 注意事项5. 示例配置R

PyQt5 GUI 开发的基础知识

《PyQt5GUI开发的基础知识》Qt是一个跨平台的C++图形用户界面开发框架,支持GUI和非GUI程序开发,本文介绍了使用PyQt5进行界面开发的基础知识,包括创建简单窗口、常用控件、窗口属性设... 目录简介第一个PyQt程序最常用的三个功能模块控件QPushButton(按钮)控件QLable(纯文本

PyCharm中配置PyQt的实现步骤

《PyCharm中配置PyQt的实现步骤》PyCharm是JetBrains推出的一款强大的PythonIDE,结合PyQt可以进行pythion高效开发桌面GUI应用程序,本文就来介绍一下PyCha... 目录1. 安装China编程PyQt1.PyQt 核心组件2. 基础 PyQt 应用程序结构3. 使用 Q

Redis MCP 安装与配置指南

《RedisMCP安装与配置指南》本文将详细介绍如何安装和配置RedisMCP,包括快速启动、源码安装、Docker安装、以及相关的配置参数和环境变量设置,感兴趣的朋友一起看看吧... 目录一、Redis MCP 简介二、安www.chinasem.cn装 Redis MCP 服务2.1 快速启动(推荐)2.

MySQL多实例管理如何在一台主机上运行多个mysql

《MySQL多实例管理如何在一台主机上运行多个mysql》文章详解了在Linux主机上通过二进制方式安装MySQL多实例的步骤,涵盖端口配置、数据目录准备、初始化与启动流程,以及排错方法,适用于构建读... 目录一、什么是mysql多实例二、二进制方式安装MySQL1.获取二进制代码包2.安装基础依赖3.清

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束

Apache Ignite缓存基本操作实例详解

《ApacheIgnite缓存基本操作实例详解》文章介绍了ApacheIgnite中IgniteCache的基本操作,涵盖缓存获取、动态创建、销毁、原子及条件更新、异步执行,强调线程池注意事项,避免... 目录一、获取缓存实例(Getting an Instance of a Cache)示例代码:二、动态

Spring Boot配置和使用两个数据源的实现步骤

《SpringBoot配置和使用两个数据源的实现步骤》本文详解SpringBoot配置双数据源方法,包含配置文件设置、Bean创建、事务管理器配置及@Qualifier注解使用,强调主数据源标记、代... 目录Spring Boot配置和使用两个数据源技术背景实现步骤1. 配置数据源信息2. 创建数据源Be