Stella.java
1    package npw;
2    
3    import painter.SPainter;
4    import shapes.SSquare;
5    
6    import javax.swing.*;
7    import java.awt.*;
8    import java.util.Scanner;
9    
10   public class Stella {
11   
12       public static void main(String[] args) {
13           SwingUtilities.invokeLater(new Runnable() {
14               @Override
15               public void run() { new Stella(); }
16           });
17       }
18   
19       public Stella() {paintTheImage();}
20   
21       private void paintTheImage() {
22           int nrOfSquares = getNumber("Enter the number of squares");
23   
24           SPainter painter = new SPainter("Stella" , 800, 800);
25           Color firstColor = randomColor();
26           Color secondColor = randomColor();
27   
28           SSquare square = new SSquare(700);
29           paintOneSquare(painter, square, nrOfSquares, firstColor, secondColor);
30   
31       }
32   
33       private int getNumber(String prompt) {
34           String nss = JOptionPane.showInputDialog(null, prompt + "?");
35           Scanner sc = new Scanner(nss);
36           return sc.nextInt();
37       }
38   
39       private void paintOneSquare(SPainter painter, SSquare square, int nrOfSquares, Color firstColor, Color secondColor) {
40           int i = 0;
41           while ( i < nrOfSquares) {
42               if ( i % 2 == 0 ) {
43                   painter.setColor(firstColor);
44                   painter.paint(square);
45               } else {
46                   painter.setColor(secondColor);
47                   painter.paint(square);
48               }
49               square.shrink( 700 / nrOfSquares );
50               i = i + 1;
51           }
52       }
53   
54       private static Color randomColor() {
55           int rv = (int) (Math.random()*256);
56           int gv = (int) (Math.random()*256);
57           int bv = (int) (Math.random()*256);
58           return new Color( rv,gv,bv );
59       }
60   
61   }
62