Question: This need to be done in java /* * Painting_Thing.java */ import java.awt.*; import java.util.*; /** * A graphics program that simulates Piet Mondrian paintings
This need to be done in java
/*
* Painting_Thing.java
*/
import java.awt.*;
import java.util.*;
/**
* A graphics program that simulates Piet Mondrian paintings
*
* @author Dr. Norman J. Bashias
*/
@SuppressWarnings("serial")
public class Painting_Thing extends javax.swing.JFrame
{
static final int MIN_WIDTH = 25;
static final int MIN_HEIGHT = 25;
/*
* Recursive Mondrian painting method
*
* *** COMPLETE THE METHOD CODE AS INDICATED ***s
*/
void paintMondrian(int left, int top, int width, int height, Graphics pen)
{
// if both width and height are each greater than its respective minimum (ADD THE IF CONDITION):
if () {
pen.setColor(getRandomColor());
pen.fillRect(left, top, width, height);
/*
* Use the Random generator to generate one of three random integers between 0 and 2,
* assigned to an integer variable (choice).
*
* *** ADD CODE HERE ***
*/
/*
* if the choice is 0, draw a horizontal line, and the areas above and below it:
* 1. set an integer variable (topHeight) to a call to generator's nextInt
* method, generating a random integer between 0 and the height
* 2. paint the area above the horizontal line at topHeight by
* making a recursive call to paintMondrian with:
* left, top, width, topHeight and pen
* 3. paint the area below the horizontal line at topHeight by
* making a recursive call to paintMondrian with:
* left, top+topHeight, width, height-topHeight and pen
* 4. draw the horizontal line at topHeight by calling
* the drawHorizontalLine method with:
* left, top+topHeight, width and pen
*
* otherwise, if the choice is 1, draw a vertical line, and the areas to its left and right:
* 1. set an integer variable (leftWidth) to a call to generator's nextInt
* method, generating a random integer between 0 and the width
* 2. paint the area to the left of the vertical line at leftWidth by
* making a recursive call to paintMondrian with:
* left, top, leftWidth, height and pen
* 3. paint the area to the right of the vertical line at leftWidth by
* making a recursive call to paintMondrian with:
* left+leftWidth, top, width-leftWidth, height and pen
* 4. draw the vertical line at topHeight by calling
* the drawVerticalLine method with:
* left+leftWidth, top, height and pen
*
* *** ADD THE MULTI-WAY BRANCHING CODE BELOW ***
*/
} // end outer if
// else base case: canvas is too small to continue
} // end paintMondrian
/**
* *********************************************************
* WARNING!!! DO NOT ADD OR MODIFY CODE BELOW THIS LINE!!! *
* *********************************************************
*/
public void paint(Graphics g)
{
// draw the window title, menu, buttons
super.paint(g);
// get the Graphics context for the canvas
Graphics pen = canvas.getGraphics();
// set the top-left corner (X,Y) coordinates and
// the width and height of the canvas
xCorner = canvas.getX();
yCorner = canvas.getY();
width = canvas.getWidth();
height = canvas.getHeight();
// display the Mondrian painting
paintMondrian(xCorner, yCorner, width, height, pen);
} // end paint
// Returns either Color.WHITE or a color chosen at random from
// the array COLORS. WHITE is returned about 50% of the time.
private Color getRandomColor()
{
Color randomColor;
if (generator.nextInt(2) == 0) {
int randomIndex = generator.nextInt(COLORS.length);
// randomIndex >= 0 and
The above image is a print of Mondrian's Composition with Red Blue Yellow. Mimicking his style can involve random selections of color and randomly placed black lines of of random thicknesses. Of course, we must set some constraints to avoid a totally ugly painting! To-paint a Mondrian," we can fill the entire area with a random color selected from among red, yellow, and white. We then divide the area into two pieces by drawing a randomly placed horizontal or vertical line. The next step is to recursively paint a Mondrian in each of these two pieces. If a piece is smaller than a certain size, we'll do nothing (this is the simple case). Describing our strategy in Java pseudocode gives us the following recursive algorithm: Algorithm paintMondrian(a rectangular area) if (the area exceeds a given minimum) Choose a color at random from a selection of colors Paint the area in the chosen color At random, draw either a horizontal or vertical line at a random position across the entire area if (a horizontal line was drawn) paintMondrian(the upper portion of the area) paintMondrian (the lower portion of the area) else a vertical line was drawn paintMondrian(the left portion of the area) paintMondrian(the right portion of the area) randomColor = COLORS[randomIndex];
} else
randomColor = Color.WHITE;
return randomColor;
} // end getRandomColor
// Draws a horizontal black line at a given position with a given
// length and random thickness.
private void drawHorizontalLine(int left, int top, int length, Graphics pen)
{
int thickness = generator.nextInt(4) + 1; // 1, 2, 3, or 4 pixels
pen.setColor(Color.BLACK);
pen.fillRect(left, top, length, thickness);
} // end drawHorizontalLine
// Draws a vertical black line at a given position with a given
// length and random thickness.
private void drawVerticalLine(int left, int top, int length, Graphics pen)
{
int thickness = generator.nextInt(4) + 1; // 1, 2, 3, or 4 pixels
pen.setColor(Color.BLACK);
pen.fillRect(left, top, thickness, length);
} // end drawVerticalLine
/**
* Creates new form NB_Painting_Thing2
*/
public Painting_Thing()
{
initComponents();
generator = new Random();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the NB_Painting_Thing2 Editor.
*/
// //GEN-BEGIN:initComponents
private void initComponents()
{
canvas = new javax.swing.JPanel();
resetBtn = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("My Mondrian Painter");
canvas.setBackground(new java.awt.Color(255, 255, 255));
canvas.setLayout(new java.awt.BorderLayout());
getContentPane().add(canvas, java.awt.BorderLayout.CENTER);
resetBtn.setText("Reset");
resetBtn.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
resetBtnActionPerformed(evt);
}
});
getContentPane().add(resetBtn, java.awt.BorderLayout.PAGE_END);
setSize(new java.awt.Dimension(816, 639));
setLocationRelativeTo(null);
}// //GEN-END:initComponents
private void resetBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetBtnActionPerformed
repaint();
}//GEN-LAST:event_resetBtnActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[])
{
/* Set the Nimbus look and feel */
//
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try
{
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
{
if ("Nimbus".equals(info.getName()))
{
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex)
{
java.util.logging.Logger.getLogger(Painting_Thing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex)
{
java.util.logging.Logger.getLogger(Painting_Thing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex)
{
java.util.logging.Logger.getLogger(Painting_Thing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex)
{
java.util.logging.Logger.getLogger(Painting_Thing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new Painting_Thing().setVisible(true);
}
});
} // end main method
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel canvas;
private javax.swing.JButton resetBtn;
private int xCorner;
private int yCorner;
private int width;
private int height;
private Random generator;
private static final Color[] COLORS = {Color.RED, Color.BLUE, Color.YELLOW};
// End of variables declaration//GEN-END:variables
} // end Painting_Thing class
Painting_Thing.java
Displaying Painting_Thing.java.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
