前面兩個博文都是用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();
}
}
自己在工程中添加一個檔案夾,然後放一張圖檔重命名就可以了。
