Skip to main content

Binding to the points in a polygon - JavaFX Compiled

2 replies [Last post]
robmcq
Offline
Joined: 2007-07-08
Points: 0

(This was originally posted to the JavaFX wiki, sorry if this was inappropriate)

What I'd like to be able to do is create a group of circles, binding them to the points in a polygon.

Unfortunately, the points in the Polygon class are stored as a sequence of Numbers

While I'd like to do the following:

Group{
content:bind for(point in polygon.points) //polygon is of class javafx.scene.geometry.Polygon
Circle{
fill: Color{red:.1 green:.1 blue:1 opacity:.5}
centerX:bind point.x
centerY:bind point.y
radius:3
}
},

I can't for the fact that the Polygon.points attribute is not a sequence of point objects but a sequence of alternating x,y values.

Is there a way to bind Polygon.points to my group of Circles without keeping a separate list of point objects.

Something like this would also be great:

Group{
content:bind for(x, y in polygon.points by 2) //not correct JavaFX syntax, but trying to express my idea
Circle{
fill: Color{red:.1 green:.1 blue:1 opacity:.5}
centerX:bind x
centerY:bind y
radius:3
}
},

In this expression, I'm iterating through the list two elements at a time and binding them to x and y values. So every time polygon.points changes, these circles will update their position automatically. Is there any way currently to express this behavior in JavaFX?

Thanks in advance,

Robert

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
alexsch
Offline
Joined: 2007-07-12
Points: 0

You can use iteration by sequence index to avoid the problem.

[code]
import javafx.application.*;

import javafx.scene.*;
import javafx.scene.geometry.*;
import javafx.scene.paint.Color;

var polygon = Polygon {
points : [
10,10,
100,10,
100,100
]
fill: Color.YELLOW
};

Frame {
title: "MyApplication"
width: 200
height: 200
closeAction: function() { java.lang.System.exit( 0 );}
visible: true

stage: Stage {
content: Group{
content:bind for(index in [0.. sizeof polygon.points - 1]) if (index mod 2 == 0) then
Circle{
fill: Color{red:.1 green:.1 blue:1 opacity:.5}
centerX: bind polygon.points[index + 0]
centerY :bind polygon.points[index + 1]
radius:3
} else []
},
}
}
[/code]

robmcq
Offline
Joined: 2007-07-08
Points: 0

Pretty good workaround. I was able to come up with a similar solution by binding to functions that take my list of polygons as an argument.

Thanks very much for your prompt reply.

Robert