1 /* 2 * Program to paint rows and columns of uniformly colored dots based on user input 3 * (options are red, green, and blue), with white space in between each dot. 4 */ 5 6 package npw; 7 8 import painter.SPainter; 9 import shapes.SCircle; 10 import javax.swing.*; 11 import java.awt.*; 12 import java.util.Scanner; 13 14 public class Number4 { 15 //The solution to the graphical problem 16 private void paintTheImage() { 17 //Get the input info from the user 18 int nrOfRows = getNumber("rows"); 19 int nrOfColumns = getNumber("columns"); 20 int sizeOfCircle = getNumber("circle radius"); 21 String colorChoice = getColor("red, green, or blue"); 22 //Establish the painter 23 int height = nrOfRows * sizeOfCircle; 24 int width = nrOfColumns * sizeOfCircle; 25 SPainter miro = new SPainter("Number 4", (width*2) + 50, (height*2) + 50); 26 miro.setBrushWidth(4); 27 SCircle circle = new SCircle(sizeOfCircle); 28 //Paint the rectangles 29 paintRectangle(miro, circle, colorChoice, nrOfRows, nrOfColumns); 30 } 31 32 private static int getNumber(String prompt) { 33 String nss = JOptionPane.showInputDialog(null, prompt+"?"); 34 Scanner scanner = new Scanner(nss); 35 return scanner.nextInt(); 36 } 37 38 private static String getColor(String prompt) { 39 String colorString = JOptionPane.showInputDialog(null, prompt+"?"); 40 Scanner scanner = new Scanner(colorString); 41 return scanner.next(); 42 } 43 44 private static void paintRectangle(SPainter miro, SCircle circle, String colorChoice, int nrOfRows, int nrOfColumns) { 45 //Position the painter to paint the rectangle of circles 46 miro.mlt(((nrOfColumns-1)/2.0) * circle.diameter()); 47 miro.mbk(((nrOfRows-1)/2.0) * circle.diameter()); 48 //Paint the rectangle of circles 49 int i = 1; 50 while(i <= nrOfRows) { 51 paintOneRow(miro, colorChoice, nrOfColumns, circle); 52 miro.mfd(circle.diameter()); 53 i++; 54 } 55 //Make the method invariant with respect to pointer position 56 miro.mrt(((nrOfColumns-1)/2.0) * circle.diameter()); 57 miro.mfd(((nrOfRows-1)/2.0) * circle.diameter()); 58 } 59 60 private static void paintOneRow(SPainter miro, String colorChoice, int nrOfColumns, SCircle circle) { 61 int i = 1; 62 while(i <= nrOfColumns) { 63 paintOneCircle(miro, colorChoice, circle); 64 miro.mrt(circle.diameter()); 65 i++; 66 } 67 miro.mlt(nrOfColumns*circle.diameter()); 68 } 69 70 private static void paintOneCircle(SPainter miro, String colorChoice, SCircle circle) { 71 circle.s2(); //make dot smaller by half to show white space 72 if (colorChoice.equalsIgnoreCase("red")) { 73 miro.setColor(Color.RED); 74 } else if (colorChoice.equalsIgnoreCase("green")) { 75 miro.setColor(Color.GREEN); 76 } else if (colorChoice.equalsIgnoreCase("blue")) { 77 miro.setColor(Color.BLUE); 78 } else { 79 miro.setColor(Color.BLACK); //black dots if user enters an unaccepted color 80 } 81 miro.paint(circle); 82 circle.x2(); //put dot back to regular size so loop doesn't go cray 83 } 84 85 public Number4() { 86 paintTheImage(); 87 } 88 89 public static void main(String[] args) { 90 SwingUtilities.invokeLater(new Runnable() { 91 @Override 92 public void run() { 93 new Number4(); 94 } 95 }); 96 } 97 }