1 package assign_4; 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 @Override 16 public void run() { 17 new assign_4.Stella(); 18 } 19 }); 20 } 21 22 private Stella() { 23 paintTheImage(); 24 } 25 26 private void paintTheImage() { 27 int numOfSquares = getNumber(); 28 SPainter painter = new SPainter("Stella", 800, 800); 29 SSquare square = new SSquare(700); 30 squares(square, painter, numOfSquares); 31 } 32 private void squares(SSquare square, SPainter painter, int numOfSquares) { 33 Random random = new Random(); 34 final float hue = random.nextFloat(); 35 final float saturation = (random.nextInt(2000) + 1000) / 10000f; 36 final float luminance = 0.9f; 37 final Color color2 = Color.getHSBColor(hue, saturation, luminance); 38 final float hue1 = random.nextFloat(); 39 final float saturation1 = (random.nextInt(2000) + 1000) / 10000f; 40 final float luminance1 = 0.9f; 41 final Color color1 = Color.getHSBColor(hue1, saturation1, luminance1); 42 double num = square.side()/numOfSquares; 43 int j = numOfSquares; 44 int i = 0; 45 while (j >= 0) { 46 if (i == 0) { 47 painter.setColor(color1); 48 painter.paint(square); 49 square.shrink(num); 50 } else { 51 painter.setColor(color2); 52 painter.paint(square); 53 square.shrink(num); 54 } 55 j = j - 1; 56 i = i + 1; 57 if (i >= 2) { 58 i = 0; 59 } 60 } 61 62 } 63 64 private int getNumber() { 65 String nss = JOptionPane.showInputDialog(null, "number of concentric squares" + "?"); 66 Scanner scanner = new Scanner(nss); 67 return scanner.nextInt(); 68 } 69 70 } 71 72