Stella.java
1    /* 
2    *squares that read from a dialog 
3    * 2 random colors 
4    * canvas size 800 by 800 
5    * largest square 700 by 700 
6     */
7    
8    package npw;
9    
10   import painter.SPainter;
11   import shapes.SSquare;
12   import java.util.Scanner;
13   import javax.swing.*;
14   import java.awt.*;
15   import java.util.Random;
16   
17   public class Stella {
18       private void paintTheImage() {
19           int canvasSize = 800;
20           int numberOfStellas = getNumber("Number Of Stellas");
21           int squareSide = (canvasSize-100);
22           int i = 0;
23           double sizeChange = (squareSide/ numberOfStellas);
24           Color color1 = randomColor();
25           Color color2 = randomColor();
26           SSquare stella = new SSquare(squareSide);
27           SPainter painter = new SPainter("Stella", squareSide, squareSide);
28           paintStella(i, numberOfStellas, color1, color2, stella, painter, sizeChange);
29       }
30   
31       //Creates the random color needed for both color1 and color2
32       private Color randomColor() {
33           Random rgen = new Random();
34           int r = rgen.nextInt(256);
35           int g = rgen.nextInt(256);
36           int b = rgen.nextInt(256);
37           return new Color(r, g, b);
38       }
39   
40       private void paintStella(int i, int numberOfStellas, Color color1, Color color2, SSquare stella, SPainter painter, double sizeChange) {
41           while (i < numberOfStellas) {
42               //Alternate between two random colors
43               if (i % 2 != 0) {
44                   painter.setColor(color1);
45                   painter.paint(stella);
46                   stella.shrink(sizeChange);
47               } else {
48                   painter.setColor(color2);
49                   painter.paint(stella);
50                   stella.shrink(sizeChange);
51               }
52               //will continue until the if statement is satisfied, in other words until the set number of stellas have been displayed
53               i = i + 1;
54           }
55       }
56   
57           private int getNumber(String squareSide) {
58           String nss = JOptionPane.showInputDialog(null,squareSide+1);
59           Scanner scanner = new Scanner(nss);
60           return scanner.nextInt();
61       }
62   
63       //INFRASTRUCTURE
64       public Stella() {
65           paintTheImage();
66       }
67   
68       public static void main(String[] args) {
69           SwingUtilities.invokeLater(new Runnable() {
70               @Override
71               public void run() {
72                   new Stella();
73               }
74           });
75       }
76   }
77