From 68aa5b6b498e3720de2d9ea0e1d52b7a67044941 Mon Sep 17 00:00:00 2001 From: Keiko Katsuragawa <kkatsuragawa@uwaterloo.ca> Date: Fri, 2 Feb 2018 09:09:48 -0500 Subject: [PATCH] bug fix --- java/2-8-Transformation/BarExercise.java | 43 +++++++++---------- java/2-8-Transformation/Transform2.java | 2 +- java/2-8-Transformation/shapemodel/Shape.java | 12 +++++- .../shapemodel/ShapeDemo.java | 8 ++-- 4 files changed, 35 insertions(+), 30 deletions(-) diff --git a/java/2-8-Transformation/BarExercise.java b/java/2-8-Transformation/BarExercise.java index 89b1e80..16fc9d3 100644 --- a/java/2-8-Transformation/BarExercise.java +++ b/java/2-8-Transformation/BarExercise.java @@ -34,21 +34,17 @@ public class BarExercise { class Canvas extends JComponent { - // the house shape (model position is centred at top left corner) - private Polygon shape = new Polygon(new int[] { -50, 50, 50, 0, -50}, - new int[] { 75, 75, -25, -75, -25}, 5); - int step = 0; Canvas() { // only mouse clicked events addMouseListener(new MouseAdapter() { - public void mouseClicked(MouseEvent me) { + public void mouseClicked(MouseEvent me) { step ++; repaint(); - } - }); + } + }); } // custom graphics drawing @@ -68,11 +64,12 @@ class Canvas extends JComponent { // save the current transform matrix AffineTransform M = g2.getTransform(); - // the shape will get transformed into "world" coordinates + // transfrom at one time if (true) { g2.translate(50, 100); g2.rotate(Math.toRadians(30)); g2.translate(-50, -100); + g2.setColor(Color.BLUE.darker()); drawBar(g2, 50, 100, 150, 100); @@ -82,23 +79,23 @@ class Canvas extends JComponent { switch (step % 4) { case 1: - g2.translate(-50, -100); - drawBar(g2, 50, 100, 150, 100); - g2.setTransform(M); - break; + g2.translate(-50, -100); + drawBar(g2, 50, 100, 150, 100); + g2.setTransform(M); + break; case 2: - g2.rotate(Math.toRadians(30)); - g2.translate(-50, -100); - drawBar(g2, 50, 100, 150, 100); - g2.setTransform(M); - break; + g2.rotate(Math.toRadians(30)); + g2.translate(-50, -100); + drawBar(g2, 50, 100, 150, 100); + g2.setTransform(M); + break; case 3: - g2.translate(50, 100); - g2.rotate(Math.toRadians(30)); - g2.translate(-50, -100); - drawBar(g2, 50, 100, 150, 100); - g2.setTransform(M); - break; + g2.translate(50, 100); + g2.rotate(Math.toRadians(30)); + g2.translate(-50, -100); + drawBar(g2, 50, 100, 150, 100); + g2.setTransform(M); + break; } } } diff --git a/java/2-8-Transformation/Transform2.java b/java/2-8-Transformation/Transform2.java index ba650cf..f0ed75a 100644 --- a/java/2-8-Transformation/Transform2.java +++ b/java/2-8-Transformation/Transform2.java @@ -54,7 +54,7 @@ class Canvas extends JComponent { // the shape will get transformed when rendered g2.translate(M.x, M.y); - g2.rotate(45); + g2.rotate(Math.toRadians(45)); g2.scale(2, 1); g2.setStroke(new BasicStroke(3)); diff --git a/java/2-8-Transformation/shapemodel/Shape.java b/java/2-8-Transformation/shapemodel/Shape.java index 716741f..07701ea 100644 --- a/java/2-8-Transformation/shapemodel/Shape.java +++ b/java/2-8-Transformation/shapemodel/Shape.java @@ -27,6 +27,7 @@ class Shape { // add a point to end of shape public void addPoint(double x, double y) { + if (points == null) clearPoints(); addPoint(new Point2d(x, y)); } @@ -78,9 +79,16 @@ class Shape { // shape's transform - // quick hack, get and set would be better float scale = 1.0f; + public float getScale(){ + return scale; + } + + public void setScale(float scale){ + this.scale = scale; + } + // some optimization to cache points for drawing Boolean pointsChanged = false; // dirty bit int[] xpoints, ypoints; @@ -129,7 +137,7 @@ class Shape { } // reset the transform to what it was before we drew the shape - g2.setTransform(M); +// g2.setTransform(M); } diff --git a/java/2-8-Transformation/shapemodel/ShapeDemo.java b/java/2-8-Transformation/shapemodel/ShapeDemo.java index eb1ebc1..42829c0 100644 --- a/java/2-8-Transformation/shapemodel/ShapeDemo.java +++ b/java/2-8-Transformation/shapemodel/ShapeDemo.java @@ -23,12 +23,12 @@ public class ShapeDemo extends JPanel { shape = new Shape(); // change shape type - // shape.setIsClosed(true); - // shape.setIsFilled(true); +// shape.setIsClosed(true); +// shape.setIsFilled(true); shape.setColour(Color.BLUE); // try setting scale to something other than 1 - shape.scale = 1.0f; + shape.setScale(1.0f); repaint(); } @@ -57,7 +57,7 @@ public class ShapeDemo extends JPanel { Graphics2D g2 = (Graphics2D) g; // cast to get 2D drawing methods g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // antialiasing look nicer RenderingHints.VALUE_ANTIALIAS_ON); - + if (shape != null) shape.draw(g2); } -- GitLab