1 package NPW; 2 3 import java.awt.Color; 4 import java.util.Random; 5 import java.util.Scanner; 6 import javax.swing.JOptionPane; 7 import javax.swing.SwingUtilities; 8 import painter.SPainter; 9 import shapes.SCircle; 10 import shapes.SSquare; 11 12 public class Number3 { 13 // The solution to the graphical problem. 14 15 private void paintTheImage() { 16 // Get the input information from the user. 17 int nrOfRows = getNumber("rows"); 18 int nrOfColumns = getNumber("columns"); 19 int sizeOfCirc = getNumber("Circle radius length"); 20 21 // Establish the painter. 22 int height = nrOfRows * sizeOfCirc; 23 int width = nrOfColumns * sizeOfCirc; 24 SPainter miro = new SPainter("Number 1",width+50,height+50); 25 miro.setBrushWidth(4); 26 SCircle circ = new SCircle(sizeOfCirc); 27 28 // Paint the circles. 29 paintCircles(miro,circ,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 void paintCircles(SPainter miro, SCircle circ, 39 int nrOfRows, int nrOfColumns) { 40 // Position the painter to paint the rectangle of squares. 41 miro.mlt(((nrOfColumns-1)/2.0) * circ.radius()); 42 miro.mbk(((nrOfRows-1)/2.0) * circ.radius()); 43 44 // Paint the rectangle of squares. 45 int i = 1; 46 while ( i <= nrOfRows) { 47 paintOneRow(miro,nrOfColumns, circ); 48 miro.mfd(circ.radius()); 49 i = i + 1; 50 } 51 52 // Make the method invariant with respect to painter position. 53 miro.mrt(((nrOfColumns-1)/2.0) * circ.radius()); 54 miro.mfd(((nrOfRows-1)/2.0) * circ.radius()); 55 } 56 57 private static void paintOneRow(SPainter miro, int nrOfColumns, SCircle circ) { 58 int i = 1; 59 while ( i <= nrOfColumns ) { 60 paintOnedot(miro, circ); 61 miro.mrt(circ.radius()); 62 i = i + 1; 63 } 64 miro.mlt(nrOfColumns* circ.radius()); 65 } 66 // made 67 private static void paintOnedot(SPainter miro, SCircle circ) { 68 circ.s2(); 69 miro.setColor(randomColor()); 70 miro.paint(circ); 71 miro.draw(circ); 72 circ.x2(); 73 } 74 private static Color randomColor() { 75 Random rgen = new Random(); 76 int r = rgen.nextInt(256); 77 int g = rgen.nextInt(256); 78 int b = rgen.nextInt(256); 79 return new Color(r,b,g); 80 } 81 82 public Number3(){ 83 paintTheImage(); 84 } 85 86 public static void main(String[] args){ 87 SwingUtilities.invokeLater(new Runnable() { 88 @Override 89 public void run() { 90 new Number3(); 91 } 92 }); 93 } 94 }