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