Класс графики с учётом матрицы преобразований

This commit is contained in:
Victor 2014-02-22 14:46:10 +02:00
parent db863112de
commit ca39d89d48

View File

@ -15,7 +15,8 @@ public class GraphicsExt {
private final Graphics g;
private final int width, height;
private double oldX, oldY;
private Point old;
private Matrix transformMatrix;
public GraphicsExt(Graphics g, int width, int height) {
this.g = g;
@ -24,22 +25,55 @@ public class GraphicsExt {
g.setColor(Color.BLACK);
this.width = width;
this.height = height;
oldX = oldY = 0;
old = new Point();
transformMatrix = new Matrix();
}
public void move(double x, double y) {
oldX = x;
oldY = y;
old.setX(x);
old.setY(y);
}
public void move(Point point) {
old = point;
}
public void draw(double x, double y) {
line(oldX, oldY, x, y);
oldX = x;
oldY = y;
Point point = new Point(x, y);
line(old, point);
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) {
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) {