-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRectangle.java
More file actions
128 lines (111 loc) · 4.24 KB
/
Rectangle.java
File metadata and controls
128 lines (111 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package Assignment_1;
import java.util.Scanner;
public class Rectangle {
private int recWidth;
private int recHeight;
private int canvasWidth;
private int canvasHeight;
private String printingCharacter;
private int[] topLeftCoordinate = {0,0}; // longer length of canvas
// Constructor
public Rectangle(int recWidth, int recHeight, int canvasWidth, int canvasHeight, String printingCharacter) {
this.recWidth = recWidth;
this.recHeight = recHeight;
this.canvasWidth = canvasWidth;
this.canvasHeight = canvasHeight;
this.printingCharacter = printingCharacter;
}
// Looping to draw the rectangle on the Canvas space.
// w: Width; h: Height, to understand the loop easier.
public void drawingRectangle(String backgroundCharacter) {
int recHeight = this.recHeight;
int recWidth = this.recWidth;
for(int h=0; h < this.canvasHeight; h++) {
String line = "";
if (h < recHeight) {
for(int w=0; w < this.canvasWidth; w++) {
if (w <recWidth) {
line += this.printingCharacter;
}
else {
line += backgroundCharacter;
}
}
}
else {
for(int w=0; w < this.canvasWidth; w++) {
line += backgroundCharacter;
}
}
System.out.println(line);
}
}
public void moveRectangle(String move, String backgroundCharacter) {
if (move.equalsIgnoreCase("a")){
topLeftCoordinate[0] -= 1;
if (topLeftCoordinate[0] < 0) {
topLeftCoordinate[0] = 0;
System.out.println("You cannot move this rectangle outside of the drawing canvas!");
}
}
if (move.equalsIgnoreCase("s")){
topLeftCoordinate[0] += 1;
if (topLeftCoordinate[0] > (this.canvasWidth - this.recWidth)) {
topLeftCoordinate[0] -= 1;
System.out.println("You cannot move this rectangle outside of the drawing canvas!");
}
}
if (move.equalsIgnoreCase("w")){
topLeftCoordinate[1] -= 1;
if (topLeftCoordinate[1] < 0) {
topLeftCoordinate[1] = 0;
System.out.println("You cannot move this rectangle outside of the drawing canvas!");
}
}
if (move.equalsIgnoreCase("z")){
topLeftCoordinate[1] += 1;
if (topLeftCoordinate[1] > (this.canvasHeight - this.recHeight)) {
topLeftCoordinate[1] -= 1;
System.out.println("You cannot move this rectangle outside of the drawing canvas!");
}
}
int recHeight = this.recHeight;
int recWidth = this.recWidth;
for(int h=0; h < this.canvasHeight; h++) {
String line = "";
if (h < topLeftCoordinate[1]) {
line = backgroundCharacter.repeat(canvasWidth);
}
else if (h >= recHeight + topLeftCoordinate[1]) {
line = backgroundCharacter.repeat(canvasWidth);
}
else {
for(int w=0; w < this.canvasWidth; w++) {
if (w < topLeftCoordinate[0]) {
line += backgroundCharacter;
continue;
}
if (w < recWidth + topLeftCoordinate[0]) {
line += this.printingCharacter;
}
else {
line += backgroundCharacter;
}
}
}
System.out.println(line);
}
}
public void setRecWidth(int recWidth) {
this.recWidth = recWidth;
}
public void setRecHeight(int recHeight) {
this.recHeight = recHeight;
}
public int getRecWidth() {
return this.recWidth;
}
public int getRecHeight() {
return this.recHeight;
}
}