1 /* 2 * A variant of program Number3. 3 * Program to paint dots of the named color, or black dots should the user type in something other than the three specified color names. 4 */ 5 package npw; 6 7 import java.awt.Color; 8 import java.util.Random; 9 import java.util.Scanner; 10 import javax.swing.JOptionPane; 11 import javax.swing.SwingUtilities; 12 13 import painter.SPainter; 14 import shapes.SCircle; 15 16 public class Number4 { 17 // The solution to the graphical problem. 18 private void paintTheImage() { 19 // Get the input information from the user. 20 int nrOfRows = getNumber("rows"); 21 int nrOfColumns = getNumber("columns"); 22 int sizeOfCircle = getNumber("Color side length"); 23 String colorOfCircle = getColor ("Color of Circles"); 24 //Establish the painter. 25 int height = nrOfRows * sizeOfCircle; 26 int width = nrOfColumns * sizeOfCircle; 27 SPainter miro = new SPainter("Number 3", width + 50, height + 50); 28 miro.setBrushWidth(4); 29 SCircle circle = new SCircle (sizeOfCircle); 30 //Paint the rectangles. 31 paintRectangle(miro, circle, nrOfRows, nrOfColumns, colorOfCircle); 32 } 33 34 private String getColor(String prompt) { 35 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 36 Scanner scanner = new Scanner(nss); 37 return scanner.nextLine(); 38 } 39 40 private int getNumber(String prompt) { 41 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 42 Scanner scanner = new Scanner(nss); 43 return scanner.nextInt(); 44 } 45 46 private static void paintRectangle(SPainter miro, SCircle circle, int nrOfRows, int nrOfColumns, String colorOfCircle) { 47 // Position the painter to paint the rectangle of squares. 48 miro.mlt(((nrOfColumns - 1) / 2.0) * circle.diameter()); 49 miro.mbk(((nrOfRows - 1) / 2.0) * circle.diameter()); 50 //Paint the rectangle of squares. 51 int i = 1; 52 while (i <= nrOfRows) { 53 paintOneRow(miro, nrOfColumns, circle, colorOfCircle); 54 miro.mfd(circle.diameter()); 55 i = i + 1; 56 } 57 //Make the method invariant with respect to painter position. 58 miro.mrt(((nrOfColumns - 1) / 2.0) * circle.diameter()); 59 miro.mfd(((nrOfRows - 1) / 2.0) * circle.diameter()); 60 } 61 62 private static void paintOneRow(SPainter miro, int nrOfColumns, SCircle circle, String colorOfCircle) { 63 int i = 1; 64 while (i <= nrOfColumns) { 65 paintOneCircle(miro,circle,colorOfCircle); 66 miro.mrt(circle.diameter()); 67 i = i + 1; 68 } 69 miro.mlt(nrOfColumns * circle.diameter()); 70 } 71 72 private static void paintOneCircle(SPainter miro, SCircle circle, String colorOfCircle) { 73 circle.s2(); 74 if (colorOfCircle.equalsIgnoreCase("RED")){ 75 miro.setColor(Color.RED); 76 miro.paint(circle); 77 } 78 else if (colorOfCircle.equalsIgnoreCase("BLUE")){ 79 miro.setColor(Color.BLUE); 80 miro.paint(circle); 81 } 82 else if (colorOfCircle.equalsIgnoreCase("GREEN")){ 83 miro.setColor(Color.GREEN); 84 miro.paint(circle); 85 } 86 else { 87 miro.setColor(Color.BLACK); 88 miro.paint(circle); 89 } 90 circle.x2(); 91 92 } 93 94 private static Color randomColor() { 95 Random rgen = new Random(); 96 int r = rgen.nextInt(256); 97 int g = rgen.nextInt(256); 98 int b = rgen.nextInt(256); 99 return new Color(r, b, g); 100 } 101 102 public Number4(){ 103 paintTheImage(); 104 } 105 106 public static void main(String[] args){ 107 SwingUtilities.invokeLater(new Runnable() { 108 @Override 109 public void run() { 110 new Number4(); 111 } 112 }); 113 114 } 115 } 116