Created
February 26, 2016 08:10
-
-
Save bugabinga/358b4a4fce069d74b419 to your computer and use it in GitHub Desktop.
Example of binding a nodes size to answer Stackoverflow question https://stackoverflow.com/questions/35632807/javafx-8-monopoly-board-layout-with-image-in-center-of-board .
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.isp.stackoverflow; | |
import javafx.application.Application; | |
import javafx.beans.binding.DoubleBinding; | |
import javafx.scene.Scene; | |
import javafx.scene.image.ImageView; | |
import javafx.scene.layout.ColumnConstraints; | |
import javafx.scene.layout.GridPane; | |
import javafx.scene.layout.Pane; | |
import javafx.scene.layout.RowConstraints; | |
import javafx.scene.layout.StackPane; | |
import javafx.stage.Stage; | |
public class SpelbordView extends GridPane | |
{ | |
public Pane[] panes = new Pane[40]; | |
public SpelbordView() | |
{ | |
layoutNodes(); | |
} | |
private void layoutNodes() | |
{ | |
final RowConstraints rowsEdge = new RowConstraints(); | |
rowsEdge.setPercentHeight( 14 ); | |
final RowConstraints rowsMid = new RowConstraints(); | |
rowsMid.setPercentHeight( 8 ); | |
final ColumnConstraints colEdge = new ColumnConstraints(); | |
colEdge.setPercentWidth( 14 ); | |
final ColumnConstraints colMid = new ColumnConstraints(); | |
colMid.setPercentWidth( 8 ); | |
this.getColumnConstraints().addAll( colEdge, colMid, colMid, colMid, colMid, colMid, colMid, colMid, colMid, colMid, colEdge ); | |
this.getRowConstraints().addAll( rowsEdge, rowsMid, rowsMid, rowsMid, rowsMid, rowsMid, rowsMid, rowsMid, rowsMid, rowsMid, rowsEdge ); | |
final StackPane imagePane = new StackPane(); | |
this.add( imagePane, 1, 1, 9, 9 ); | |
final DoubleBinding multipliedHeight = this.heightProperty().multiply( 0.72 ); | |
final DoubleBinding multipliedWidth = this.widthProperty().multiply( 0.72 ); | |
imagePane.maxHeightProperty().bind( multipliedHeight ); | |
imagePane.maxWidthProperty().bind( multipliedWidth ); | |
imagePane.minHeightProperty().bind( multipliedHeight ); | |
imagePane.minWidthProperty().bind( multipliedWidth ); | |
imagePane.prefHeightProperty().bind( multipliedHeight ); | |
imagePane.prefWidthProperty().bind( multipliedWidth ); | |
final ImageView imageView = | |
new ImageView( "http://1.bp.blogspot.com/-Wjc79oqi1y0/VHitLAU44BI/AAAAAAAAG80/0UZ9n2JmvEo/s1600/Logo%2BMonopoly.png" ); | |
imageView.setPreserveRatio( true ); | |
imageView.fitWidthProperty().bind( imagePane.widthProperty().divide( 2 ) ); | |
imagePane.setStyle( "-fx-background-color: red;" ); | |
imagePane.getChildren().add( imageView ); | |
this.setGridLinesVisible( true ); | |
} | |
public static class Monopoly extends Application | |
{ | |
@Override | |
public void start( final Stage primaryStage ) throws Exception | |
{ | |
final Scene scene = new Scene( new SpelbordView(), 800, 600 ); | |
primaryStage.setScene( scene ); | |
primaryStage.show(); | |
} | |
public static void main( final String[] args ) | |
{ | |
launch( args ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment