JavaFX Borders


When I am not busy with my day job or writing books on JavaFX, JavaFX - Developing Rich Internet Applications, I spend my spare time helping out on the open source JFXtras project, http://code.google.com/p/jfxtras/ . One part I have contributed is the borders package. The idea started off trying to duplicate some of the borders found in Swing. However, finding it relatively easy to implement the Border classes in JavaFX, the types of Borders started to grow.

The following screen snapshot shows the current set of  borders:

Border Ensemble

To create a border just import the Border package from the JFXtras project and then include it in your scenegraph. For example, to implement the PipeBorder from the ensemble above, the code looks like:

import org.jfxtras.scene.border.PipeBorder;
import javafx.scene.image.ImageView; 
import javafx.scene.image.Image;
def image = Image {
    url: "{__DIR__}images/Mona_Lisa.jpg"
    width: 100
    height: 150
    preserveRatio: true
};
var pipeBorder  = PipeBorder {
    content: ImageView { image: image }
}; 

All the borders are created as JavaFX Controls and have corresponding Skins that allow you to modify their appearance either programatically or via CSS Style sheets. For the PipeBorder example, it is possible to have the pipe raised or lowered. A lowered pipe has its raised variable set to false and reverses the coloring of the border. This ends up looking like:

Pipe Border Lowered

You can also control the coloring of the border through CSS Style like statements. In the above Ensemble, the FrameBorder used such a statement to get the coral colored border:

VBox {
   spacing: 5
   nodeHPos: HPos.CENTER
   content: [
      FrameBorder {
          style: "background-fill: coral;"
          content: ImageView { image: image }
      },
      Label { text: "FrameBorder" }
   ]
}

If we change this to another color, say aquamarine, we get the following:

Frame Border

One of the more interesting borders is the ShapeBorder.

Shape Border

This uses any JavaFX Shape object as a clip over the underlying content. In this case, the Mona Lisa image. This example uses the JFXtras Star2 Shape class.

import org.jfxtras.scene.border.ShapeBorder;
import org.jfxtras.scene.shape.Star2;

VBox {
    spacing: 5
    nodeHPos: HPos.CENTER
    content: [
        ShapeBorder {
            var anode: Node;
            shape: Star2 {
                centerX: bind anode.layoutBounds.width/2
                centerY: bind anode.layoutBounds.height/2
                outerRadius: bind anode.layoutBounds.width/2
                innerRadius: bind anode.layoutBounds.width/6
                count: 5
            }
            content: anode = ImageView { image: image }
            },
        Label { text: "ShapeBorder" }
   ]
}

As you can guess, there are way too many options to explain in a short blog, but I hope I have peaked your interest to check out the JFXtras project.

You can get the source code for the above examples from here and, of course, the Mona Lisa from here.

Next week is JavaONE and my schedule is already full to the max. See y'all there.


Comments:

Nice article. I'm just getting into javafx, it would be a great help knowing how the borders were implemented, as I imagine similar decorators can be implemented using the same pattern.

PS: By the way "I hope I have peaked your interest". You did that, but I think you mean "piqued".

Posted by mat on November 07, 2009 at 11:53 PM EST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

jimclarke

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today