diff --git a/javacooksrc/javacooksrc/main/java/plotter/PlotDriver.java b/javacooksrc/javacooksrc/main/java/plotter/PlotDriver.java index 773561a..1b3191e 100644 --- a/javacooksrc/javacooksrc/main/java/plotter/PlotDriver.java +++ b/javacooksrc/javacooksrc/main/java/plotter/PlotDriver.java @@ -43,6 +43,17 @@ public static void main(String[] argv) { r.drawString("Hello World"); r.penColor(4); r.drawBox(10, 10); + + //Draw a triangle with moveTo and rmoveTo + //proves that the PlotterAWT moveTo and rmoveTo works fine even when you don't have a drawBox afterward. + r.penUp(); + r.moveTo(r.MAXX - 10, r.MAXY - 10); + r.penDown(); + r.penColor(1); + r.rmoveTo(-50, 0); + r.rmoveTo(50,-50); + r.rmoveTo(0, 50); + r.penUp(); } } // END main diff --git a/javacooksrc/javacooksrc/main/java/plotter/PlotterAWT.java b/javacooksrc/javacooksrc/main/java/plotter/PlotterAWT.java index f52953e..ddaa2f1 100644 --- a/javacooksrc/javacooksrc/main/java/plotter/PlotterAWT.java +++ b/javacooksrc/javacooksrc/main/java/plotter/PlotterAWT.java @@ -44,12 +44,17 @@ public void drawBox(int w, int h) { } public void rmoveTo(int incrx, int incry){ - moveTo(curx += incrx, cury += incry); + //the moveTo method will update the curx and cury to the abs coordinates, + //so we should not update curx and cury here + moveTo(curx + incrx, cury +incry); } public void moveTo(int absx, int absy){ - if (!penIsUp) + //everytime we draw a line we need to repaint to make sure the canvas is updated in every situation + if (!penIsUp) { g.drawLine(curx, cury, absx, absy); + p.repaint(); + } curx = absx; cury = absy; }