In: Computer Science
The painter Mondrian is known for paintings that are made up of rectangles of different colors arranged together in patterns like the one below. Write an application that displays a Mondrian-like picture in a window using JavaFX (you are not allowed to use panels or frames). There must be at least nine rectangles, and they must be aligned so that they fill the window without any gaps. There must be at least two different colors, one of them you have to create (using the Color class) and at least two different sizes of rectangles. Use Mondrian as the class name
Program
package mondrian;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
/**
*
* @author
*/
public class Mondrian extends Application {
@Override
public void start(Stage stage) {
//Drawing a Rectangle
Rectangle r1 = new Rectangle(20,20, 520, 220);
r1.setStroke(Color.BLACK);
r1.setFill(Color.WHITE);
Rectangle r2 = new Rectangle(20, 20, 80, 85);
r2.setStroke(Color.BLUE);
r2.setFill(Color.WHITE);
Rectangle r3 = new Rectangle(20, 85, 80, 155);
r3.setStroke(Color.GREEN);
r3.setFill(Color.WHITE);
Rectangle r4 = new Rectangle(100, 20, 140, 150);
r4.setStroke(Color.ORANGE);
r4.setFill(Color.WHITE);
Rectangle r5 = new Rectangle(240, 20, 20, 150);
r5.setStroke(Color.RED);
r5.setFill(Color.WHITE);
Rectangle r6 = new Rectangle(260, 20, 280, 80);
r6.setStroke(Color.VIOLET);
r6.setFill(Color.WHITE);
Rectangle r7 = new Rectangle(260, 100, 140, 70);
r7.setStroke(Color.BROWN);
r7.setFill(Color.WHITE);
Rectangle r8 = new Rectangle(400, 100, 140, 70);
r8.setStroke(Color.CYAN);
r8.setFill(Color.WHITE);
Rectangle r9 = new Rectangle(100, 170, 240, 70);
r9.setStroke(Color.YELLOW);
r9.setFill(Color.WHITE);
//Creating a Group object
Group root = new Group();
root.getChildren().add(r1);
root.getChildren().add(r2);
root.getChildren().add(r3);
root.getChildren().add(r4);
root.getChildren().add(r5);
root.getChildren().add(r6);
root.getChildren().add(r7);
root.getChildren().add(r8);
root.getChildren().add(r9);
//Creating a scene object
Scene scene = new Scene(root, 650, 300);
//Setting title to the Stage
stage.setTitle("Mondrian");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Output: