Класс графики с учётом матрицы преобразований
This commit is contained in:
parent
db863112de
commit
ca39d89d48
@ -15,7 +15,8 @@ public class GraphicsExt {
|
|||||||
private final Graphics g;
|
private final Graphics g;
|
||||||
private final int width, height;
|
private final int width, height;
|
||||||
|
|
||||||
private double oldX, oldY;
|
private Point old;
|
||||||
|
private Matrix transformMatrix;
|
||||||
|
|
||||||
public GraphicsExt(Graphics g, int width, int height) {
|
public GraphicsExt(Graphics g, int width, int height) {
|
||||||
this.g = g;
|
this.g = g;
|
||||||
@ -24,22 +25,55 @@ public class GraphicsExt {
|
|||||||
g.setColor(Color.BLACK);
|
g.setColor(Color.BLACK);
|
||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
oldX = oldY = 0;
|
old = new Point();
|
||||||
|
|
||||||
|
transformMatrix = new Matrix();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void move(double x, double y) {
|
public void move(double x, double y) {
|
||||||
oldX = x;
|
old.setX(x);
|
||||||
oldY = y;
|
old.setY(y);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void move(Point point) {
|
||||||
|
old = point;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void draw(double x, double y) {
|
public void draw(double x, double y) {
|
||||||
line(oldX, oldY, x, y);
|
Point point = new Point(x, y);
|
||||||
oldX = x;
|
line(old, point);
|
||||||
oldY = y;
|
old = point;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void draw(Point point) {
|
||||||
|
line(old, point);
|
||||||
|
old = point;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reset() {
|
||||||
|
transformMatrix.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void rotate(double angle) {
|
||||||
|
transformMatrix = transformMatrix.multiply(Matrix.createRotationMatrix(angle));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void translate(double tx, double ty) {
|
||||||
|
transformMatrix.translate(tx, ty);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void scale(double value) {
|
||||||
|
transformMatrix.multiply(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void line(double x1, double y1, double x2, double y2) {
|
public void line(double x1, double y1, double x2, double y2) {
|
||||||
g.drawLine(convertX(x1), convertY(y1), convertX(x2), convertY(y2));
|
line(new Point(x1, x2), new Point(x2, y2));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void line(Point from, Point to) {
|
||||||
|
double[] p1 = transformMatrix.applyTransform(from.getVector3());
|
||||||
|
double[] p2 = transformMatrix.applyTransform(to.getVector3());
|
||||||
|
g.drawLine(convertX(p1[0]), convertY(p1[1]), convertX(p2[0]), convertY(p2[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
private int convertX(double x) {
|
private int convertX(double x) {
|
||||||
|
Loading…
Reference in New Issue
Block a user