Thursday, March 15, 2012

JavaFX 2.0: Rectangle and RectangleBuilder

Update: RectangleBuilder is deprecated and will be removed in the next version. Please read JavaFX 8 Rectangle example.


JavaFX 2.0: Rectangle and RectangleBuilder
package javafx_rect;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.RectangleBuilder;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_Rect extends Application {

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
      launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
      primaryStage.setTitle("java-buddy.blogspot.com");
    
      Rectangle rect1 = new Rectangle(10, 10, 280, 130);
      rect1.setFill(Color.BLUE);
      rect1.setStrokeWidth(3);
      rect1.setStroke(Color.BLACK);
    
      Rectangle rect2 = RectangleBuilder.create()
              .arcWidth(30)
              .arcHeight(30)
              .fill(Color.WHITESMOKE)
              .x(10)
              .y(160)
              .strokeWidth(3)
              .stroke(Color.BLACK)
              .build();
      rect2.setWidth(280);
      rect2.setHeight(130);
    
      Group root = new Group();
      root.getChildren().addAll(rect1, rect2);
      primaryStage.setScene(new Scene(root, 300, 300));
      primaryStage.show();
  }
}

5 comments:

  1. How could I create a rectangle without fill ? With stroke only. Thx.

    ReplyDelete
  2. Please note RectangleBuilder is deprecated and will be removed in the future version.

    ReplyDelete
  3. So, How could I create a rectangle with a transparent fill and a stroke not tranparent ?. Thx.

    ReplyDelete