
Import java.Awt.Color;
/**
* Класс, описывающий трапецию
*/
public class Trapezium extends FigureClass {
protected int xA, xB, xC, xD;
protected int yA, yB, yC, yD;
protected Color bgColor;
public int getXA() {
return xA;
}
public int getXB() {
return xB;
}
public int getXC() {
return xC;
}
public int getXD() {
return xD;
}
public int getYA() {
return yA;
}
public int getYB() {
return yB;
}
public int getYC() {
return yC;
}
public int getYD() {
return yD;
}
public Color getBgColor() {
return bgColor;
}
private void reCalcPositions (int x, int y, int h, int a, int sizeAE, int sizeFD) {
this.x = x;
this.y = y;
// Пересчитаем координаты точек трапеции...
// по горизонтали...
this.xA = this.x - sizeAE;
this.xB = this.x;
this.xC = this.x + a;
this.xD = this.x + a + sizeFD;
// по вертикали...
this.yA = this.y;
this.yB = this.y + h;
this.yC = this.y + h;
this.yD = this.y;
}
/**
* Построение трапеции по её высоте, длине меньшего основания и двум дополнениям к большему основанию
* <pre> <b>a</b>
* <b>B</b> <b>•</b>-------------------------<b>•</b> <b>C</b>
* /| BE = CF - <i>высота</i> (<b>h</b>) | \
* / | BC = EF - <i>меньшее</i> |<b>h</b> \
* / <b>h</b>| <i>основание</i> (<b>a</b>)| \
* / | <b>E(<i>x,y</i>)</b> <b>a</b> | <b>F</b> \
* <b>A</b><b>•</b>----<b>•</b>----------------------------------<b>•</b> <b>D</b>
* <br />
* @param x - X-координата точки P
* @param y - Y-координата точки P
* @param h - Высота
* @param a - Длина меньшего основания
* @param sizeAE - Длина отрезка AE
* @param sizeFD - Длина отрезка FD
* @param bgColor - Fill color
* @param borderColor - Border color
* </pre>
*/
public Trapezium(int x, int y, int h, int a, int sizeAE, int sizeFD, Color bgColor, Color borderColor) {
super();
if (borderColor != null) this.color = borderColor;
if (bgColor != null) this.bgColor = bgColor;
reCalcPositions(x, y, h, a, sizeAE, sizeFD);
System.out.println("Рисуем трапецию с координатами точек:");
System.out.println(" A(" + xA + ", " + yA + "),");
System.out.println(" B(" + xB + ", " + yB + "),");
System.out.println(" C(" + xC + ", " + yC + "),");
System.out.println(" D(" + xD + ", " + yD + ").");
}
/**
* Move Trapezium 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.xA += dx;
this.xB += dx;
this.xC += dx;
this.xD += dx;
this.yA += dy;
this.yB += dy;
this.yC += dy;
this.yD += dy;
System.out.println("Новые координаты трапеции:");
System.out.println(" A(" + xA + ", " + yA + "),");
System.out.println(" B(" + xB + ", " + yB + "),");
System.out.println(" C(" + xC + ", " + yC + "),");
System.out.println(" D(" + xD + ", " + yD + ").");
}
/**
* Нарисовать трапецию с новыми параметрами
* @param x - X-координата точки P
* @param y - Y-координата точки P
* @param h - Высота
* @param a - Длина меньшего основания
* @param sizeAE - Длина отрезка AE
* @param sizeFD - Длина отрезка FD
* @param bgColor - Fill color
* @param borderColor - Border color
*/
public void reDraw (int x, int y, int h, int a, int sizeAE, int sizeFD, Color bgColor, Color borderColor) {
if (borderColor != null) this.color = borderColor;
if (bgColor != null) this.bgColor = bgColor;
reCalcPositions(x, y, h, a, sizeAE, sizeFD);
System.out.println("Рисуем трапецию с координатами точек:");
System.out.println(" A(" + xA + ", " + yA + "),");
System.out.println(" B(" + xB + ", " + yB + "),");
System.out.println(" C(" + xC + ", " + yC + "),");
System.out.println(" D(" + xD + ", " + yD + ").");
}
}
Треугольник. Треугольник проще всего будет строить по его трем точкам. Реализация класса «Triangle.java» следующая: