diff --git a/src/com/annimon/graphics/GraphicsExt.java b/src/com/annimon/graphics/GraphicsExt.java index 5b68b81..bd2fa7b 100644 --- a/src/com/annimon/graphics/GraphicsExt.java +++ b/src/com/annimon/graphics/GraphicsExt.java @@ -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) {