Stella.java
1    /* 
2    * This programs will have a input dialog box to input the number of squares to draw and paint with 2 random colors 
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           @Override
19           public void run() { new Stella();}
20       });
21   }
22   public Stella(){
23       paintTheImage();
24   
25   }
26   
27       private void paintTheImage() {
28       int width= 800;
29       int height= 800;
30       int nrOfSquares= getNumber("how many squares?");
31   
32           SPainter painter= new SPainter("Stella", width, height);
33           SSquare square= new SSquare(700);
34           paintStella(painter, square, nrOfSquares);
35   
36       }
37   
38       private void paintStella(SPainter painter, SSquare square, int nrOfSquares) {
39       // set up two random colors
40           Color color1= randomColor();
41           Color color2= randomColor();
42   
43          double truQuotient= (square.side()/nrOfSquares);
44          for (int i= 0; i<nrOfSquares; i=i+1){
45   
46           if (i%2==0) {
47   
48               painter.setColor(color1);
49               painter.paint(square);
50               square.shrink(truQuotient);
51           }
52           else{
53                 painter.setColor(color2);
54               painter.paint(square);
55               square.shrink(truQuotient);
56              }
57           }
58       }
59   
60       private static Color randomColor() {
61       Random rgen= new Random();
62       int r= rgen.nextInt(256);
63       int g= rgen.nextInt(256);
64       int b= rgen.nextInt(256);
65       return new Color(r,g,b);
66       }
67   
68       private static int getNumber(String prompt) {
69            String nss= JOptionPane.showInputDialog(null, prompt);
70           Scanner scanner= new Scanner(nss);
71           return scanner.nextInt();
72       }
73   
74   }
75