forked from bailuk/java-gtk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageBridge.java
More file actions
112 lines (89 loc) · 3.1 KB
/
ImageBridge.java
File metadata and controls
112 lines (89 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package examples;
import java.io.IOException;
import java.io.InputStream;
import ch.bailu.gtk.cairo.Context;
import ch.bailu.gtk.gdk.Gdk;
import ch.bailu.gtk.gdkpixbuf.Pixbuf;
import ch.bailu.gtk.gdkpixbuf.PixbufFormat;
import ch.bailu.gtk.gtk.DrawingArea;
import ch.bailu.gtk.gtk.Window;
import ch.bailu.gtk.lib.bridge.Image;
import ch.bailu.gtk.lib.handler.CallbackHandler;
import ch.bailu.gtk.lib.handler.SignalHandler;
import ch.bailu.gtk.lib.util.JavaResource;
import ch.bailu.gtk.type.Str;
public class ImageBridge implements DemoInterface {
private static final Str TITLE = new Str("Image bridge");
private static final Str DESCRIPTION = new Str("Load and display GTK logo from Java stream");
private Pixbuf pixbuf = null;
@Override
public Window runDemo() {
listSupportedFormats();
var demoWindow = new Window();
demoWindow.setSizeRequest(300, 300);
var drawingArea = new DrawingArea();
demoWindow.setChild(drawingArea);
drawingArea.onResize(this::setPixbuf);
drawingArea.setDrawFunc((cb, drawing_area, cr, width, height, user_data) -> drawLogo(cr), null, (cb, data)->{});
demoWindow.onDestroy(() -> {
CallbackHandler.unregister(drawingArea);
SignalHandler.disconnect(demoWindow);
});
return demoWindow;
}
private void listSupportedFormats() {
var list = Pixbuf.getFormats();
int count = 1;
while(list.isNotNull() && list.getFieldData().isNotNull()) {
var format = new PixbufFormat(list.getFieldData().cast());
System.out.println("__");
System.out.println("Format " + count + ":");
System.out.println(format.getName());
System.out.println(format.getDescription());
if (format.isDisabled()) {
System.out.println("disabled");
}
if (format.isScalable()) {
System.out.println("scalable");
}
if (format.isWritable()) {
System.out.println("writeable");
}
list = list.getFieldNext();
count ++;
}
}
private void setPixbuf(int width, int height) {
try {
var pixbufNew = loadPixbuf(width, height);
if (pixbuf != null) {
pixbuf.unref();
}
pixbuf = pixbufNew;
} catch (IOException e) {
System.err.println("ERROR reading image");
}
}
private Pixbuf loadPixbuf(int width, int height) throws IOException {
try (InputStream inputStream = new JavaResource("/GTK.svg").asStream()) {
return Image.load(inputStream, width, height);
}
}
private boolean drawLogo(Context context) {
if (pixbuf != null) {
context.save();
Gdk.cairoSetSourcePixbuf(context, pixbuf, 0, 0);
context.paint();
return true;
}
return false;
}
@Override
public Str getTitle() {
return TITLE;
}
@Override
public Str getDescription() {
return DESCRIPTION;
}
}