This site has been retired. For up to date information, see handbook.gnome.org or gitlab.gnome.org.


[Home] [TitleIndex] [WordIndex

The canonical source for these examples is GNOME SVN; if you see an issue please check there.


Back to JGIR.

GtkTest

package org.verbum;

import org.gnome.gir.dynamic.Gdk.Event;
import org.gnome.gir.dynamic.Gtk.GtkGlobals;
import org.gnome.gir.dynamic.Gtk.TextBuffer;
import org.gnome.gir.dynamic.Gtk.TextView;
import org.gnome.gir.dynamic.Gtk.Widget;
import org.gnome.gir.dynamic.Gtk.Window;
import org.gnome.gir.dynamic.Gtk.WindowType;
import org.gnome.gir.gobject.GErrorException;

public class GtkTest {

        public static void main(String[] args) throws GErrorException {
                /* All global functions go into a "FooGlobals" class.  
                   This function takes both argc and argv as pointers,
                   we just pass null. */
                GtkGlobals.initCheck(null, null);

                /* Enumerations work as you would expect - the name is
                   uppercase. */
                Window w = new Window(WindowType.TOPLEVEL);

                /* The default constructor.  You may also pass an Object[]
                   array of arguments, or a Map<String,Object>. */
                TextView tv = new TextView();

                /* Method calls are converted to camelCase. */
                final TextBuffer buf = tv.getBuffer();
                buf.insertAtCursor("hello world!", -1);
                w.add(tv);
                w.setDefaultSize(640, 480);

                /* Signals are represented by inner classes.  To attach a signal
                   handler, an anonymous class can be used. */
                w.connect(new Widget.DeleteEvent() {
                        @Override
                        public boolean onDeleteEvent(Widget w, Event arg0) {
                                buf.insertAtCursor("DELETE ME?! NEVER!\n", -1);
                                buf.insertAtCursor("(ok, try using xkill)", -1);
                                return true;
                        }
                });

                /* Anything that uses GError is mapped to a global GErrorException
                   for now.  */
                try {
                        w.setIconFromFile("/usr/share/pixmaps/drscheme.png");
                } catch (GErrorException e) {
                        System.err.println("Failed to set icon");
                        e.printStackTrace();
                }

                w.showAll();
                GtkGlobals.main();
        }
}

GStreamer Hello World

See the original C source for this example.

package org.verbum;

import org.gnome.gir.dynamic.Gst.Bus;
import org.gnome.gir.dynamic.Gst.BusFunc;
import org.gnome.gir.dynamic.Gst.DebugLevel;
import org.gnome.gir.dynamic.Gst.Element;
import org.gnome.gir.dynamic.Gst.GstGlobals;
import org.gnome.gir.dynamic.Gst.Message;
import org.gnome.gir.dynamic.Gst.MessageType;
import org.gnome.gir.dynamic.Gst.Pad;
import org.gnome.gir.dynamic.Gst.Pipeline;
import org.gnome.gir.dynamic.Gst.State;
import org.gnome.gir.dynamic.Gst.Element.PadAdded;
import org.gnome.gir.gobject.MainLoop;

import com.sun.jna.Pointer;

public class GStreamerTest {
        
        public static void main(String...args) {
                GstGlobals.init(null, null);
                GstGlobals.debugSetDefaultThreshold(DebugLevel.DEBUG);
                GstGlobals.debugSetColored(false);
                GstGlobals.debugSetActive(true);
                final MainLoop loop = new MainLoop();
                final Pipeline pipeline;
                final Element source, demuxer, decoder, conv, sink;
                final Bus bus;
                
                pipeline = new Pipeline("audio-player");
                source = GstGlobals.elementFactoryMake("filesrc", "file-source");
                demuxer = GstGlobals.elementFactoryMake("oggdemux", "ogg-demuxer");
                decoder = GstGlobals.elementFactoryMake("vorbisdec", "vorbis-decoder");
                conv = GstGlobals.elementFactoryMake("audioconvert", "converter");
                sink = GstGlobals.elementFactoryMake("autoaudiosink", "audio-output");

                if (pipeline == null || source == null || 
                                demuxer == null || decoder == null || 
                                conv == null|| sink == null) {
                    System.err.println("One element could not be created. Exiting.");
                    System.exit(-1);
                  }

                  /* Set up the pipeline */

                  /* we set the input filename to the source element */
                  source.set("location", args[0]);
                  System.out.printf("Set location to %s%n", source.get("location"));

                  /* we add a message handler */
                  bus = pipeline.getBus();
                  bus.addWatch(new BusFunc() {
                        @Override
                        public boolean callback(Bus arg0, Message msg, Pointer arg2) {
                                System.out.printf("Got Message type: %d%n", msg.type);
                                if (msg.type == MessageType.EOS) {
                                        System.out.println("End of stream");
                                        loop.quit();
                                } else if (msg.type == MessageType.ERROR) {
                                        System.err.println("error!");
                                        
                                        loop.quit();
                                }
                                return true;
                        }
                  }, null);

                  /* we add all elements into the pipeline */
                  /* file-source | ogg-demuxer | vorbis-decoder | converter | alsa-output */
                  pipeline.add(source);
                  pipeline.add(demuxer);
                  pipeline.add(decoder);
                  pipeline.add(conv);
                  pipeline.add(sink);

                  /* we link the elements together */
                  /* file-source -> ogg-demuxer ~> vorbis-decoder -> converter -> alsa-output */
                  source.link(demuxer);
                  decoder.link(conv);
                  conv.link(sink);
                  demuxer.connect(new PadAdded() {
                        @Override
                        public void onPadAdded(Element src, Pad pad) {
                                  /* We can now link this pad with the vorbis-decoder sink pad */
                                  System.out.println("Dynamic pad created, linking demuxer/decoder");

                                  Pad sinkpad = decoder.getStaticPad("sink");

                                  pad.link(sinkpad);
                        }                         
                  });

                  /* note that the demuxer will be linked to the decoder dynamically.
                     The reason is that Ogg may contain various streams (for example
                     audio and video). The source pad(s) will be created at run time,
                     by the demuxer when it detects the amount and nature of streams.
                     Therefore we connect a callback function which will be executed
                     when the "pad-added" is emitted.*/


                  /* Set the pipeline to "playing" state*/
                  System.out.printf("Now playing: %s%n", args[0]);
                  pipeline.setState(State.PLAYING);


                  /* Iterate */
                  System.out.println("Running...");
                  loop.run();

                  /* Out of the main loop, clean up nicely */
                  System.out.println("Returned, stopping playback");
                  pipeline.setState(State.NULL);

                  System.out.println("Deleting pipeline");
        }
}

Clutter

From the Clutter Tutorial, Chapter 6

package org.fishsoup.jgir;

import org.gnome.gir.dynamic.Clutter.ClutterGlobals;
import org.gnome.gir.dynamic.Clutter.Color;
import org.gnome.gir.dynamic.Clutter.Rectangle;
import org.gnome.gir.dynamic.Clutter.RotateAxis;
import org.gnome.gir.dynamic.Clutter.Stage;
import org.gnome.gir.dynamic.Clutter.Timeline;
import org.gnome.gir.gobject.GObjectGlobals;

public class TimelineExample {
        public static Color color(int r, int g, int b, int a) {
                return new Color((byte)r, (byte)g, (byte)b, (byte)a);
        }
        
        private static class TimelineDelegate implements Timeline.NewFrame {
                Rectangle rect;
                int rotationAngle = 0;
                int colorChangeCount = 0;
                
                public TimelineDelegate(Rectangle rect) {
                        this.rect = rect;
                }
                
                public void onNewFrame(Timeline timeline, int frameNumber) {
                        rotationAngle += 1;
                        if (rotationAngle > 360)
                                rotationAngle = 0;
                        
                        rect.setRotation(RotateAxis.X_AXIS, (double)rotationAngle, 0, 0, 0);
                        
                        colorChangeCount += 1;
                        if (colorChangeCount > 100)
                                colorChangeCount = 0;
                        
                        if (colorChangeCount == 0) {
                                Color rectColor = color(0xff, 0xff, 0xff, 0x99);
                                rect.setColor(rectColor);
                        } else if (colorChangeCount == 50) {
                                Color rectColor = color(0x10, 0x40, 0x90, 0xff);
                                rect.setColor(rectColor);
                        }
                }
        }
        
        public static void main(String[] args) {
                GObjectGlobals.init();
                ClutterGlobals.init(null, null);
                
                Color stageColor = color(0, 0, 0, 0xff);
                Color actorColor = color(0xff, 0xff, 0xff, 0x99);
                
                Stage stage = (Stage)ClutterGlobals.stageGetDefault();
                stage.setSize(200, 200);
                stage.setColor(stageColor);
                
                Rectangle rect = new Rectangle(actorColor);
                rect.setSize(70, 70);
                rect.setPosition(50, 100);
                stage.addActor(rect);
                rect.show();
                
                Timeline timeline = new Timeline(10 /* frames */, 120 /* frames per second */);
                timeline.connect(new TimelineDelegate(rect));
                timeline.setLoop(true);
                timeline.start();
                
                stage.show();
                
                ClutterGlobals.main();
        }
}

Hippo Canvas

See the original Python source for this example.

package org.verbum;

import java.util.HashMap;

import org.gnome.gir.dynamic.Gdk.Event;
import org.gnome.gir.dynamic.Gtk.GtkGlobals;
import org.gnome.gir.dynamic.Gtk.Widget;
import org.gnome.gir.dynamic.Gtk.Window;
import org.gnome.gir.dynamic.Gtk.WindowType;
import org.gnome.gir.dynamic.HippoCanvas.Box;
import org.gnome.gir.dynamic.HippoCanvas.Canvas;
import org.gnome.gir.dynamic.HippoCanvas.ItemAlignment;
import org.gnome.gir.dynamic.HippoCanvas.Orientation;
import org.gnome.gir.gobject.GErrorException;

@SuppressWarnings("serial")
public class HippoCanvasTest {

        public static void main(String[] args) throws GErrorException {
                GtkGlobals.initCheck(null, null);
                Window w = new Window(WindowType.TOPLEVEL);
                
                w.connect(new Widget.DeleteEvent() {
                        @Override
                        public boolean onDeleteEvent(Widget w, Event arg0) {
                                GtkGlobals.mainQuit();
                                return true;
                        }
                });
                
                Box box = new Box(new HashMap<String,Object>() {
                        {
                                put("background-color", 0xFFFFFFFF);
                                put("xalign", ItemAlignment.FILL);
                                put("yalign", ItemAlignment.FILL);
                                put("orientation", Orientation.HORIZONTAL);
                        }
                });
                Canvas c = new Canvas();
                w.add(c);
                c.show();
                c.setRoot(box);
                c.setSizeRequest(100, 100);
                
                for (final int color : new int[] { 0xffff00ff, 0x000000ff, 0x444444ff, 0x666666ff, 0xaaaaaaff }) {
                        Box b2 = new Box(new HashMap<String,Object>() {
                                {
                                        put("background-color", color);
                                        put("box-width", 20);
                                        put("box-height", 20);
                                }
                        });
                        box.append(b2, 0);                      
                }

                w.show();
                GtkGlobals.main();
        }

}

libnotify

package org.verbum;

import org.gnome.gir.dynamic.Gtk.Widget;
import org.gnome.gir.dynamic.Notify.ActionCallback;
import org.gnome.gir.dynamic.Notify.Notification;
import org.gnome.gir.dynamic.Notify.NotifyGlobals;
import org.gnome.gir.gobject.MainLoop;

import com.sun.jna.Pointer;

public class NotifyTest {

        private static final String MESSAGE = "This is a test of the emergency Java-GObject-Introspection bindings. "
                +"This is only a test.  Do not panic.";
        
        public static void main(String[] args) {
                NotifyGlobals.init("MyApp");
                
                MainLoop loop = new MainLoop();
                
                Notification notify = new Notification("Test Notification", MESSAGE, "gtk-info", (Widget)null);
                notify.addAction("foo", "FOO", new ActionCallback() {
                        @Override
                        public void callback(Notification arg0, String arg1, Pointer arg2) {
                                System.out.printf("User pressed %s%n", arg1);
                        }                       
                }, null, null);
                notify.show(null);
                loop.run();
        }
}

Gio

See the original C source for this example.

package org.verbum;

import org.gnome.gir.dynamic.Gio.File;
import org.gnome.gir.dynamic.Gio.FileMonitor;
import org.gnome.gir.dynamic.Gio.FileMonitorEvent;
import org.gnome.gir.dynamic.Gio.FileMonitorFlags;
import org.gnome.gir.dynamic.Gio.GioGlobals;
import org.gnome.gir.gobject.GObjectGlobals;
import org.gnome.gir.gobject.MainLoop;

public class GvfsMonitorTest {
        
        public static void main(String...args) {
                GObjectGlobals.init();
                
                File f = GioGlobals.fileNewForCommandlineArg(args[0]);
                FileMonitor fm = f.monitorFile((int) FileMonitorFlags.WATCH_MOUNTS, null);
                System.err.printf("Returned File stub: %s%n", f);

                fm.connect(new FileMonitor.Changed() {
                        @Override
                        public void onChanged(FileMonitor monitor, File child, File otherFile,
                                        FileMonitorEvent eflags) {
                                  System.err.println("File Monitor Event:");
                                  System.err.printf("File = %s%n", child.getParseName());
                                  switch (eflags)
                                    {
                                    case CHANGED:
                                      System.out.println("Event = CHANGED");
                                      break;
                                    case CHANGES_DONE_HINT:
                                      System.out.println("Event = CHANGES_DONE_HINT");
                                      break;
                                    case DELETED:
                                      System.out.println("Event = DELETED");
                                      break;
                                    case CREATED:
                                      System.out.println ("Event = CREATED");
                                      break;
                                    case UNMOUNTED:
                                      System.out.println ("Event = UNMOUNTED");
                                      break;
                                    case PRE_UNMOUNT:
                                      System.out.println("Event = PRE_UNMOUNT");
                                      break;
                                    case ATTRIBUTE_CHANGED:
                                      System.out.println("Event = ATTRIB CHANGED");
                                      break;
                                    }
                        }                       
                });
                
                new MainLoop().run();
        }
}

GooCanvas

See the original C source for this example.

package org.verbum;

import java.util.HashMap;

import org.gnome.gir.dynamic.Gdk.Event;
import org.gnome.gir.dynamic.GooCanvas.Canvas;
import org.gnome.gir.dynamic.GooCanvas.Item;
import org.gnome.gir.dynamic.GooCanvas.Rect;
import org.gnome.gir.dynamic.GooCanvas.Text;
import org.gnome.gir.dynamic.Gtk.AnchorType;
import org.gnome.gir.dynamic.Gtk.GtkGlobals;
import org.gnome.gir.dynamic.Gtk.ScrolledWindow;
import org.gnome.gir.dynamic.Gtk.ShadowType;
import org.gnome.gir.dynamic.Gtk.Widget;
import org.gnome.gir.dynamic.Gtk.Window;
import org.gnome.gir.dynamic.Gtk.WindowType;

public class GooCanvasTest {

        @SuppressWarnings("serial")
        public static void main(String[] args) {
                GtkGlobals.initCheck(null, null);
                  
                    /* Create the window and widgets. */
                Window window = new Window(WindowType.TOPLEVEL);
                window.setDefaultSize(640, 600);
                window.show();
                window.connect(new Widget.DeleteEvent() {
                        @Override
                        public boolean onDeleteEvent(Widget arg0, Event arg1) {
                                GtkGlobals.mainQuit();
                                return true;
                        }
                });

                ScrolledWindow scrolledWin = new ScrolledWindow();
                scrolledWin.setShadowType(ShadowType.IN);
                scrolledWin.show();
                window.add(scrolledWin);

                Canvas canvas = new Canvas();
                canvas.setSizeRequest(600, 450);
                canvas.setBounds(0.0, 0.0, 1000.0, 1000.0);
                canvas.show();
                scrolledWin.add(canvas);
                final Item root = canvas.getRootItem();

                Rect rect = new Rect(new HashMap<String,Object>() {
                        {
                                put("parent", root);
                                put("x", 100.0);
                                put("y", 100.0);
                                put("width", 400.0);
                                put("height", 400.0);
                                put("line-width", 10.0);
                                put("radius-x", 20.0);
                                put("radius-y", 10.0);
                                put("stroke-color", "yellow");
                                put("fill-color", "red");                               
                        }
                });

                Text text = new Text(new HashMap<String,Object>() {
                        {
                                put("parent", root);
                                put("text", "Hello World");
                                put("x", 300.0);
                                put("y", 300.0);
                                put("width", -1.0);
                                put("anchor", AnchorType.CENTER);
                                put("font", "Sans 24");
                        }
                });
                text.rotate(45.0, 300.0, 300.0);

                /* This handles button presses in item views. We simply output a message to
             the console. */
                rect.connect(new Item.ButtonPressEvent() {
                        @Override
                        public Boolean onButtonPressEvent(Item arg0, Item arg1, Event arg2) {
                            System.out.printf("rect item received button press event%n");
                                return true;
                        }
                });

                GtkGlobals.main();
        }
}

GConf

See the original C source for this example.

package org.verbum;

import org.gnome.gir.dynamic.GConf.Client;
import org.gnome.gir.dynamic.GConf.ClientNotifyFunc;
import org.gnome.gir.dynamic.GConf.ClientPreloadType;
import org.gnome.gir.dynamic.GConf.Entry;
import org.gnome.gir.dynamic.GConf.GConfGlobals;
import org.gnome.gir.dynamic.Gdk.Event;
import org.gnome.gir.dynamic.Gtk.GtkGlobals;
import org.gnome.gir.dynamic.Gtk.Label;
import org.gnome.gir.dynamic.Gtk.Widget;
import org.gnome.gir.dynamic.Gtk.Window;
import org.gnome.gir.dynamic.Gtk.WindowType;
import org.gnome.gir.gobject.GErrorException;

import com.sun.jna.Pointer;

public class GConfTest {
        
        public static void main(String...args) throws GErrorException {
                GtkGlobals.init(null, null);
                GConfGlobals.init(null, null);
                
                Window w = new Window(WindowType.TOPLEVEL);
                w.setSizeRequest(320, 240);
                
                final Client client = GConfGlobals.clientGetDefault();
                
                boolean bg = client.getBool("/desktop/gnome/background/draw_background");
                
                final Label label = new Label("drawing background:" + bg);
                w.add(label);
                
                client.addDir("/desktop/gnome/background", ClientPreloadType.PRELOAD_NONE);
                client.notifyAdd("/desktop/gnome/background/draw_background", 
                                new ClientNotifyFunc() {
                                        @Override
                                        public void callback(Client arg0, int arg1, Entry arg2,
                                                        Pointer arg3) {
                                                boolean bg;
                                                try {
                                                        bg = client.getBool("/desktop/gnome/background/draw_background");
                                                } catch (GErrorException e) {
                                                        e.printStackTrace();
                                                        throw new RuntimeException(e);
                                                } 
                                                label.setText("drawing background:" + bg);
                                        }
                        
                }, null, null);
                
                w.connect(new Widget.DeleteEvent() {
                        @Override
                        public boolean onDeleteEvent(Widget arg0, Event arg1) {
                                GtkGlobals.mainQuit();
                                return true;
                        }
                });
                
                w.showAll();
                GtkGlobals.main();
        }
}


Back to JGIR.

See JavaScript (Rhino) examples.


2024-10-23 11:37