-
Notifications
You must be signed in to change notification settings - Fork 0
/
Canvas.java
86 lines (72 loc) · 2.3 KB
/
Canvas.java
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
package com.javarush.task.task24.task2413;
/**
* Класс-холст для отрисовки.
*/
public class Canvas {
//ширина и высота
private int width;
private int height;
//матрица, где рисуем. символ - это цвет.
private char[][] matrix;
public Canvas(int width, int height) {
this.width = width;
this.height = height;
this.matrix = new char[height + 2][width + 2];
}
/**
* Очищаем холст
*/
void clear() {
this.matrix = new char[height + 2][width + 2];
}
/**
* Печатаем переданную фигуру в указанных координатах цветом c.
* Если переданный массив содержит единицы, то на холсте им будут соответствовать символы - с.
*/
void drawMatrix(double x, double y, int[][] matrix, char c) {
int height = matrix.length;
int width = matrix[0].length;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (matrix[i][j] == 1)
setPoint(x + j, y + i, c);
}
}
}
/**
* Ставим одну точку на холсте с координатами (x,y) и цветом - c.
*/
void setPoint(double x, double y, char c) {
int x0 = (int) Math.round(x);
int y0 = (int) Math.round(y);
if (y0 < 0 || y0 >= matrix.length) return;
if (x0 < 0 || x0 >= matrix[y0].length) return;
matrix[y0][x0] = c;
}
/**
* Печатаем содержимое холста на экран.
*/
void print() {
System.out.println();
for (int i = 0; i < height + 2; i++) {
for (int j = 0; j < width + 2; j++) {
System.out.print(" ");
System.out.print(matrix[i][j]);
System.out.print(" ");
}
System.out.println();
}
System.out.println();
System.out.println();
System.out.println();
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public char[][] getMatrix() {
return matrix;
}
}