首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

SWT 全方位實戰指南:從 HelloWorld 到 OpenGL 3D 繪圖

     編輯:Java 相關知識

SWT(Standard Widget Toolkit)是 Eclipse 平台的原生 GUI 元件庫,提供 Write Once, Run Anywhere 的原生觀感與穩定效能。相較 AWT/Swing,SWT 元件更豐富、平台表現更穩定、Bug 更少。本文從基礎架構、佈局管理、2D 繪圖到 OpenGL 3D 整合,完整涵蓋 SWT 核心技術。

核心架構與 HelloWorld

import org.eclipse.swt.widgets.*;
public class HelloWorld {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Hello World");
    shell.setSize(200, 100);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) display.sleep();
    }
    display.dispose();
  }
}

四大核心類別

  • Display:頂層容器,代表螢幕,連接底層視窗系統,通常為單例
  • Shell:視窗,Composite/Control 樹的根,對應 JFrame
  • Composite:容器,可包含其他 Composite 與 Control
  • Control:重量級系統物件,Button、Label、Table、Tree 等皆繼承自此

關鍵機制

  • 訊息迴圈:readAndDispatch()/sleep() 手動處理事件,異於 Swing 事件驅動
  • 資源釋放:程式設計師責任釋放(dispose()),父釋放子跟著釋放
  • 標準建構式:new Widget(parent, styleBit),Style Bit 以位元運算組合(如 SWT.SINGLE | SWT.BORDER)
  • 錯誤分類:SWTError(不可修復)、SWTException(可恢復)、IllegalArgumentException(參數錯誤)
  • Item:輕量級元素,TreeItem、TableItem、MenuItem 等

五大佈局管理器

佈局特性適用場景LayoutData
FillLayout單行/單列,等寬高,不支援邊距/換行計算機面板、工具列按鈕無
RowLayout類 FlowLayout,支援換行、邊距、間距表單欄位排列、動態內容RowData(寬/高)
GridLayout網格狀,功能最強大也最複雜複雜表單、對話框、需精確對齊GridData(跨欄/列、對齊、縮放)
FormLayout依附關係定位(附著於邊緣/其他控制項)精密對齊、響應式視窗FormData
StackLayout疊層,同 CardLayout,一次顯示一層Wizard、分頁切換無

建議:GridLayout 複雜度高,建議搭配視覺化工具(如 WindowBuilder)設計,手寫僅限簡單案例。

2D 繪圖:Graphics Context (GC)

GC 封裝所有繪圖操作,可繪製於任何實作 Drawable 的物件(Control、Image、Display、Printer)。

繪製於 Image

Image image = new Image(display, "C:/music.gif");
GC gc = new GC(image);
gc.drawLine(0, 0, bounds.width, bounds.height);
gc.drawLine(0, bounds.height, bounds.width, 0);
gc.dispose(); image.dispose(); // 必須釋放

繪製於 Control(正確模式:PaintListener)

shell.addPaintListener(new PaintListener() {
  public void paintControl(PaintEvent e) {
    Rectangle area = shell.getClientArea();
    e.gc.drawLine(0, 0, area.width, area.height);
  }
});

核心繪圖 API 分類

  • 線條/形狀:drawLine、drawPolyline、drawPolygon、drawRectangle、drawRoundedRectangle、drawOval、drawArc
  • 樣式控制:setLineStyle(SOLID/DASH/DOT/DASHDOT)、setLineWidth
  • 文字:drawText(處理 \t \n)、drawString(原樣輸出)、textExtent/stringExtent 計算尺寸
  • 填充:fillRectangle、fillRoundRectangle、fillOval、fillArc、fillGradientRectangle(漸層)
  • 剪裁:setClipping(x,y,w,h) 限制可繪區域
  • XOR 模式:setXORMode(true) 像素級異或運算
  • 圖片:drawImage(Image, x, y),支援 GIF/BMP/JPG/PNG/TIFF

Canvas:專為繪圖設計的 Control 子類,建議繼承建立自訂繪圖元件。

SWT + OpenGL 3D 繪圖

Eclipse 3.2+ 整合 org.eclipse.swt.opengl,核心類別:

  • GLContext:橋接 SWT 與 OpenGL,綁定 Canvas,需手動 resize()、dispose()
  • GL、GLU:OpenGL 1.1 完整 API 對應(330+ 指令),與 C API 幾乎一對一

GLScene 範本類別

public class GLScene extends Canvas {
  private GLContext context;
  public GLScene(Composite parent) {
    super(parent, SWT.NONE);
    context = new GLContext(this);
    addListener(SWT.Resize, e -> context.resize());
    addListener(SWT.Dispose, e -> context.dispose());
    setSize(parent.getClientArea().width, parent.getClientArea().height);
  }
  protected void initGL() {
    GL gl = context.getGL();
    gl.glClearColor(0, 0, 0, 0);
    gl.glEnable(GL.GL_DEPTH_TEST);
    gl.glDepthFunc(GL.GL_LEQUAL);
    gl.glShadeModel(GL.GL_SMOOTH);
    gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
  }
  public void render() {
    if (context == null || context.isDisposed()) return;
    context.makeCurrent();
    GL gl = context.getGL();
    drawScene(gl);
    context.swapBuffers();
  }
  protected void drawScene(GL gl) {
    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
    gl.glLoadIdentity();
    // 子類別覆寫實作具體繪圖
  }
}

顯示清單優化效能

重複物件(如圖表圓柱體)使用 Display List:

int list = gl.glGenLists(1);
gl.glNewList(list, GL.GL_COMPILE);
// 繪製圓柱體:gluDisk(底) + gluCylinder(身) + glTranslated + gluDisk(頂)
gl.glEndList();
// 繪製時:
gl.glCallList(list);

週期性重繪

背景執行緒每 100ms 呼叫 render(),達 10 FPS 動畫效果。

總結

SWT 以原生效能、豐富元件、穩定跨平台著稱。掌握 GC 2D 繪圖 可滿足大多數客製化 UI 需求;OpenGL 整合 則開啟高效能 3D 視覺化大門。關鍵在於:理解資源管理(dispose)、事件迴圈、佈局機制,並善用視覺化工具輔助複雜介面設計。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved