I "ported" the example code found at
http://gnomejournal.org/article/34/writing-a-widget-using-cairo-and-gtk28 t=
o
libgtk-java.
To be able to run the code you need a patch from bugzilla
http://bugzilla.gnome.org/show_bug.cgi?id=3D328171 which exposes a
widgets allocation size.
import org.gnu.gdk.Color;
import org.gnu.gdk.GdkCairo;
import org.gnu.gtk.Allocation;
import org.gnu.gtk.DrawingArea;
import org.gnu.gtk.Gtk;
import org.gnu.gtk.Window;
import org.gnu.gtk.event.ExposeEvent;
import org.gnu.gtk.event.ExposeListener;
import org.gnu.gtk.event.LifeCycleEvent;
import org.gnu.gtk.event.LifeCycleListener;
public class EggClockFace extends DrawingArea implements ExposeListener {
public EggClockFace() {
super();
this.addListener(this);
}
public boolean exposeEvent(ExposeEvent event) {
GdkCairo cr =3D new GdkCairo(this.getWindow());
cr.rectangle(event.getArea());
cr.clip();
draw(cr);
return false;
}
private void draw(GdkCairo cr) {
double x, y, radius;
Allocation alloc =3D this.getAllocation();
x =3D alloc.getX() + alloc.getWidth() / 2;
y =3D alloc.getY() + alloc.getHeight() / 2;
radius =3D (x < y ? x : y) - 5;
cr.arc(x, y, radius, 0, 2 * Math.PI);
cr.setSourceRGB(1, 1, 1);
cr.fillPreserve();
cr.setSourceColor(Color.BLACK);
cr.stroke();
for (int i =3D 0; i < 12; i++) {
double inset;
cr.save();
if ( i % 3 =3D=3D 0 ) {
inset =3D 0.2 * radius;
} else {
inset =3D 0.1 * radius;
cr.setLineWidth(0.5 * cr.getLineWidth());
}
cr.moveTo(x + (radius - inset) * Math.cos(i * Math.PI / 6),
y + (radius - inset) * Math.sin(i * Math.PI / 6));
cr.lineTo(x + radius * Math.cos(i * Math.PI / 6),
y + radius * Math.sin(i * Math.PI / 6));
cr.stroke();
cr.restore();
}
}
public static void main(String[] args) {
Gtk.init(args);
Window window =3D new Window();
EggClockFace clock =3D new EggClockFace();
window.add(clock);
window.addListener(new LifeCycleListener() {
public void lifeCycleEvent(LifeCycleEvent event) {
};
public boolean lifeCycleQuery(LifeCycleEvent event) {
Gtk.mainQuit();
return false;
}
});
window.showAll();
Gtk.main();
}
}
|