/home/dmyrie/NetBeansProjects/CS1/src/npw/Number3.java |
1
2
3
4
5
6 package npw;
7
8 import java.awt.Color;
9 import java.util.Random;
10 import java.util.Scanner;
11 import javax.swing.JOptionPane;
12 import javax.swing.SwingUtilities;
13 import painter.SPainter;
14 import shapes.SCircle;
15
16
17
18 @author
19
20 public class Number3 {
21
22
23
24 @param args
25
26 public static void main(String[] args) {
27 SwingUtilities.invokeLater(new Runnable() {
28 public void run() {
29 new Number3();
30
31 }
32 });
33
34 }
35
36 public Number3() {
37 paintTheImage();
38 }
39
40
41 private void paintTheImage() {
42
43 int nrOfRows = getNumber("rows");
44 int nrOfColumns = getNumber("columns");
45 int sizeOfCircle = getNumber("circle diameter");
46
47
48 int height = nrOfRows * sizeOfCircle;
49 int width = nrOfColumns * sizeOfCircle;
50 SPainter miro = new SPainter("Number 3", width + 50, height + 50);
51 miro.setBrushWidth(4);
52 SCircle dot = new SCircle(sizeOfCircle / 2);
53
54
55 paintSquare(miro, dot, nrOfRows, nrOfColumns);
56 System.out.println("");
57
58 }
59
60 private static int getNumber(String prompt) {
61 String nss = JOptionPane.showInputDialog(null, prompt + "?");
62 Scanner scanner = new Scanner(nss);
63 return scanner.nextInt();
64 }
65
66 private static void paintSquare(SPainter miro, SCircle dot, int nrOfRows, int nrOfColumns) {
67
68 miro.mlt(((nrOfColumns - 1) / 2.0) * dot.diameter());
69 miro.mbk(((nrOfRows - 1) / 2.0) * dot.diameter());
70
71
72 int i = 1;
73 while (i <= nrOfRows) {
74 paintOneRow(miro, nrOfColumns, dot);
75 miro.mfd(dot.diameter());
76 i = i + 1;
77 }
78
79
80 miro.mrt(((nrOfColumns - 1) / 2.0) * dot.diameter());
81 miro.mfd(((nrOfRows - 1) / 2.0) * dot.diameter());
82 System.out.println("");
83
84 }
85
86 private static void paintOneRow(SPainter miro, int nrOfColumns, SCircle dot) {
87 int i = 1;
88 while (i <= nrOfColumns) {
89 paintOneCircle(miro, dot);
90 miro.mrt(dot.diameter());
91 i = i + 1;
92 }
93 miro.mlt(nrOfColumns * dot.diameter());
94 System.out.println("");
95
96 }
97
98 private static void paintOneCircle(SPainter miro, SCircle dot) {
99 dot.s2();
100 miro.setColor(randomColor());
101 miro.paint(dot);
102 miro.setColor(Color.WHITE);
103 miro.draw(dot);
104 dot.x2();
105
106 }
107
108 private static Color randomColor() {
109 Random rgen = new Random();
110 int r = rgen.nextInt(256);
111 int g = rgen.nextInt(256);
112 int b = rgen.nextInt(256);
113 return new Color(r, b, g);
114 }
115 }
116