Hatena::ブログ(Diary)

yukobaのブログ このページをアンテナに追加 RSSフィード Twitter

2012年03月21日 SWT + JNA + Cairo

SWT + JNA + Cairo

Win32 ではこんな感じで出来ます。Cairo は http://www.gtk.org/download/win32.phpall-in-one bundle から入手するのがおすすめ

import com.sun.jna.Pointer;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.junit.Test;

public class SWTTest {
    @Test
    public void test() {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Shell");
        shell.setSize(200, 200);
        shell.open();

        int hWnd = shell.handle;
        int hdc = OS.GetDC(hWnd);
        try {
            Cairo cairo = Cairo.INSTANCE;
            Pointer surface = cairo.cairo_win32_surface_create(hdc);
            Pointer pattern = cairo.cairo_pattern_create_rgba(1, 0, 0, 0.1);
            Pointer cr = cairo.cairo_create(surface);
            cairo.cairo_set_source(cr, pattern);
            cairo.cairo_rectangle(cr, 0, 0, 100, 100);
            cairo.cairo_fill(cr);
            cairo.cairo_destroy(cr);
            cairo.cairo_pattern_destroy(pattern);
            cairo.cairo_surface_destroy(surface);
        } finally {
            OS.ReleaseDC(hWnd, hdc);
        }

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }
}
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;

public interface Cairo extends Library {
    final Cairo INSTANCE = (Cairo) Native.loadLibrary("libcairo-2", Cairo.class);

    // cairo_t
    Pointer cairo_create(Pointer target);
    void cairo_destroy(Pointer cr);
    void cairo_set_source(Pointer cr, Pointer source);
    void cairo_fill(Pointer cr);
    void cairo_rectangle(Pointer cr, double x, double y, double width, double heigth);

    // pattern
    Pointer cairo_pattern_create_rgb(double red, double green, double blue);
    Pointer cairo_pattern_create_rgba(double red, double green, double blue, double alpha);
    void cairo_pattern_destroy(Pointer pattern);

    // Surface
    void cairo_surface_destroy(Pointer surface);

    // Win32
    Pointer cairo_win32_surface_create(int hdc);
}
トラックバック - http://d.hatena.ne.jp/yukoba/20120321/p1