Question: Lab 13: Recursive Mondrian Art This lab combines graphics with recursion by constructing a simple subdivision fractal that resembles the famous works of abstract art
Lab 13: Recursive Mondrian Art
This lab combines graphics with recursion by constructing a simple subdivision fractal that resembles the famous works of abstract art by Piet Mondrian. An example run of the instructor's model solution produced the random image captured for eternity in the following screenshot, to which your images should be reasonably similar in style and spirit, using controlled randomness to generate a new piece of abstract visual art every time your program is run

Write a class Mondrian that extends JPanel, with the following fields:
// Cutoff size for when a rectangle is not subdivided further.
private static final int CUTOFF = 40;
// Percentage of rectangles that are white.
private static final double WHITE = 0.75;
// Colours of non-white rectangles.
private static final Color[] COLORS = { Color.YELLOW, Color.RED, Color.BLUE, Color.CYAN };
// RNG instance to make the random decisions with.
private Random rng = new Random();
// The Image in which the art is drawn. private Image mondrian;
After this, the class should have the following methods:
public Mondrian(int w, int h)
The constructor for the class, with the desired width and the height of the resulting artwork given as constructor arguments.
Initialize the field mondrian to a new BufferedImage of size w and h, and ask that image for its Graphics2D object to be passed as the last argument of the top-level call of the recursive subdivide method below
public void paintComponent(Graphics g)
Draws the mondrian image created in the constructor on the surface of this component.
private void subdivide(int tx, int ty, int w, int h, Graphics2D g2)
Recursively subdivides the given rectangle whose top left corner is in coordinates (tx, ty) and whose width and height are (w, h). The base case of the recursion is when either w or h is less than CUTOFF, in which case this rectangle is drawn on the Graphics2D object provided. (The toplevel call in the constructor should ask the image object its Graphics2D object and pass that on to the recursive call.) This rectangle should be white with probability WHITE, and choose a random colour from COLORS otherwise.
Otherwise, this recursive subdivision method should make two recursive calls for the two rectangles that you split randomly from the given rectangle. Make sure that you always split each rectangle along its longer edge, and that the splitting line is not too close to either edge that has the same direction, to keep the subdivision reasonably balanced. Subdivisions that contain thin shards and forced sharp angles tend to be perceived as ugly and disharmonious, same as when somebody writes cursive by hand and has to squeeze the words in more narrowly at the end of the line to stay within the margins. In all walks of life, that elusive sweet spot of perfect harmony between the extremes of predictably solid and boring order opposite the screeching cacophony of randomness will usually be perceived as the most aesthetic by actual humans
To admire your randomly generated pieces of art, write a main method that creates a JFrame instance that contains one instance of Mondrian that is 1,000 pixels wide and 800 pixels tall. You can also try varying the above parameters and the colour scheme to aim for an even more artistic and aesthetically pleasing result
This random subdivision is not yet aesthetically the best possible due to a massive bias in the positioning of the subdivision walls inside the rectangle. Each subdivision will always contain exactly one straight fault line directly through that entire rectangle, which then continues to recursively hold for the two rectangles separated by this line. This is unconsciously perceived as being unnatural, even if the viewer does not consciously perceive the existence of these lines. (The reader might enjoy finding these recursive fault lines from the previous example image, and deduce the order of the subdivision recursion from those lines.) A better subdivision rule would break each rectangle into more than two smaller rectangles to eliminate the fault line through that entire rectangle, generally making the resulting subdivision structure seem more natural and pleasant
Mondrian HE 11 IT
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
