Skip to content
Snippets Groups Projects
Commit 68aa5b6b authored by Keiko Katsuragawa's avatar Keiko Katsuragawa
Browse files

bug fix

parent a6d6d19c
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
}
}
......
......@@ -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));
......
......@@ -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);
}
......
......@@ -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);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment