
Import java.Awt.Color;
/**
* Класс, описывающий квадрат
*/
public class Square extends FigureClass {
protected int size;
protected Color bgColor;
public int getSize() {
return size;
}
public Color getBgColor() {
return bgColor;
}
/**
* Drawing Square
* @param x - Horizontal (Base) point position
* @param y - Vertical (Base) point position
* @param size - Side length
* @param bgColor - Fill color
* @param borderColor - Border color
*/
Square (int x, int y, int size, Color bgColor, Color borderColor) {
super();
this.x = x;
this.y = y;
this.size = size;
this.color = borderColor;
this.bgColor = bgColor;
System.out.println("Square nas been drawed from position (x, y) = (" + x + ", " + y + ") and side length = " + size);
}
/**
* Move Square to new position with NO change others parameters
* @param x - Horizontal (Base) point position
* @param y - Vertical (Base) point position
*/
public void moveTo (int x, int y) {
this.x = x;
this.y = y;
System.out.println("Square nas been moved to new position (x, y) = (" + x + ", " + y + ")");
}
/**
* reDraw Square to new position with change others parameters
* @param x - Horizontal (Base) point position
* @param y - Vertical (Base) point position
* @param size - Side length
* @param bgColor - Fill color
* @param borderColor - Border color
*/
public void reDraw (int x, int y, int size, Color bgColor, Color borderColor) {
this.x = x;
this.y = y;
this.size = size;
if (bgColor != null) this.bgColor = bgColor;
if (borderColor != null) this.color = borderColor;
System.out.println("Square nas been moved to new position (x, y) = (" + x + ", " + y + ") with new size " + size + "");
}
}
Перейдем к ромбу. Ромб будет строить по координатам базовой точки - точки пересечения его диагоналей и длине этих диагоналей. Также, считаем, что диагонали ромба параллельны осям координат. Реализация класса «Rhomb.java»:
Import java.Awt.Color;
/**
* Класс, описывающий ромб
*/
public class Rhomb extends FigureClass {
protected int x1, x2, x3, x4;
protected int y1, y2, y3, y4;
protected Color bgColor;
public int getX1() {
return x1;
}
public int getX2() {
return x2;
}
public int getX3() {
return x3;
}
public int getX4() {
return x4;
}
public int getY1() {
return y1;
}
public int getY2() {
return y2;
}
public int getY3() {
return y3;
}
public int getY4() {
return y4;
}
public Color getBgColor() {
return bgColor;
}
private void reCalcPositions (int x, int y, int vDiagSize, int hDiagSize) {
this.x = x;
this.y = y;
// Пересчитаем координаты точек ромба...
// по горизонтали...
this.x1 = this.x - (hDiagSize/2);
this.x2 = this.x;
this.x3 = this.x + (hDiagSize/2);
this.x4 = this.x;
// по вертикали...
this.y1 = this.y;
this.y2 = this.y + (vDiagSize/2);
this.y3 = this.y;
this.y4 = this.y - (vDiagSize/2);
}
/**
* Построение ромба по точке пересечения диагоналей и длинам этих диагоналей
* @param x
* @param y
* @param vDiagSize
* @param hDiagSize
* @param bgColor
* @param borderColor
*/
public Rhomb(int x, int y, int vDiagSize, int hDiagSize, Color bgColor, Color borderColor) {
super();
if (borderColor != null) this.color = borderColor;
if (bgColor != null) this.bgColor = bgColor;
reCalcPositions(x, y, vDiagSize, hDiagSize);
System.out.println("Рисуем ромб по точке пересечения диагоналей (x, y)=(" + x + ", " + y + ") и их длинам (" + hDiagSize + ", " + vDiagSize + ")");
}
/**
* Move RHomb to new position with NO change others parameters
* @param x - Horizontal (Base) point position
* @param y - Vertical (Base) point position
*/
public void moveTo (int new_X, int new_Y) {
int dx, dy;
dx = new_X - this.x;
dy = new_Y - this.y;
this.x1 += dx;
this.x2 += dx;
this.x3 += dx;
this.x4 += dx;
this.y1 += dy;
this.y2 += dy;
this.y3 += dy;
this.y4 += dy;
System.out.println("Rhomb nas been moved to new position (x, y) = (" + x + ", " + y + ")");
}
/**
* Нарисовать ромб с новыми параметрами
* @param x
* @param y
* @param vDiagSize
* @param hDiagSize
* @param bgColor
* @param borderColor
*/
public void reDraw (int x, int y, int vDiagSize, int hDiagSize, Color bgColor, Color borderColor) {
reCalcPositions(x, y, vDiagSize, hDiagSize);
if (bgColor != null) this.bgColor = bgColor;
if (borderColor != null) this.color = borderColor;
System.out.println("Рисуем ромб по точке пересечения диагоналей (x, y)=(" + x + ", " + y + ") и их длинам (" + hDiagSize + ", " + vDiagSize + ")");
}
}
Теперь трапеция. Строить трапецию будем по её высоте, длине меньшего основания и двум дополнениям к большему основанию.
Конструктор класса для трапеции:
Trapezium (int x, int y, int h, int a, int sizeAE, int sizeFD, Color bgColor, Color borderColor)
a
B •-------------------------• C
/| BE = CF - высота (h) | \
/ | BC = EF - меньшее |h \
/ h| основание (a)| \
/ | E(x,y) a | F \
A•----•----------------------------------• D
Parameters:
x - X-координата точки P
y - Y-координата точки P
h - Высота
a - Длина меньшего основания
sizeAE - Длина отрезка AE
sizeFD - Длина отрезка FD
bgColor - Fill color
borderColor - Border color
Реализация класса «Trapezium.java»: