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 13 public static void main(String[] args) { 14 SwingUtilities.invokeLater(new Runnable() { 15 public void run() { 16 new Stella(); 17 } 18 }); 19 } 20 21 public Stella() { 22 paintTheImage(); 23 } 24 25 private void paintTheImage(){ 26 int width = 800; 27 int height = 800; 28 int nrOfSquares = input("how many squares?"); 29 30 SPainter painter = new SPainter("Stella", width, height); 31 SSquare dot = new SSquare(700); 32 33 paintStella(painter, dot, height, width, nrOfSquares); 34 } 35 36 private static int input(String prompt) { 37 String nss = JOptionPane.showInputDialog(null,prompt); 38 Scanner scanner = new Scanner(nss); 39 return scanner.nextInt(); 40 } 41 42 private void paintStella(SPainter painter, SSquare square, int height, int width, int nrOfSquares) { 43 Color c1 = randomColor(); 44 Color c2 = randomColor(); 45 painter.setColor(c2); 46 painter.paint(square); 47 int decrease = 700 / nrOfSquares; 48 49 for ( int i = 0; i <= nrOfSquares; i++ ) { 50 square.resetSide((int) square.side() - decrease); 51 if ( i % 2 == 0) { 52 painter.setColor(c1); 53 } 54 else { 55 painter.setColor(c2); 56 } 57 painter.paint(square); 58 } 59 } 60 61 private Color randomColor() { 62 Random r = new Random(); 63 int rgen = r.nextInt(10); 64 Random g = new Random(); 65 int ggen = g.nextInt(150); 66 Random b = new Random(); 67 int bgen = b.nextInt(200); 68 Color randomColor = new Color(rgen, ggen, bgen); 69 return randomColor; 70 } 71 }