Java etc...


zoom RSS The JavaFX Properties binding(1)

<<   作成日時 : 2013/01/12 12:47   >>

ブログ気持玉 0 / トラックバック 0 / コメント 0

The JavaFX Properties binding for computing the area of triangle(1)

As I wrote a previous article,
I found that JavaFX program using Properties' binding is in the Chapter 3, the book of "JavaFX 2 Pro".

That is the code that the triangle area can be computed by the following fomula,

Area = |(x1*y2 + x2*y3 + x3*y1 - x1*y3 - x2*y1 - x3*y2)| / 2

where the three vertices is at (x1, y1), (x2, y2), (x3, y3) on Cartesian plane.

Here is the source code, which is almost same as the code of Listing 3-6 in the JavaFX 2 Pro book.

public class TriangleAreaFluentExample {
  public static void main(String[] args) {
    IntegerProperty x1 = new SimpleIntegerProperty(0);
    IntegerProperty y1 = new SimpleIntegerProperty(0);
    IntegerProperty x2 = new SimpleIntegerProperty(0);
    IntegerProperty y2 = new SimpleIntegerProperty(0);
    IntegerProperty x3 = new SimpleIntegerProperty(0);
    IntegerProperty y3 = new SimpleIntegerProperty(0);

    final NumberBinding area =

    StringExpression output = Bindings.format(
      "3点 A(%d,%d), B(%d,%d), C(%d,%d) " +
            "の三角形の面積は %3.1f",
                  x1, y1, x2, y2, x3, y3, area);

    x1.set(0); y1.set(0);
    x2.set(6); y2.set(0);
    x3.set(4); y3.set(3);

    x1.set(1); y1.set(0);
    x2.set(2); y2.set(2);
    x3.set(0); y3.set(1);

    x1.set(10); y1.set(10);
    x2.set(2); y2.set(2);
    x3.set(5); y3.set(1);

The result of colculation of triangles area print out on console

The first time I saw the code, It is really interesting.
It's JavaFX code, but it's has no UI Controls !!

Now, I would like to add some UIs on the code.

First of all, I think it's a sort of ideas anyone can come up with,
trying to giving the three vertices by mouse clicking on the screen,
instead of by program code.

A small Circle(like a dot) is appears at the mouse clicked points, it's at most 3 points.
And also, The Line is drawn between each small circles.
Therefore, there are three Circles and Three Lines.

Some DoubleProperties introduced into the code as two of three elements of arrays.
One array is for x-coordinate and the other array is for y-coordinate of the three vertices,
which are setted the value of the SceneX and SceneY of MouseEvents raised when mouse is clicked.

To show up small Circle at the mouse clicked,
the Circle's Propertes centerX and centerY is bound to the DoubleProperties.
Also, To draw Line between two Circles,
the Line' Properties startX, startY, endX and endY is bound to the two Circle's Property centerX and centerY.

After the 3 mouse clicked occures, then the area of triangle is computed.
The code for computaion of the area is almost same as original code.
The extra code for getting the absolute value is added.
It use the When class and its then() and otherwise() methoed.
It's interesting class and methods as well.

Here is the source code.

import javafx.application.Application;
import javafx.beans.binding.NumberBinding;
import javafx.beans.binding.When;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.GroupBuilder;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.LineBuilder;
import javafx.scene.shape.RectangleBuilder;
import javafx.stage.Stage;

public class TriangleArea0 extends Application {
  private int clickcount = 0;
  private Circle c[] = new Circle[3];
  private Line l[] = new Line[3];
  private DoubleProperty x[] = new SimpleDoubleProperty[3];
  private DoubleProperty y[] = new SimpleDoubleProperty[3];

  private NumberBinding area;

    for(int i = 0; i < 3; i++ ) {
        c[i] = new Circle();
        l[i] = new Line();
        x[i] = new SimpleDoubleProperty(0);
        y[i] = new SimpleDoubleProperty(0);

  public TriangleArea0() {

    for(int i = 0; i < 3; i++) {
      int j = (i == 2) ? 0 : i+1;



      tmp_area = x[0].multiply(y[1])

    area = new When(tmp_area.lessThan(0))

  private void initialize() {
    clickcount = 0;

  private void print_area() {
        "A = (" + x[0].get() + ", " + y[0].get() + ")");
        "B = (" + x[1].get() + ", " + y[1].get() + ")");
        "C = (" + x[2].get() + ", " + y[2].get() + ")");
    System.out.println("Area = " + area.doubleValue());

  @Override public void start(Stage stage) {
    final Group root;
    Scene scene = SceneBuilder.create()
      .root(root = GroupBuilder.create()
          .onMouseClicked(new EventHandler<MouseEvent>() {
            @Override public void handle(MouseEvent e) {
              if (clickcount > 2) { initialize(); }
              for (int j = clickcount; j < 3; j++) {
              if (clickcount == 3) { print_area(); }
        l[0], l[1], l[2],
        c[0], c[1], c[2]

    stage.setTitle("Triangle Area");

  public static void main(String[] args) {
The virtices is given by mose clicks

While the the code is running,
the first mouse click is indicated the first vertix is at the clicked potision,
and the small circle is shown at the position of the screen.
With a second click, other small circle is shown at the position,
and also the Line between the centers of 2 small circles is drawn.
With a third click, another small circle is shown at the position,
and the two other Lines are drawn.
The area of triangle is printed out the standard output.
The value of Area is expressed in terms of square pixels.

The occuring of fourth click is handling with the same way as the occuring the first click.

This code dose not output the area of triangle on the UI Screen. Next, try to output the area of triangle on the screen

(due to the limitation of the number of characters in a article) to be cotinuted ...


注目テーマ 一覧





タイトル (本文) ブログ名/日時

トラックバック用URL help

自分のブログにトラックバック記事作成(会員用) help

本 文


内 容 ニックネーム/日時

コメントする help

本 文
The JavaFX Properties binding(1) Java etc.../BIGLOBEウェブリブログ
文字サイズ:       閉じる