package npw; import painter.SPainter; import shapes.SRectangle; import javax.swing.*; import java.awt.*; import java.util.Random; public class Invention2 { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new Invention2(); } }); } public Invention2() { paintTheImage(); } private void paintTheImage() { int canvasSide = 800; SPainter painter = new SPainter("Invention2", canvasSide, canvasSide); int squareWidth = (canvasSide); int numOfSquares = randomNumber(); int i = 0; double sizeChange = ((canvasSide) / numOfSquares); SRectangle square = new SRectangle(squareWidth, squareWidth); Color Color1 = randomColor(); Color Color2 = randomColor(); Color Color3 = randomColor(); paintSquares(i, numOfSquares, painter, square, sizeChange, Color1, Color2, Color3); } private void paintSquares(int i, int nrOfSquares, SPainter painter, SRectangle square, double sizeChange, Color randColor1, Color randColor2, Color color3) { while (i < nrOfSquares) { //Alternate between two randomly colored squares, and shrink after each iteration. if (i % 2 != 0) { painter.setColor(randColor1); painter.paint(square); square.shrink(sizeChange, sizeChange / 2); } else { painter.setColor(randColor2); painter.paint(square); square.shrink(sizeChange, sizeChange / 2); } i = i + 1; } } private Color randomColor() { Random randColor = new Random(); int A = randColor.nextInt(250); int B = randColor.nextInt(250); int C = randColor.nextInt(250); return new Color(A, B, C); } private static int randomNumber() { Random random = new Random(); int Num1 = random.nextInt(100); if (Num1 == 0) { return (Num1 + 1); } else { return Num1; } } }