Skip to content

Category: english

JavaFX, Duke Potato

Do you know the toy Mr. Potato Head? Now meet the Java Potato.

[youtube]6NrUdp5XX_o[/youtube]

Duke images here from previous dukes I posted and other images from Open Clipart Project.

Java Web Start:

The code:

package dukepotato;

import javafx.application.Frame;
import javafx.application.Stage;
import javafx.scene.CustomNode;
import javafx.scene.Node;
import javafx.scene.Group;
import javafx.input.MouseEvent;
import javafx.scene.geometry.Circle;
import javafx.scene.paint.Color;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;

public class Img extends ImageView{
    public  attribute content: Node[];
    public  attribute src: String;

    private attribute endX = 0.0;
    private attribute endY = 0.0;
    private attribute startX = 0.0;
    private attribute startY = 0.0;

    override attribute translateX = bind endX;
    override attribute translateY = bind endY;
    override attribute blocksMouse = true;

    init {
        image = Image {
            url: "{__DIR__}/{src}"
        }
    }

    override attribute onMousePressed = function(e:MouseEvent):Void {
        startX = e.getDragX()-endX;
        startY = e.getDragY()-endY;
    }

    override attribute onMouseDragged = function(e:MouseEvent):Void {
        endX = e.getDragX()-startX;
        endY = e.getDragY()-startY;
    }
}

var dukesimages = ["duke1.png", "duke2.png", "duke3.png", "duke4.png"];
var dukes = for (image in dukesimages){
    Image {
        url: "{__DIR__}/{image}"
    }
}
var index = 0;
var duke = ImageView {
    x: 200, y:170
    image: bind dukes[index];
    onMouseClicked: function( e: MouseEvent ):Void {
        index = (index + 1) mod sizeof dukesimages;
    }
}

var hat = Img { src: "hat.png", x: 370 }
var partyhat = Img { src: "party_hat.png", x: 160, y: 5 }
var cap = Img { src: "cap.png", x: 230, y: 10 }
var cake = Img { src: "cake.png", x: 526, y: 190 }
var glove = Img { src: "glove.png", x: 338, y: 363 }
var baseball = Img { src: "baseball.png", x: 548, y:373 }
var pencil = Img { src: "pencil.png", x: 451, y: 365 }
var camera = Img { src: "camera.png", x: 125, y: 380 }
var coffee = Img { src: "coffee.png", x: 541, y: 114 }
var burger = Img { src: "burger.png", x: 542, y: 282 }
var diamond = Img { src: "diamond.png", x: 243, y: 383 }
var pliers = Img { src: "pliers.png", x: 20, y: 368 }
var rubikcube = Img { src : "rubikcube.png", x: 37, y: 295 }
var syringe = Img { src: "syringe.png", x: 35, y: 245 }
var hourglass = Img { src: "hourglass.png", x: 35, y: 127 }
var adventurehat = Img { src: "adventurehat.png", x: 8, y:30 }
var tie = Img { src: "tie.png", x: 547, y:35 }

Frame {
    title: "Duke Potato"
    width: 640
    height: 480
    closeAction: function() {
        java.lang.System.exit( 0 );
    }
    visible: true

    stage: Stage {
        content: [duke, hat, partyhat, cake, adventurehat, cap, glove,
            baseball, pencil, camera, coffee, burger, diamond,
            pliers, rubikcube, syringe, hourglass, tie]
    }
}
  • Lines 14 to 42 is the same dragging approach I showed in the post Draggable Nodes, but this time creating a class that inherits the behavior of ImageView.
  • Lines 44 to 57 is the Duke that changes when you click on it. It cycles over the dukesimages list.
  • Lines 59 to 75 is just instantiations of all toys and objects we will use to dress the Duke. Look how easier was to create and place a image.
  • Lines 78 to the end is just creating a Frame and putting all elements on it.

Download a package with the NetBeans project, sources, libraries and images, DukePotato.tar.gz.

Robot Fish

