1 /* 2 *Program that will be used to create alternating squares of two random colors 3 * The number of square will be read from a dialogue box 4 */ 5 6 package npw; 7 8 import npw.templates.SquareTemplate; 9 import painter.SPainter; 10 import shapes.SSquare; 11 12 import javax.swing.*; 13 import javax.swing.text.AttributeSet; 14 import java.awt.*; 15 import java.util.Random; 16 import java.util.Scanner; 17 18 public class Stella { 19 20 private void paintTheImage() { 21 //lets get us some information from the user 22 int nrOfSquares = getNrOfSquares(); 23 //create a painter 24 SPainter stella = new SPainter("Stella",800,800); 25 SSquare square = new SSquare(700); 26 paintTheSquares(stella,nrOfSquares,square); 27 } 28 29 private int getNrOfSquares() { 30 String nss = JOptionPane.showInputDialog(null, "Please enter number of squares"); 31 Scanner scanner = new Scanner(nss); 32 return scanner.nextInt(); 33 } 34 private void paintTheSquares(SPainter stella, int nrOfSquares, SSquare square) { 35 //idk man some magic numbers 36 double shrinkValue = (700/((double)nrOfSquares+1)); 37 //establish color 38 Color colorUno = new Color(rValue(),gValue(),bValue()); 39 Color colorDos = new Color(gValue(),bValue(),rValue()); 40 41 stella.setColor(colorUno); 42 int i = 1; 43 while (i <= nrOfSquares) { 44 if (stella.paintBrushColor() == colorUno) { 45 stella.setColor(colorDos); 46 } else { 47 stella.setColor(colorUno); 48 } 49 square.shrink(shrinkValue); 50 stella.paint(square); 51 i = i+1; 52 } 53 54 } 55 56 private int bValue() { 57 Random rgen = new Random(); 58 int b = rgen.nextInt(256); 59 return b; 60 } 61 62 private int gValue() { 63 Random rgen = new Random(); 64 int g = rgen.nextInt(256); 65 return g; 66 } 67 68 private int rValue() { 69 Random rgen = new Random(); 70 int r = rgen.nextInt(256); 71 return r; 72 } 73 74 public Stella() { 75 paintTheImage(); 76 } 77 78 public static void main(String[] args) { 79 SwingUtilities.invokeLater(new Runnable() { 80 public void run() { 81 new Stella(); 82 } 83 }); 84 } 85 } 86