Created
March 1, 2016 09:44
-
-
Save bugabinga/172ff38a0f7f4d2300b4 to your computer and use it in GitHub Desktop.
Example for StackOverflow question http://stackoverflow.com/questions/35675705/javafx-resize-rotated-object-to-gridpane-cell
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.geometry.Insets; | |
import javafx.geometry.VPos; | |
import javafx.scene.Node; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Label; | |
import javafx.scene.layout.Background; | |
import javafx.scene.layout.BackgroundFill; | |
import javafx.scene.layout.ColumnConstraints; | |
import javafx.scene.layout.GridPane; | |
import javafx.scene.layout.Pane; | |
import javafx.scene.layout.Priority; | |
import javafx.scene.layout.RowConstraints; | |
import javafx.scene.layout.StackPane; | |
import javafx.scene.paint.Color; | |
import javafx.stage.Stage; | |
/** | |
* http://stackoverflow.com/questions/35675705/javafx-resize-rotated-object-to-gridpane-cell | |
* | |
* @author Mahen Vermeulen | |
* @author okr | |
* @date 01.03.2016 | |
* | |
*/ | |
public class MonopolyApp extends Application | |
{ | |
@Override | |
public void start( final Stage primaryStage ) | |
{ | |
//Construction of the grid | |
final GridPane square = new GridPane(); | |
square.setGridLinesVisible( true ); | |
final int heightPercent = 14; | |
final int widthPercent = 8; | |
final RowConstraints rowsEdge = new RowConstraints(); | |
rowsEdge.setPercentHeight( heightPercent ); | |
final RowConstraints rowsMid = new RowConstraints(); | |
rowsMid.setPercentHeight( widthPercent ); | |
final ColumnConstraints colEdge = new ColumnConstraints(); | |
colEdge.setHgrow( Priority.ALWAYS ); | |
final ColumnConstraints colMid = new ColumnConstraints(); | |
colMid.setPercentWidth( widthPercent ); | |
square.getColumnConstraints().addAll( colEdge, colMid, colMid, colMid, colMid, colMid, colMid, colMid, colMid, colMid, colEdge ); | |
square.getRowConstraints().addAll( rowsEdge, rowsMid, rowsMid, rowsMid, rowsMid, rowsMid, rowsMid, rowsMid, rowsMid, rowsMid, | |
rowsEdge ); | |
final Node wrongSuare = makeMonopolySquare( Layout.LEFT ); | |
square.add( wrongSuare, 0, 4 ); | |
// wrongSuare.setRotate( 90 ); | |
final Node correctSquare = makeMonopolySquare( Layout.RIGHT ); | |
square.add( correctSquare, 10, 7 ); | |
// correctSuare.getTransforms().add( new Rotate( 270 ) ); | |
final Node topSquare = makeMonopolySquare( Layout.TOP ); | |
square.add( topSquare, 5, 0 ); | |
final Node rightSquare = makeMonopolySquare( Layout.BOTTOM ); | |
square.add( rightSquare, 1, 10 ); | |
final Scene s = new Scene( square ); | |
primaryStage.setHeight( 500 ); | |
primaryStage.setWidth( 500 ); | |
primaryStage.setScene( s ); | |
primaryStage.show(); | |
} | |
/** | |
* @param args ignored. | |
*/ | |
public static void main( final String[] args ) | |
{ | |
Application.launch( args ); | |
} | |
private static enum Layout | |
{ | |
TOP, | |
LEFT, | |
BOTTOM, | |
RIGHT; | |
} | |
private static Node makeMonopolySquare( final Layout layout ) | |
{ | |
final GridPane monopolySquare = new GridPane(); | |
monopolySquare.setGridLinesVisible( true ); | |
final Label label = new Label( "name" ); | |
final StackPane name = new StackPane( label ); | |
final Pane colorPane = new Pane(); | |
GridPane.setMargin( colorPane, new Insets( 1 ) ); | |
colorPane.setMaxSize( Double.MAX_VALUE, Double.MAX_VALUE ); | |
colorPane.setBackground( new Background( new BackgroundFill( Color.BLUE, null, null ) ) ); | |
final RowConstraints top = new RowConstraints(); | |
final RowConstraints bottom = new RowConstraints(); | |
// bottom.setValignment( VPos.CENTER ); | |
// top.setValignment( VPos.CENTER ); | |
final ColumnConstraints c = new ColumnConstraints(); | |
c.setPercentWidth( 100 ); | |
final ColumnConstraints right = new ColumnConstraints(); | |
// right.setHalignment( HPos.CENTER ); | |
final ColumnConstraints left = new ColumnConstraints(); | |
// left.setHalignment( HPos.CENTER ); | |
final RowConstraints r = new RowConstraints(); | |
r.setPercentHeight( 100 ); | |
switch ( layout ) | |
{ | |
case BOTTOM: | |
top.setPercentHeight( 20 ); | |
bottom.setPercentHeight( 80 ); | |
monopolySquare.getRowConstraints().addAll( top, bottom ); | |
monopolySquare.getColumnConstraints().addAll( c ); | |
monopolySquare.add( colorPane, 0, 0 ); | |
monopolySquare.add( name, 0, 1 ); | |
break; | |
case LEFT: | |
right.setPercentWidth( 20 ); | |
left.setPercentWidth( 80 ); | |
monopolySquare.getColumnConstraints().addAll( left, right ); | |
monopolySquare.getRowConstraints().addAll( r ); | |
monopolySquare.add( name, 0, 0 ); | |
monopolySquare.add( colorPane, 1, 0 ); | |
break; | |
case RIGHT: | |
right.setPercentWidth( 80 ); | |
left.setPercentWidth( 20 ); | |
monopolySquare.getColumnConstraints().addAll( left, right ); | |
monopolySquare.getRowConstraints().addAll( r ); | |
monopolySquare.add( colorPane, 0, 0 ); | |
monopolySquare.add( name, 1, 0 ); | |
break; | |
case TOP: | |
top.setPercentHeight( 80 ); | |
bottom.setPercentHeight( 20 ); | |
monopolySquare.getRowConstraints().addAll( top, bottom ); | |
monopolySquare.getColumnConstraints().addAll( c ); | |
bottom.setValignment( VPos.CENTER ); | |
monopolySquare.add( colorPane, 0, 1 ); | |
monopolySquare.add( name, 0, 0 ); | |
break; | |
} | |
return monopolySquare; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment