1 /* 2 * Assignment 4 Problem 6: Deterministic Invention 3 * It uses at least one while statement in a nontrivial way. 4 * It uses at least one if statement in a nontrivial way. 5 * It features both circles and squares, all created from just one SCircle and just one SSquare. No rectangles. 6 * It creates the exact same image every time it is run. 7 * There is some chance that the casual observer might find the image interesting! 8 */ 9 10 package npw; 11 12 import painter.SPainter; 13 import shapes.SCircle; 14 import shapes.SSquare; 15 import javax.swing.*; 16 import java.awt.*; 17 import java.awt.geom.Point2D; 18 19 public class Invention1 { 20 21 // THE SOLUTION TO THE BLUE DOT PROBLEM 22 23 private void paintTheImage() { 24 String input = JOptionPane.showInputDialog(null, "Enter time in 24 hr format(ex: 1:24)..."); 25 // position of colon is important to know the length of each number 26 int division = input.indexOf(":"); 27 String firstHr = input.substring(0, 1); 28 String secondHr = input.substring(division - 1, division); 29 String firstMin = input.substring(division + 1, division + 2); 30 String secondMin = input.substring(division + 2, division + 3); 31 String colon = input.substring(division,division+1); 32 System.out.println(firstHr + "+" + secondHr + "+" + colon + "+" + firstMin + "+" + secondMin); 33 // paint each number in different canvas so that the paintNumber method can be used 34 // again and again without changing the position of each dot every time. 35 paintFirstHr(firstHr,input); 36 paintSecondHr(secondHr); 37 paintColon(colon); 38 paintFirstMin(firstMin); 39 paintSecondMin(secondMin); 40 } 41 // String x is needed because the paintNumber method use x parameter so that the method 42 // isn't needed to copy and paste in every method. 43 private void paintColon(String colon) { 44 SPainter painter = new SPainter("Colon",100,200); 45 painter.setScreenLocation(530,100); 46 String x = colon; 47 paintNumber(painter,x); 48 } 49 50 private void paintFirstHr(String firstHr,String input) { 51 // if the input length is 4, it means that the hour is single digit. 52 // Not to repeat the hour twice, make sure the first canvas is blank if the hour is single digit. 53 if (input.length()==4) { 54 SPainter painter = new SPainter("Hour", 230, 380); 55 painter.setScreenLocation(0,0); 56 } else { 57 SPainter painter = new SPainter("Hour", 230, 380); 58 painter.setScreenLocation(0,0); 59 String x = firstHr; 60 paintNumber(painter,x); 61 } 62 } 63 64 // other painting number can used same methods except new canvas and different screen location. 65 private void paintSecondHr(String secondHr) { 66 SPainter painter = new SPainter("Hour", 230, 380); 67 painter.setScreenLocation(300,0); 68 String x = secondHr; 69 paintNumber(painter,x); 70 } 71 72 private void paintFirstMin(String firstMin) { 73 SPainter painter = new SPainter("Minute", 230, 380); 74 painter.setScreenLocation(630,0); 75 String x = firstMin; 76 paintNumber(painter,x); 77 } 78 79 private void paintSecondMin(String secondMin) { 80 SPainter painter = new SPainter("Minute", 230, 380); 81 painter.setScreenLocation(860,0); 82 String x = secondMin; 83 paintNumber(painter,x); 84 } 85 86 87 // create method of rows and columns so that the length of program can be limited. 88 private void paintNumber(SPainter painter, String x) { 89 // here are just one square and one circle I used for the program 90 SSquare square = new SSquare(50); 91 SCircle circle = square.inscribingCircle(); 92 // painter need to wash the previous canvas so that the painting wont be repeated. 93 // if loop here!!! 94 if (x.equals("0")) { 95 painter.wash(); 96 paintFirstColumn(painter,circle,square); 97 paintThirdRow(painter,circle,square); 98 paintFirstRow(painter,circle,square); 99 paintSecondColumn(painter,circle,square); 100 } else if (x.equals("1")) { 101 painter.wash(); 102 paintSecondColumn(painter,circle,square); 103 } else if (x.equals("2")) { 104 painter.wash(); 105 paintFirstRow(painter,circle,square); 106 paintHalfTopSecondColumn(painter,circle,square); 107 paintSecondRow(painter,circle,square); 108 paintHalfBottomFirstColumn(painter,circle,square); 109 paintThirdRow(painter,circle,square); 110 } else if (x.equals("3")) { 111 painter.wash(); 112 paintFirstRow(painter,circle,square); 113 paintSecondRow(painter,circle,square); 114 paintThirdRow(painter,circle,square); 115 paintSecondColumn(painter,circle,square); 116 } else if (x.equals("4")) { 117 painter.wash(); 118 paintHalfTopFirstColumn(painter,circle,square); 119 paintSecondRow(painter,circle,square); 120 paintSecondColumn(painter,circle,square); 121 } else if (x.equals("5")) { 122 painter.wash(); 123 paintFirstRow(painter,circle,square); 124 paintHalfTopFirstColumn(painter,circle,square); 125 paintSecondRow(painter,circle,square); 126 paintHalfBottomSecondColumn(painter,circle,square); 127 paintThirdRow(painter,circle,square); 128 } else if (x.equals("6")) { 129 painter.wash(); 130 paintFirstColumn(painter,circle,square); 131 paintFirstRow(painter,circle,square); 132 paintSecondRow(painter,circle,square); 133 paintHalfBottomSecondColumn(painter,circle,square); 134 paintThirdRow(painter,circle,square); 135 } else if (x.equals("7")) { 136 painter.wash(); 137 paintFirstRow(painter,circle,square); 138 paintSecondColumn(painter,circle,square); 139 } else if (x.equals("8")) { 140 painter.wash(); 141 paintFirstColumn(painter,circle,square); 142 paintSecondColumn(painter,circle,square); 143 paintFirstRow(painter,circle,square); 144 paintSecondRow(painter,circle,square); 145 paintThirdRow(painter,circle,square); 146 } else if (x.equals("9")) { 147 painter.wash(); 148 paintFirstRow(painter,circle,square); 149 paintHalfTopFirstColumn(painter,circle,square); 150 paintSecondRow(painter,circle,square); 151 paintSecondColumn(painter,circle,square); 152 } else if (x.equals(":")){ 153 painter.wash(); 154 paintOneCircle(painter,circle,square); 155 painter.mbk(circle.diameter()); 156 paintOneCircle(painter,circle,square); 157 } 158 } 159 160 private void paintHalfBottomSecondColumn(SPainter painter, SCircle circle, SSquare square) { 161 Point2D.Double position = new Point2D.Double(175,175); 162 painter.setPosition(position); 163 for (int i = 0; i <= 3; i++){ 164 paintOneCircle(painter,circle,square); 165 painter.mbk(circle.diameter()); 166 try { 167 Thread.sleep(50); 168 } catch (InterruptedException e) { 169 e.printStackTrace(); 170 } 171 } 172 } 173 174 private void paintHalfTopFirstColumn(SPainter painter, SCircle circle,SSquare square) { 175 Point2D.Double position = new Point2D.Double(25,25); 176 painter.setPosition(position); 177 int i = 0; 178 // while loop here!!! 179 while ( i <= 3){ 180 paintOneCircle(painter,circle,square); 181 painter.mbk(circle.diameter()); 182 i++; 183 try { 184 Thread.sleep(50); 185 } catch (InterruptedException e) { 186 e.printStackTrace(); 187 } 188 } 189 } 190 191 private void paintHalfBottomFirstColumn(SPainter painter, SCircle circle,SSquare square) { 192 Point2D.Double position = new Point2D.Double(25,175); 193 painter.setPosition(position); 194 for (int i = 0; i <= 3; i++){ 195 paintOneCircle(painter,circle,square); 196 painter.mbk(circle.diameter()); 197 try { 198 Thread.sleep(50); 199 } catch (InterruptedException e) { 200 e.printStackTrace(); 201 } 202 } 203 } 204 205 private void paintHalfTopSecondColumn(SPainter painter, SCircle circle,SSquare square) { 206 Point2D.Double position = new Point2D.Double(175,25); 207 painter.setPosition(position); 208 for (int i = 0; i <= 3; i++){ 209 paintOneCircle(painter,circle,square); 210 painter.mbk(circle.diameter()); 211 try { 212 Thread.sleep(50); 213 } catch (InterruptedException e) { 214 e.printStackTrace(); 215 } 216 } 217 } 218 219 private void paintThirdRow(SPainter painter, SCircle circle,SSquare square) { 220 Point2D.Double position = new Point2D.Double(25,325); 221 painter.setPosition(position); 222 for (int i = 0; i <= 3; i++){ 223 paintOneCircle(painter,circle,square); 224 painter.mrt(circle.diameter()); 225 try { 226 Thread.sleep(50); 227 } catch (InterruptedException e) { 228 e.printStackTrace(); 229 } 230 } 231 } 232 233 private void paintSecondRow(SPainter painter, SCircle circle,SSquare square) { 234 Point2D.Double position = new Point2D.Double(25,175); 235 painter.setPosition(position); 236 for (int i = 0; i <= 3; i++){ 237 paintOneCircle(painter,circle,square); 238 painter.mrt(circle.diameter()); 239 try { 240 Thread.sleep(50); 241 } catch (InterruptedException e) { 242 e.printStackTrace(); 243 } 244 } 245 } 246 247 private void paintFirstRow(SPainter painter, SCircle circle, SSquare square) { 248 Point2D.Double position = new Point2D.Double(25,25); 249 painter.setPosition(position); 250 for (int i = 0; i <= 3; i++){ 251 paintOneCircle(painter,circle,square); 252 painter.mrt(circle.diameter()); 253 try { 254 Thread.sleep(50); 255 } catch (InterruptedException e) { 256 e.printStackTrace(); 257 } 258 } 259 } 260 261 private void paintSecondColumn(SPainter painter, SCircle circle, SSquare square) { 262 Point2D.Double position = new Point2D.Double(175,25); 263 painter.setPosition(position); 264 for (int i = 0; i <= 6; i++){ 265 paintOneCircle(painter,circle,square); 266 painter.mbk(circle.diameter()); 267 try { 268 Thread.sleep(50); 269 } catch (InterruptedException e) { 270 e.printStackTrace(); 271 } 272 } 273 } 274 275 private void paintFirstColumn(SPainter painter, SCircle circle, SSquare square) { 276 Point2D.Double position = new Point2D.Double(25,25); 277 painter.setPosition(position); 278 for (int i = 0; i <= 6; i++){ 279 paintOneCircle(painter,circle,square); 280 painter.mbk(circle.diameter()); 281 try { 282 Thread.sleep(50); 283 } catch (InterruptedException e) { 284 e.printStackTrace(); 285 } 286 } 287 } 288 289 // paint the square too so that the number is more clear. 290 private void paintOneCircle(SPainter painter, SCircle circle,SSquare square) { 291 painter.setColor(Color.red); 292 painter.paint(square); 293 painter.setColor(Color.YELLOW); 294 painter.paint(circle); 295 painter.setColor(Color.BLACK); 296 painter.draw(square); 297 } 298 299 300 // REQUIRED INFRASTRUCTURE 301 302 public Invention1() { 303 paintTheImage(); 304 } 305 306 public static void main(String[] args) { 307 SwingUtilities.invokeLater(new Runnable() { 308 public void run() { 309 new Invention1() ; 310 } 311 }); 312 } 313 }