1. What is this document?

This document is the index to the Djnn Java recipes. This cookbook is made for programmers who wish to learn from examples. It contains fragments of code that each show how to a achieve a given task: drawing a rectangle, opening multiple windows, connecting components, etc. Most examples are executable as is, some are not. Some address basic needs, while others are more complex recipes.

Some examples are executable. If you want to test them, copy and paste the code into your favorite IDE, compile and execute it, supposing that Djnn is properly installed on your computer.

2. Drawing

2.1. Windowing

How to open a window on the screen.

/* required imports */
import net.djnn.java.core.Application;
import net.djnn.java.core.Component;
import net.djnn.java.gui.Frame;
import net.djnn.java.gui.Text;

public class HelloWorld {
  public static void main (String[] args) {
    /* Create an application and init required additional modules*/
    Application app = new Application ();
    app.initModule (Module.GUI);

    /* Create the root component */
    Component root = new Component (null, "root");

    /* add children components, here a frame and a text */
    new Frame (root, "frame", "title", 10, 10, 300, 300);
    new Text (root, "text", 50, 50, "Hello World!");

    /* start the root component (this will also start its children) */
    root.run ();

    /* and don't forget this last command to run the mainloop */
    app.startMainloop ();
  }
}

How to create several windows and draw in them.

import net.djnn.java.core.Application;
import net.djnn.java.core.Component;
import net.djnn.java.gui.*;

public class Frames {
  public static void main (String[] args) {
    /* Create an application and init required additional modules*/
    Application app = new Application ();
    app.initModule (Module.GUI);

    /* For creating multiple frames you need a root component to handle them */
    Component root = new Component (null, "root");

    /* create a first frame and draw a rectangle */
    new Frame (root, "frame", "f1", 10, 10, 300, 200);
    new Rectangle (root, "r1", 40, 20, 40, 40, 5, 5);

    /* create a second frame and draw a circle */
    /* Note that all frames and shapes have the same parent, the root component.*/
    new Frame (root, "frame", "f2", 310, 10, 150, 100);
    new Circle (root, "c1", 30, 25, 15);

    root.run ();
    app.startMainloop();
  }
}

2.2. Graphical objects

How to create shapes in a window.

import net.djnn.java.core.Application;
import net.djnn.java.core.Component;
import net.djnn.java.gui.*;

public class DrawingShapes {
  public static void main (String[] args) {
    /* Create an application and init required additional modules*/
    Application app = new Application ();
    app.initModule (Module.GUI);

    Component root = new Component (null, "root");

    /* The available shapes are rectangle, circle, ellipse, line, polygon, and paths,
       For the rectangle, rx and ry parameters can be used for rounded rectangle */
    new Frame (root, "frame", "f1", 10, 10, 400, 300);
    new Rectangle (root, "rectangle", 40, 40, 40, 40, 5,5);
    new Ellipse (root, "ellipse", 100, 25, 30, 20);
    new Circle (root, "circle", 150, 25, 15);
    new Line (root, "line", 170, 10, 250, 40);
    Polygon p = new Polygon (root, "polygon");
    p.addPoint (10, 100);
    p.addPoint (50, 130);
    p.addPoint (25, 150);
    p.addPoint (10, 150);

    Polyline pl = new Polyline (root, "polyline");
    pl.addPoint (90, 100);
    pl.addPoint (130, 130);
    pl.addPoint (110, 150);
    pl.addPoint (75, 130);

    Path path = new Path (root, "path");
    path.addSubpath (50, 200);
    path.addLine (150, 250);
    path.addQuadratic (250, 220, 170, 230);
    path.addCubic (300, 270, 350, 300, 330, 150);
    path.closeSubpath ();

    root.run ();

    app.startMainloop ();
  }
}

How to set and change the color of shapes.

import net.djnn.java.core.Application;
import net.djnn.java.core.Component;
import net.djnn.java.gui.Circle;
import net.djnn.java.gui.Ellipse;
import net.djnn.java.gui.Frame;
import net.djnn.java.gui.Rectangle;
import net.djnn.java.gui.style.FillColor;
import net.djnn.java.gui.style.Gradient.FillCoords;
import net.djnn.java.gui.style.Gradient.FillSpread;
import net.djnn.java.gui.style.LinearGradient;
import net.djnn.java.gui.style.OutlineWidth;

public class Style {
  public static void main (String[] args) {
    Application app = new Application ();
    app.initModule (Module.GUI);

    Component root = new Component (null, "root");
    new Frame (root, "frame", "f1", 10, 10, 300, 200);
    /* each style feature is conceived as a component to be added in the component tree
       a style component will be used for drawing all (drawable) components added after it until it is redefined */

    new FillColor (root, "fc", 10, 210, 150);
    new Rectangle (root, "rectangle", 40, 40, 40, 40, 5, 5);

    new OutlineWidth (root, null, 5);
    new Ellipse (root, "ellipse", 100, 25, 30, 20);

    new OutlineWidth (root, null, 1);
    new Circle (root, "circle", 150, 25, 15);

    LinearGradient lg = new LinearGradient (root, null, 150, 20, 290, 20, FillSpread.PAD, FillCoords.RENDERING);
    lg.addGradientStop (132, 167, 191, 1, 0);
    lg.addGradientStop (213, 220, 224, 1, 1);

    new Rectangle (root, null, 150, 100, 100, 40, 5, 5);
    root.run ();

    app.startMainloop ();

  }
}

