Stella.java
1    /* 
2     * Write a program called Stella that paints images with the following constraints: 
3     * 1. The number of concentric squares will be read from a dialog box 
4     * 2. Two randomly chosen colors will be used for the image that is painted when the program is run 
5     * 3. Canvas is 800x800 and largest square is 700x700 
6     */
7    package npw;
8    
9    import painter.SPainter;
10   import shapes.SSquare;
11   import javax.swing.*;
12   import java.awt.*;
13   import java.util.Scanner;
14   
15   public class Stella {
16       private void paintTheImage() {
17           //Get the input info from the user
18           int nrOfSquares = getNumber("squares");
19           int bottomSqSideLength = 700;
20   
21           //Establish the painter
22           SPainter miro = new SPainter("Stella", 800, 800);
23           miro.setBrushWidth(4);
24           SSquare square = new SSquare(bottomSqSideLength);
25   
26           //Paint the squares
27           paintSquares(miro, nrOfSquares, square);
28       }
29   
30       private static int getNumber(String prompt) {
31           String nss = JOptionPane.showInputDialog(null, prompt+"?");
32           Scanner scanner = new Scanner(nss);
33           return scanner.nextInt();
34       }
35   
36       private static void paintSquares(SPainter miro, int nrOfSquares, SSquare square) {
37           //Paint the squares
38           int i = 1;
39           Color color1 = randomColor();
40           Color color2 = randomColor();
41           //paint bottom square outside of loop so its 700x700
42           miro.setColor(color1);
43   
44           while(i <= nrOfSquares) {
45               miro.paint(square);
46               square.shrink(700.0 / nrOfSquares); //shrink square side length depending on the number of squares
47               if (i % 2 == 0) { //If i is even, paint color1
48                   miro.setColor(color1);
49               } else { //If i is not even, paint color2
50                   miro.setColor(color2);
51               }
52               i++;
53           }
54       }
55   
56       private static Color randomColor() {
57           int rv = (int)(Math.random()*256);
58           int gv = (int)(Math.random()*256);
59           int bv = (int)(Math.random()*256);
60           return new Color(rv, gv, bv);
61       }
62   
63       public Stella() {
64           paintTheImage();
65       }
66   
67       public static void main(String[] args) {
68           SwingUtilities.invokeLater(new Runnable() {
69               @Override
70               public void run() {
71                   new Stella();
72               }
73           });
74       }
75   }
76