Invention1.java
1    /* 
2    It uses at least one while statement in a nontrivial way. 
3    It uses at least one if statement in a nontrivial way. 
4    It features both circles and squares, all created from just one circle and just one square. No rectangles. 
5    It creates the exact same image every time it is run. 
6    There is some chance that the casual observer might find the image interesting! 
7     */
8    package NPW;
9    
10   import java.awt.*;
11   import java.util.Scanner;
12   import javax.swing.*;
13   
14   import painter.SPainter;
15   import shapes.SCircle;
16   import shapes.SSquare;
17   
18   public class Invention1 {
19   
20       private void paintTheImage() {
21           //Collecting data from the user
22   
23           int sizeOfSquare = getNumber("square side length");
24           int radiusOfCircle = getNumber("circle radius");
25           int nrOfSquares = getNumber("number of squares");
26           int nrOfCircles = getNumber("number of circles");
27           String color = getColor("color of shapes: Orange, Green, Pink");
28   
29           //Establish painter
30           int nrOfShapes = nrOfCircles + nrOfSquares;
31           SPainter painter = new SPainter("Invention1",500, 500);
32           painter.setBrushWidth(4);
33           SCircle circle = new SCircle(radiusOfCircle);
34           SSquare square = new SSquare(sizeOfSquare);
35   
36           //run method to create the image
37           paintCanvas(painter,circle,square,nrOfCircles,nrOfSquares,nrOfShapes,color);
38   
39       }
40   
41       private static int getNumber(String prompt) {
42           String nss = JOptionPane.showInputDialog(null,prompt+"?");
43           Scanner scanner = new Scanner(nss);
44           return scanner.nextInt();
45       }
46   
47       private static String getColor(String prompt) {
48           String nss = JOptionPane.showInputDialog(null,prompt+"?");
49           Scanner scanner = new Scanner(nss);
50           return scanner.nextLine();
51       }
52   
53       private static void paintCanvas(SPainter painter, SCircle circle, SSquare square,
54                                       int nrOfCircles, int nrOfSquares, int nrOfShapes, String color) {
55           int i = 0;
56           while (i <= nrOfShapes) {
57               //Run all of the methods together and paint squares and circles
58               paintCircles(painter,circle,square,nrOfCircles,color);
59               painter.paint(circle);
60               paintSquares(painter,circle,square,nrOfSquares,color);
61               painter.paint(square);
62               i = i + 1;
63           }
64   
65       }
66   
67       private static void paintCircles(SPainter painter, SCircle circle,SSquare square,
68                                      int nrOfCircles, String color) {
69           //paint circles
70           int i = 0;
71           while (i <= nrOfCircles) {
72               paintShapes(painter,circle,square,color);
73               painter.move();
74               i = i + 1;
75   
76   
77           }
78   
79       }
80   
81       private static void paintSquares(SPainter painter, SCircle circle, SSquare square,
82                                       int nrOfSquares, String color) {
83           //paint squares
84           int i = 0;
85           while (i <= nrOfSquares) {
86               paintShapes(painter,circle,square,color);
87               //move to a random place on the canvas
88               painter.move();
89               i = i + 1;
90   
91           }
92       }
93   
94       private static void paintShapes(SPainter painter, SCircle circle, SSquare square,
95                                       String color) {
96           //Selects color from the input
97           if (color.equalsIgnoreCase("Orange")) {
98               painter.setColor(Color.ORANGE);
99   
100          } else if (color.equalsIgnoreCase("Green")) {
101              painter.setColor(Color.green);
102  
103          } else if (color.equalsIgnoreCase("Pink")) {
104              painter.setColor(Color.pink);
105  
106          } else {
107              painter.setColor(Color.blue);
108  
109          }
110      }
111      public Invention1() {paintTheImage();}
112  
113      public static void main(String[] args){
114          SwingUtilities.invokeLater(new Runnable() {
115              @Override
116              public void run() {
117                  new Invention1();
118              }
119          });
120      }
121  
122  
123  }
124