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.Random; 9 import java.util.Scanner; 10 11 public class Stella { 12 public static void main(String[] args) { 13 SwingUtilities.invokeLater(new Runnable() { 14 @Override 15 public void run() { 16 new Stella(); 17 } 18 }); 19 } 20 public Stella(){ 21 paintTheImage(); 22 } 23 private void paintTheImage() { 24 SPainter klee = new SPainter("Stella", 800, 800); 25 int sideLength = 700; 26 SSquare square = new SSquare(sideLength); 27 int nrOfSquares = getNumber(); 28 double shrink = (square.side()/nrOfSquares); 29 Color c1 = randomColor(); 30 Color c2 = randomColor(); 31 paintSquares(klee, square, nrOfSquares, shrink, c1, c2); 32 33 } 34 35 private void paintSquares(SPainter klee, SSquare square, int nrOfSquares, double shrink, Color c1, Color c2) { 36 int i = 0; 37 while (i<=nrOfSquares){ 38 if (i % 2 == 0) { 39 klee.setColor(c2); 40 } else { 41 klee.setColor(c1); 42 } 43 klee.paint(square); 44 square.shrink(shrink); 45 i++; 46 } 47 } 48 49 private static int getNumber() { 50 String nss = JOptionPane.showInputDialog(null, "The number of concentric squares: " + "?"); 51 Scanner scanner = new Scanner(nss); 52 return scanner.nextInt(); 53 } 54 private static Color randomColor() { 55 Random rgen = new Random(); 56 int r = rgen.nextInt(256); 57 int g = rgen.nextInt(256); 58 int b = rgen.nextInt(256); 59 return new Color(r, g, b); 60 } 61 } 62