1 package npw; 2 3 import java.awt.Color; 4 import java.util.Random; 5 import java.util.Scanner; 6 import javax.swing.JOptionPane; 7 import javax.swing.SwingUtilities; 8 9 import painter.SPainter; 10 import shapes.SSquare; 11 12 public class Stella { 13 14 private void paintTheImage(){ 15 // General Variables 16 int canvasDimension = 800; 17 int side = 700; 18 19 // Gets Input From User 20 int numSquares = getNumber("Number of squares"); 21 22 // Object Initialization 23 SSquare square = new SSquare(side); 24 SPainter klee = new SPainter("Stella", canvasDimension, canvasDimension); 25 double descalingNum = square.side() / numSquares; 26 27 paintSquares(klee, numSquares, square, descalingNum); 28 29 30 } 31 32 33 34 private static void paintSquares(SPainter klee, int numSquares, SSquare square, double descalingNum){ 35 36 // Chooses Two Randomized Colors 37 Color colorOne = randomColor(); 38 Color colorTwo = randomColor(); 39 40 // Alternates the Use of Colors 41 for(int i = 0; i < numSquares; i++){ 42 if(i % 2 == 0){ 43 klee.setColor(colorOne); 44 }else{ 45 klee.setColor(colorTwo); 46 } 47 48 // Paints the Square and Shrinks the Next Square 49 klee.paint(square); 50 square.resetSide((int)(square.side() - descalingNum)); 51 } 52 } 53 54 // Prompts and Read Input From User 55 private static int getNumber(String prompt) { 56 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 57 Scanner scanner = new Scanner(nss); 58 return scanner.nextInt(); 59 } 60 61 // Returns Random Color 62 private static Color randomColor() { 63 Random random = new Random(); 64 int r = random.nextInt(256); 65 int g = random.nextInt(256); 66 int b = random.nextInt(256); 67 return new Color(r,b,g); 68 } 69 70 // REQUIRED INFRASTRUCTURE // 71 public Stella(){ 72 paintTheImage(); 73 } 74 75 public static void main(String[] args){ 76 SwingUtilities.invokeLater(new Runnable() { 77 @Override 78 public void run() { 79 new Stella(); 80 } 81 }); 82 } 83 } 84