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

相关文章

Zookeeper安装和配置说明

一、Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及伪集群模式。 ■ 单机模式:Zookeeper只运行在一台服务器上,适合测试环境; ■ 伪集群模式:就是在一台物理机上运行多个Zookeeper 实例; ■ 集群模式:Zookeeper运行于一个集群上,适合生产环境,这个计算机集群被称为一个“集合体”(ensemble) Zookeeper通过复制来实现

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

CentOS7安装配置mysql5.7 tar免安装版

一、CentOS7.4系统自带mariadb # 查看系统自带的Mariadb[root@localhost~]# rpm -qa|grep mariadbmariadb-libs-5.5.44-2.el7.centos.x86_64# 卸载系统自带的Mariadb[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7

Hadoop企业开发案例调优场景

需求 (1)需求:从1G数据中,统计每个单词出现次数。服务器3台,每台配置4G内存,4核CPU,4线程。 (2)需求分析: 1G / 128m = 8个MapTask;1个ReduceTask;1个mrAppMaster 平均每个节点运行10个 / 3台 ≈ 3个任务(4    3    3) HDFS参数调优 (1)修改:hadoop-env.sh export HDFS_NAMENOD

hadoop开启回收站配置

开启回收站功能,可以将删除的文件在不超时的情况下,恢复原数据,起到防止误删除、备份等作用。 开启回收站功能参数说明 (1)默认值fs.trash.interval = 0,0表示禁用回收站;其他值表示设置文件的存活时间。 (2)默认值fs.trash.checkpoint.interval = 0,检查回收站的间隔时间。如果该值为0,则该值设置和fs.trash.interval的参数值相等。

NameNode内存生产配置

Hadoop2.x 系列,配置 NameNode 内存 NameNode 内存默认 2000m ,如果服务器内存 4G , NameNode 内存可以配置 3g 。在 hadoop-env.sh 文件中配置如下。 HADOOP_NAMENODE_OPTS=-Xmx3072m Hadoop3.x 系列,配置 Nam

wolfSSL参数设置或配置项解释

1. wolfCrypt Only 解释:wolfCrypt是一个开源的、轻量级的、可移植的加密库,支持多种加密算法和协议。选择“wolfCrypt Only”意味着系统或应用将仅使用wolfCrypt库进行加密操作,而不依赖其他加密库。 2. DTLS Support 解释:DTLS(Datagram Transport Layer Security)是一种基于UDP的安全协议,提供类似于

嵌入式QT开发:构建高效智能的嵌入式系统

摘要: 本文深入探讨了嵌入式 QT 相关的各个方面。从 QT 框架的基础架构和核心概念出发,详细阐述了其在嵌入式环境中的优势与特点。文中分析了嵌入式 QT 的开发环境搭建过程,包括交叉编译工具链的配置等关键步骤。进一步探讨了嵌入式 QT 的界面设计与开发,涵盖了从基本控件的使用到复杂界面布局的构建。同时也深入研究了信号与槽机制在嵌入式系统中的应用,以及嵌入式 QT 与硬件设备的交互,包括输入输出设

OpenHarmony鸿蒙开发( Beta5.0)无感配网详解

1、简介 无感配网是指在设备联网过程中无需输入热点相关账号信息,即可快速实现设备配网,是一种兼顾高效性、可靠性和安全性的配网方式。 2、配网原理 2.1 通信原理 手机和智能设备之间的信息传递,利用特有的NAN协议实现。利用手机和智能设备之间的WiFi 感知订阅、发布能力,实现了数字管家应用和设备之间的发现。在完成设备间的认证和响应后,即可发送相关配网数据。同时还支持与常规Sof

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听