1 package npw; 2 3 import painter.SPainter; 4 import shapes.SCircle; 5 import shapes.SSquare; 6 7 import javax.swing.*; 8 import java.util.Random; 9 10 import java.awt.*; 11 import java.util.Scanner; 12 13 public class Invention1 { 14 15 public static void main(String[] args) { 16 //Reads the number of squares to be painted 17 int numOfSquares = getSquares("Number of squares to be painted"); 18 19 int squareSideBackGround = 700; 20 int circleRadius = 200; 21 int squareS = 20; 22 23 SPainter painter = new SPainter("Pumpkin", 800, 800); 24 SCircle circle = new SCircle(circleRadius); 25 SSquare square = new SSquare(squareSideBackGround); 26 27 //Paints the background 28 paintSquares(painter, square, numOfSquares); 29 30 //Paints first circle 31 painter.setColor(Color.ORANGE); 32 painter.paint(circle); 33 34 //Paints the stem 35 paintStem(painter, circle, square, circleRadius); 36 37 //Paints the face of the pumpkin 38 paintFace(painter, circle, square, circleRadius, squareS); 39 40 41 } 42 43 private static void paintSquares(SPainter painter, SSquare square, int numOfSquares) { 44 Color firstColor = randomColor1(); 45 Color secondColor = randomColor2(); 46 int numOfSquaresLeft = numOfSquares; 47 48 for (int i = 1; i <= numOfSquares; i = i + 1) { 49 //Checks if i is an even number by seeing the the remainder is = 0 50 if (i % 2 == 0) { 51 painter.setColor(firstColor); 52 } 53 else { 54 painter.setColor(secondColor); 55 } 56 painter.paint(square); 57 square.shrink(square.side() / numOfSquaresLeft ); 58 numOfSquaresLeft = numOfSquaresLeft - 1; 59 60 61 } 62 } 63 private static Color randomColor2() { 64 Random rand = new Random(); 65 int R = rand.nextInt(256); 66 int B = rand.nextInt(256); 67 int G = rand.nextInt(256); 68 return new Color(R,B,G); 69 } 70 71 72 private static Color randomColor1() { 73 Random rand = new Random(); 74 int r = rand.nextInt(256); 75 int b = rand.nextInt(256); 76 int g = rand.nextInt(256); 77 return new Color(r,g,b); 78 } 79 80 private static int getSquares(String prompt) { 81 String num = JOptionPane.showInputDialog(null, prompt); 82 Scanner scanner = new Scanner(num); 83 return scanner.nextInt(); 84 } 85 86 87 88 89 private static void paintStem(SPainter painter, SCircle circle, SSquare square, int circleRadius) { 90 square.resetSide(70); 91 circle.setRadius(circleRadius); 92 painter.mfd(circle.radius() + square.side() / 2); 93 painter.setColor(Color.GREEN); 94 painter.paint(square); 95 96 //Makes painter invariant with position 97 painter.moveToCenter(); 98 } 99 100 private static void paintFace(SPainter painter, SCircle circle, SSquare square, int squareS, int circleRadius) { 101 //Paints first "eye" 102 square.resetSide(30); 103 painter.mfd(circle.radius() / 3); 104 painter.mlt(circle.radius() / 3); 105 painter.setColor(Color.blue); 106 painter.paint(square); 107 painter.moveToCenter(); 108 109 //Paints second "eye" 110 painter.mfd(circle.radius() / 3); 111 painter.mrt(circle.radius() / 3); 112 painter.paint(square); 113 painter.moveToCenter(); 114 115 //Paints the nose 116 painter.paint(square); 117 118 //Paints the mouth and one tooth 119 painter.mbk(circle.radius() / 2); 120 circle.setRadius(50); 121 painter.paint(circle); 122 123 painter.setColor(Color.WHITE); 124 square.resetSide(20); 125 painter.mfd(circle.radius() - square.side() / 2); 126 painter.paint(square); 127 128 //Makes the painter, the circle's radius and the square's side invariant to original pos, radius and side 129 painter.moveToCenter(); 130 circle.setRadius(circleRadius); 131 square.resetSide(squareS); 132 } 133 } 134 135 136