[youtube:http://youtube.com/watch?v=eO9oseiCTdk]

Description from video:

A robotic fish developed by scientists from Essex University is put through its paces in a special tank at the London Aquarium. It works via sensors and has autonomous navigational control.

Should be hard creating robotics to work underwater…

More Dukes

More Dukes we’re creating to use in the 6th CEJUG’s birthday.

The first one is a Duke intented to be used as a badge. We should tie string in both hands and transform it in a badge for the event.

Original SVG version: duke_badge.svg

The second is a general purpouse Duke sign. You can print them in blank and use a brush to write on it.

Original SVG version: duke_sign.svg

Java, showing script engines

A simple code to show the script engines installed in your system.

As it uses the JSR-223 you need at least java 6.

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngineFactory;
import java.util.List;

public class ListEngines {
    public static void main(String[] args){
	ScriptEngineManager manager = new ScriptEngineManager();
	List  engines = manager.getEngineFactories();
	for(ScriptEngineFactory engine: engines){
		String name = engine.getEngineName();
		String lang = engine.getLanguageName();
		String ver = engine.getLanguageVersion();
		System.out.println(name+" "+lang+" "+ver);
	}
    }
}

$ java -version
java version “1.6.0_0”
OpenJDK Runtime Environment (build 1.6.0_0-b11)
OpenJDK Client VM (build 1.6.0_0-b11, mixed mode, sharing)
$ javac ListEngines.java
$ java ListEngines
Mozilla Rhino ECMAScript 1.6

For now I just have Rhino ECMAScript (JavaScript) engine accessible that comes with Java 6. I’m trying now to call Jython and JRuby code.

Glassfish on Linux

First, check if you have Java installed:

$ java -version
java version “1.6.0_10-rc”
Java(TM) SE Runtime Environment (build 1.6.0_10-rc-b28)
Java HotSpot(TM) Client VM (build 11.0-b15, mixed mode, sharing)

Now download the last version of Glassfish application server, I’m using Glassfish V2 for Linux. It’s a jar package. Execute it.

$ java -Xmx256m -jar glassfish-installer-v2ur2-b04-linux.jar

You be asked about the license agreement (CDDL+GPL).

Files will be decompressed in a directory named glassfish.

$ cd glassfish

Now we need to continue the installation. You can have Ant installed in your system or, like I’m doing, use a version that comes the Glassfish package.

$ chmod +x -R lib/ant/bin
$ lib/ant/bin/ant -f setup.xml

Tip: if you have more servers and want to create a cluster for load balancing, see this documentation. After ajust the setup-cluster.xml file you just need to run ant on it.

Let’s execute the server:

$ bin/asadmin start-domain

The default instance port is 8080. Try it on http://localhost:8080.

The default administrator port is 4848. Try it on http://localhost:4848.

The default user is admin and password is adminadmin. Log in.

Tip: you be asked for register your version. This register is not mandatory and not doing it will not limit the features of your Glassfish. It’s just a way they use to track the number of users. Aditionaly can have acess to newsletters, tutorials, screencasts, services and support. If you already have a SDN (Sun Developer Network) or Sun Online account you can use it for registration. If you simply doesn’t want to, you can skip this step.

For security reassons, click in the Application Server icon on the left sidebar and so in the Adminstrator Password tab. Chose a new password and click Save.

Let’s deploy a application. Download the file http://glassfish.dev.java.net/downloads/quickstart/hello.war.

Go to Applications → Web Applications on the sidebar.

No application will be there. Click on the Deploy button.

In the next screen, on Location select the .war that you downloaded. Click Ok.

Your application is now deployed.

Try the application on http://localhost:8080/hello.

There other ways  less easy but more flexible) to deploy your application, you can take a look on this and others topics on Glassfish Quick Start Guide.

Try to explore the Glassfish admin interface. It’s very easy and intuitive.

JavaFX, comic balloon

A example of how flexible can be extending your own Custom Node. In this example I’m creating a comic ballon that can be simple created by:

Ballon {
   text: "I can has fx?"
}

That can also be incremented to work like this:

[youtube:http://br.youtube.com/watch?v=LexJbO1-ti4]

Here a simpler implementation of a balloon, without the dragging behavior but that can be used for creating comics.

import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.geometry.Ellipse;
import javafx.scene.geometry.Polygon;
import javafx.scene.geometry.ShapeSubtract;
import javafx.scene.paint.Color;
import javafx.scene.Font;
import javafx.scene.text.Text;
import javafx.scene.FontStyle;
import java.lang.Math;

public class Balloon extends CustomNode {  

    /* (cx,cy) center of the balloon */
    public attribute cx: Number = 100 on replace {
        distance = Math.sqrt(toX * toX + toY * toY);
    }
    public attribute cy: Number = 100 on replace {
        distance = Math.sqrt(toX * toX + toY * toY);
    }

    /* (toX, toY) point where balloon points at */
    public attribute toX: Number = cx on replace {
        distance = Math.sqrt(toX * toX + toY * toY);
    }
    public attribute toY: Number = cy on replace {
        distance = Math.sqrt(toX * toX + toY * toY);
    }

    /* what is writted in the balloon */
    public attribute text: String = "balloon";

    /* font for the text */
    public attribute font: Font = Font {
        size: 24
        style: FontStyle.PLAIN
    }

    /* Distance between (cx,cy) and (toX, toY) */
    private attribute distance: Number;

    /* Text inside the balloon */
    private attribute label = Text {
        font: bind font
        content: bind text
    }

    /* place the label correctly based on text */
    init {
        label.x = -label .getWidth() / 2;
        label.y = label.font.size / 2;
    }

    /* ballon body */
    private attribute body = ShapeSubtract{
        fill: Color.WHITE
        stroke: Color.BLACK
        blocksMouse: true
        a:  bind [
            Ellipse {
                radiusX: bind label.getWidth() / 2 + 20
                radiusY: bind font.size * 2
            },
            Polygon {
                points : [
                    10 * ( - toY / distance),
                    10 * (toX / distance),
                    10 * (toY / distance),
                    10 * ( - toX / distance),
                    toX,
                    toY
                ]
            }
        ]
    }

    public function create(): Node {
        return Group {
            cursor: Cursor.HAND
            content: [ body, label]
            translateX: bind cx
            translateY: bind cy
        }
    }
}