1 package npw; 2 3 import painter.SPainter; 4 import shapes.SSquare; 5 6 import javax.swing.*; 7 import java.awt.*; 8 import java.util.Random; 9 import java.util.Scanner; 10 11 public class Stella { 12 13 public static void main(String [] args) { 14 //Reads the number of squares to be painted 15 int numOfSquares = getSquares("Number of squares to be painted"); 16 17 double squareSide = 700.0; 18 SPainter painter = new SPainter("Stella" , 800, 800); 19 SSquare square = new SSquare(squareSide); 20 21 //Paints the squares 22 paintSquares(painter, square, numOfSquares); 23 } 24 25 private static void paintSquares(SPainter painter, SSquare square, int numOfSquares) { 26 Color firstColor = randomColor1(); 27 Color secondColor = randomColor2(); 28 int numOfSquaresLeft = numOfSquares; 29 30 for (int i = 1; i <= numOfSquares; i = i + 1) { 31 //Checks if i is an even number by seeing the the remainder is = 0 32 if (i % 2 == 0) { 33 painter.setColor(firstColor); 34 } 35 else { 36 painter.setColor(secondColor); 37 } 38 painter.paint(square); 39 square.shrink(square.side() / numOfSquaresLeft ); 40 numOfSquaresLeft = numOfSquaresLeft - 1; 41 42 43 } 44 } 45 46 //Produces first random color 47 private static Color randomColor1() { 48 Random rand = new Random(); 49 int r = rand.nextInt(256); 50 int b = rand.nextInt(256); 51 int g = rand.nextInt(256); 52 return new Color(r,g,b); 53 } 54 55 //Produces the second random color 56 private static Color randomColor2() { 57 Random rand = new Random(); 58 int R = rand.nextInt(256); 59 int B = rand.nextInt(256); 60 int G = rand.nextInt(256); 61 return new Color(R,B,G); 62 63 } 64 //Gets the number of squares to be painted 65 private static int getSquares(String prompt) { 66 String num = JOptionPane.showInputDialog(null, prompt); 67 Scanner scanner = new Scanner(num); 68 return scanner.nextInt(); 69 } 70 } 71