Stella.java
1    /* 
2     * A program to paint, centered on the canvas, a circle of randomly colored, black-framed squares. 
3     */
4    
5    package npw;
6    
7    import painter.SPainter;
8    import shapes.SSquare;
9    
10   import javax.swing.*;
11   import java.awt.*;
12   import java.util.Random;
13   import java.util.Scanner;
14   
15   public class Stella {
16       public static void main(String[] args) {
17           SwingUtilities.invokeLater(new Runnable() {
18               public void run() {
19                   new Stella();
20               }
21           });
22       }
23   
24       public Stella() {
25           paintTheImage();
26       }
27   
28       private void paintTheImage() {
29           int numberOfSquares = getNumber("Enter a number of concentric squares");
30           SPainter painter = new SPainter("Stella", 800, 800);
31           paintTheSquares(painter, numberOfSquares);
32       }
33   
34   
35       private static int getNumber(String prompt) {
36           String nss = JOptionPane.showInputDialog(null, prompt + "?");
37           Scanner scanner = new Scanner(nss);
38           return scanner.nextInt();
39       }
40   
41       private void paintTheSquares(SPainter painter, int numberOfSquares) {
42           int side = 700;
43           Color color1 = randomColor();
44           Color color2 = randomColor();
45           int i = 0;
46           while (i < numberOfSquares) {
47               painter.setColor(color1);
48               SSquare square1 = new SSquare(side);
49               painter.paint(square1);
50               side = side - (side / numberOfSquares);
51               painter.setColor(color2);
52               SSquare square2 = new SSquare(side);
53               painter.paint(square2);
54               side = side - (side / numberOfSquares);
55               i = i + 1;
56           }
57   
58       }
59   
60       private static Color randomColor() {
61           for (int i = 0; i < 2; i = i + 1) {
62               Random rgen = new Random();
63               int r = rgen.nextInt(256);
64               int g = rgen.nextInt(256);
65               int b = rgen.nextInt(256);
66               return new Color(r, g, b);
67           }
68           return null;
69       }
70   }
71   
72   
73