How to add transformation components to modify shapes.

import net.djnn.java.core.Application;
import net.djnn.java.core.Component;
import net.djnn.java.gui.Frame;
import net.djnn.java.gui.Rectangle;
import net.djnn.java.gui.transformation.Rotation;
import net.djnn.java.gui.transformation.Scaling;
import net.djnn.java.gui.transformation.SkewX;
import net.djnn.java.gui.transformation.Translation;

public class Transform {
  public static void main (String[] args) {
    Application app = new Application ();
    app.initModule (Module.GUI);

    Component root = new Component (null, "root");
    new Frame (root, "frame", "f1", 10, 10, 300, 400);
    new Rotation (root, "rot", 45, 0, 0);
    new Scaling (root, "s", 2, 1, 0, 0);
    new Translation (root, "t1", 50, 0);
    new SkewX (root, "sk", 30);
    new Rectangle (root, "rectangle", 40, 40, 40, 40, 5, 5);

    root.run ();

    app.startMainloop ();
  }
}

2.3. Loading graphics

How to load graphical components from a SVG file.

import net.djnn.java.core.Application;
import net.djnn.java.core.Component;

public class ImporSvg {
  public static void main (String[] args) {
    Application app = new Application ();
    app.initModule (Module.GUI);
    String URI = args [0];
    Component root = new Component (null, "root");
    new Frame (root, "frame", "f1", 10, 10, 300, 400);
    Element svg = Component.load (URI);
    root.addChild (svg.findElement ("layer1/myButton"), "myButton"); // The name here is the path to
                                                                     // your graphical component in the SVG tree
    root.run ();
    app.startMainloop ();
}

3. Behaviors

4. Creating the component tree

4.1. Building components

Creating and using a Java component class that you can instantiate at will.

import net.djnn.java.base.Switch;
import net.djnn.java.core.Component;
import net.djnn.java.core.Connector;
import net.djnn.java.core.Element;
import net.djnn.java.base.FSM;
import net.djnn.java.gui.Rectangle;
import net.djnn.java.gui.Text;
import net.djnn.java.gui.style.FillColor;

public class Button {
  public Button (Element parent, String name, Element action, double x, double y) {
    super (parent, name);
    new Translation (this, "t", x, y);
    Switch sw = new Switch (this, "sw", "released");
    new FillColor (sw, "released", 186, 206, 222);
    new FillColor (sw, "pressed", 67, 152, 222);

    new Rectangle (this, "rec", 0, 0, 50, 19, 5, 5);
    new FillColor (this, null, 255, 255, 255);
    new Text (this, "label", 15, 20, "OK");
    FSM fsm = new FSM (this, "fsm");
    fsm.addState ("released");
    fsm.addState ("pressed");
    fsm.addTransition ("t1", "released", "pressed", "../rec/press", null);
    fsm.addTransition ("t2", "released", "pressed", "../label/press", null);
    fsm.addTransition ("t3", "pressed", "released", "../rec/release", action); //Trigger the action on release
    new Connector (this, null, "fsm/state", "sw/state");
  }
}

Using the button

import net.djnn.java.core.Application;
import net.djnn.java.core.Component;
import net.djnn.java.core.Element;
import net.djnn.java.core.ICallback;
import net.djnn.java.core.NativeAction;
import net.djnn.java.gui.Frame;

public class TestButton {
  public static void main (String[] args) {
    Application app = new Application ();
    app.initModule (Module.GUI);

    Component root = new Component (null, "root");
    new Frame (root, "frame", null, 0, 0, 400, 300);
    Element n = new NativeAction (root, "n", 1) {

      @Override
      public void callback (Element e) {
        System.out.println ("OK");
      }
    };
    new Button (root, "b1", n, 20, 20);

    root.run ();
    app.startMainloop ();
  }
}

5. Execution

6. Adding extensions in Java

How to add external code to your Djnn application or create new modules in Java.

6.1. Creating new native components

A component with its rendering implemented in Java, and its use.

import net.djnn.java.core.Element;
import net.djnn.java.core.JavaComponent;

public class MyPrinter extends JavaComponent {
  public class MyPrinter extends JavaComponent {
    private String toPrint;
    public MyPrinter (Element parent, String name, String toPrint) {
      super (parent, name);
      this.toPrint = toPrint;
    }

    @Override
    void Run () {
      System.out.println (toPrint);
    }

    @Override
    void Stop () {
      System.out.println ("Stopping MyPrinter");
    }
  }
}
import net.djnn.java.core.Application;
import net.djnn.java.core.Component;

public class TestCustomComponent {
  public static void main (String[] args) {
    new Application ();
    Component root = new Component (null, "root");
    new MyPrinter (root, "printer1");
    new MyPrinter (root, "printer2");
    root.run ();
  }
}