« Disposing Virtualizing Stack Panel | Main | Creating WPF FlowDocuments on the non-UI thread »

Separated at birth? JavaFX and WPF/XAML?

There's a buzz about JavaFX on the net, so I thought I'd spend just a moment taking a look.

Rather than me trying to describe it (other than to say, "Silverlight", "Apollo", "Flash"), here's what the web page says:

JavaFX Script is a highly productive scripting language that enables content developers to create rich media and content for deployment on Java environments. JavaFX Script is a declarative, statically typed programming language. It has first-class functions, declarative syntax, list-comprehensions, and incremental dependency-based evaluation. It can make direct calls to Java APIs that are on the platform. Since JavaFX Script is statically typed, it has the same code structuring, reuse, and encapsulation features (such as packages, classes, inheritance, and separate compilation and deployment units) that make it possible to create and maintain very large programs using Java technology. See the FAQ for more information.

There are a few demos available on that page, but I picked JavaFXPad. You'll need Java 1.5+ installed to run the demos. I was greeted with a XAMLPad clone and a simple demo. It looks like this:

So, that inspired me to do a brief comparison of something I'm more familiar with, WPF/XAML. For comparison though, here's the JavaFX code:

 

import javafx.ui.*;
import javafx.ui.canvas.*;
import javafx.ui.filter.*;

Group {
content:
[Rect {
x: 10
y: 10
width: 460
height: 140
fill: LinearGradient {
x1: 0
y1: 0
x2: 1
y2: 0
stops:
[Stop {offset: 0, color: green},
Stop {offset: .5, color: new Color(.5, 1, 0, 1)},
Stop {offset: 1, color: green}]
}
stroke: green
strokeWidth: 3
},
Text {
x: 120
y: 50
content: "JavaFX"
font: Font {face: VERDANA, style: [ITALIC, BOLD], size: 60}
fill: LinearGradient {
x1: 0, y1: 0, x2: 0, y2: 1
stops:
[Stop {
offset: 0.2
color: red
},
Stop {
offset: 0.5
color: orange
},
Stop {
offset: .8
color: red
}]
}
filter:
[Glow {
amount: 0.1
},
Noise {
monochrome: true
distribution: 0
}]
},
View {
transform: translate(160, 200)
content: Button {
icon: Image { url: "javafxpad/images/duke.gif" }
text: "Click Me!"
}
}]
}


Simple. And familiar. So, I threw together the XAML version:



 

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Canvas>
<Rectangle Stroke="green" StrokeThickness="3"
Width="460" Height="140"
Canvas.Left="10" Canvas.Top="10">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="green" Offset="0"/>
<GradientStop Color="#FFAED000" Offset="0.5"/>
<GradientStop Color="green" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<TextBlock Width="400" Height="86" Canvas.Left="53" Canvas.Top="36"
FontFamily="Verdana"
FontSize="60" FontStyle="Italic"
FontWeight="Bold"
Text="Not JavaFX"
TextWrapping="Wrap">
<TextBlock.Foreground>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="red" Offset="0"/>
<GradientStop Color="red" Offset="1"/>
<GradientStop Color="orange" Offset="0.51"/>
</LinearGradientBrush>
</TextBlock.Foreground>
</TextBlock>
<Button Width="172" Height="117" Padding="6,6,6,6" Canvas.Left="187" Canvas.Top="185">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"
Width="Auto" Height="Auto" Orientation="Horizontal">
<Image Width="100" Height="100" Source=""/>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="Click Me!"
TextWrapping="Wrap"/>
</StackPanel>
</Button>
</Canvas>
</Page>

 


Very close. The syntax is easier to read in JavaFX, only because there's no curly brackets. It would download faster to a client when compared to a Silverlight version -- but in the days of broadband, I'm not sure how big of a deal it would be. I don't know of a way to make the ugly fill noise they added in the Java version, but everything else is reasonably close.


It will be interesting to watch it progress. I'd imagine it being relatively easy to create a converter for the two environments if one was so inclined. I'm not. (XAML to JavaFX would probably be a lot simpler).

Help support my web site by searching and buying through Amazon.com (in assocation with Amazon.com).