Stella.java
1    /* 
2     *Program that will be used to create alternating squares of two random colors 
3     * The number of square will be read from a dialogue box 
4     */
5    
6    package npw;
7    
8    
9    import painter.SPainter;
10   import shapes.SSquare;
11   import javax.swing.*;
12   import javax.swing.text.AttributeSet;
13   import java.awt.*;
14   import java.util.Random;
15   import java.util.Scanner;
16   
17   public class Stella {
18       private void paintTheImage() {
19           //lets get us some information from the user
20           int nrOfSquares = getNrOfSquares();
21           //create a painter
22           SPainter stella = new SPainter("Stella",800,800);
23           SSquare square = new SSquare(700);
24           paintTheSquares(stella,nrOfSquares,square);
25       }
26   
27       private int getNrOfSquares() {
28           String nss = JOptionPane.showInputDialog(null, "Number or Squares?");
29           Scanner scanner = new Scanner(nss);
30           return scanner.nextInt();
31       }
32   
33       private void paintTheSquares(SPainter stella, int nrOfSquares, SSquare square) {
34           double shrinkValue = (700/((double)nrOfSquares+1));
35           //establish color
36           Color firstColor = new Color(rv(),gv(),bv());
37           Color secondColor = new Color(gv(),bv(),rv());
38           stella.setColor(firstColor);
39           int i = 1;
40   
41           while (i <= nrOfSquares) {
42               if (stella.paintBrushColor() == firstColor) {
43                   stella.setColor(secondColor);
44               } else {
45                   stella.setColor(firstColor);
46               }
47               square.shrink(shrinkValue);
48               stella.paint(square);
49               i = i+1;
50           }
51       }
52   
53       private int bv() {
54           Random rgen = new Random();
55           int b = rgen.nextInt(256);
56           return b;
57       }
58   
59       private int gv() {
60           Random rgen = new Random();
61           int g = rgen.nextInt(256);
62           return g;
63       }
64   
65       private int rv() {
66           Random rgen = new Random();
67           int r = rgen.nextInt(256);
68           return r;
69       }
70   
71       public Stella() {
72           paintTheImage();
73       }
74   
75       public static void main(String[] args) {
76   
77           SwingUtilities.invokeLater(new Runnable() {
78               public void run() {
79                   new Stella();
80               }
81           });
82       }
83   
84   }
85