Java Reference
In-Depth Information
When the program in Listing 7-11 is run, the UI in Figure
7-17
is displayed. It is a
JFrame
holding three Swing
components, a
JComponent
with overridden
paint()
and
getPreferredSize()
methods that makes it look like the
rectangle we saw in the earlier program, and two
JButtons
that will change the fill and the stroke of the rectangle.
Figure 7-17.
The
NoJavaFXSceneInSwingExample
program
Inasmuch as the custom-painted
JComponent
in
NoJavaFXSceneInSwingExample
is hard to maintain over the long
run, we replace it with the JavaFX
Rectangle
. This is done by replacing the Swing code with the equivalent
JFXPanel
code. Here is the Swing code:
canvas = new JComponent() {
@Override
public void paint(Graphics g) {
g.setColor(model.strokeColor);
g.fillRect(0, 0, 200, 200);
g.setColor(model.fillColor);
g.fillRect(10, 10, 180, 180);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
};
And here is the
JFXPanel
code:
canvas = new JFXPanel();
canvas.setPreferredSize(new Dimension(210, 210));
Platform.runLater(new Runnable() {
@Override
public void run() {
final Rectangle rectangle = new Rectangle(200, 200);
rectangle.setStrokeWidth(10);
rectangle.fillProperty().bind(model.fillProperty());
Search WWH ::
Custom Search