java - Why won't gc.clearRect clear the canvas? -


i have code in mazeui class creates couple of fields in javafx entering height , width of maze generated along button , event button. on clicking generate maze code should generate random maze.

now works fine. line clear canvas after maze first drawn using gc.clearrect(x, y, z, a) doesn't appear work , maze's re-drawn on top of 1 on subsequent clicks:

package dojo.maze.ui;  import dojo.maze.generator.mazegenerator; import javafx.application.application; import javafx.event.actionevent; import javafx.event.eventhandler; import javafx.geometry.insets; import javafx.scene.group; import javafx.scene.scene; import javafx.scene.canvas.canvas; import javafx.scene.canvas.graphicscontext; import javafx.scene.control.button; import javafx.scene.control.label; import javafx.scene.control.textfield; import javafx.scene.effect.boxblur; import javafx.scene.layout.gridpane; import javafx.scene.layout.hbox; import javafx.scene.layout.vbox; import javafx.scene.paint.color; import javafx.scene.shape.strokelinecap; import javafx.scene.shape.strokelinejoin; import javafx.stage.stage;  public class mazeui extends application {  private mazegenerator generator; private integer height = 15; private integer width = 15;  @override public void start(stage primarystage) throws exception {     primarystage.settitle("dojo: solving maze");      group root = new group();      drawmazeview(root);      primarystage.setscene(new scene(root, color.white));     primarystage.show(); }   private graphicscontext initialisegraphicscontext(canvas canvas) {     graphicscontext gc = canvas.getgraphicscontext2d();     gc.setfill(color.black);     gc.fillrect(0,0,600,600);     gc.setstroke(color.green);     gc.setlinecap( strokelinecap.round );     gc.setlinejoin( strokelinejoin.round );     gc.setlinewidth(3);      boxblur blur = new boxblur();     blur.setwidth(1);     blur.setheight(1);     blur.setiterations(1);     gc.seteffect(blur);     return gc; }  private void drawmazeview(group root) {     canvas canvas = new canvas(800, 800);     graphicscontext gc = initialisegraphicscontext(canvas);      gridpane.setconstraints(canvas, 0, 6);      gridpane grid = new gridpane();     grid.setpadding(new insets(10, 10, 10, 10));     grid.setvgap(5);     grid.sethgap(5);      label heightlabel = new label("height:");     final textfield heighttextfield = new textfield();     hbox hbheight = new hbox();     hbheight.getchildren().addall(heightlabel, heighttextfield);     hbheight.setspacing(10);      gridpane.setconstraints(hbheight, 0, 0);      label widthlabel = new label("label:");     final textfield widthtextfield = new textfield();     hbox hbwidth = new hbox();     hbwidth.getchildren().addall(widthlabel, widthtextfield);     hbwidth.setspacing(10);      gridpane.setconstraints(hbwidth, 0, 2);      vbox fieldsbox = new vbox();     fieldsbox.getchildren().addall(hbheight, hbwidth);      // create button allows generate new maze     button btn = new button();     btn.settext("generate random maze");     btn.setonaction(new eventhandler<actionevent>() {          @override         public void handle(actionevent event) {             system.out.println("button clicked!");             height = integer.valueof(heighttextfield.gettext());             width = integer.valueof(widthtextfield.gettext());              // clear old maze             gc.clearrect(0, 0,                     canvas.getheight(),                     canvas.getwidth());              generator = new mazegenerator(height, width);              drawmaze(gc);         }     });      gridpane.setconstraints(btn, 0, 4);      grid.getchildren().addall(fieldsbox, btn, canvas);     root.getchildren().add(grid); }  void drawmaze(graphicscontext gc) {     int[][] maze = generator.maze();     int xpos = 20,         ypos = 20,         length = 40,         gap = 10;      (int = 0; < width; i++) {         xpos = 20;         // draw north edge         (int j = 0; j < height; j++) {             if ((maze[j][i] & 1) == 0) {                 gc.strokeline(xpos, ypos, xpos + length, ypos); // horizontal             }             xpos += length + gap;              system.out.print((maze[j][i] & 1) == 0 ? "+---" : "+   ");         }         system.out.println("+");          xpos = 20;         // draw west edge         (int j = 0; j < height; j++) {             if ((maze[j][i] & 8) == 0) {                 gc.strokeline(xpos, ypos, xpos, ypos + length); // vertical             }             xpos += length + gap;              system.out.print((maze[j][i] & 8) == 0 ? "|   " : "    ");         }         gc.strokeline(xpos, ypos, xpos, ypos + length); // vertical         system.out.println("|");         ypos += length + gap;     }     // draw bottom line      xpos = 20; // reset x pos western edge      (int j = 0; j < height; j++) {         gc.strokeline(xpos, ypos, xpos + length, ypos); // horizontal         system.out.print("+---");         xpos += length + gap;     }     system.out.println("+"); }  public static void main(string[] args) {     launch(args); } } 

code mazegenerator:

package dojo.maze.generator;  import java.util.arrays; import java.util.collections;  /*  * recursive backtracking algorithm  * shamelessly borrowed ruby @  * http://weblog.jamisbuck.org/2010/12/27/maze-generation-recursive-backtracking */ public class mazegenerator {  private final int x; private final int y; private final int[][] maze;  public static final int[][] mazeproblemone = new int[][] {{2,5,2,3,7,3,1,6,7,3,3,5,6,3,5},{4,10,3,5,12,6,3,9,10,3,5,12,8,6,13},{12,6,5,12,12,14,3,1,6,3,9,10,3,9,12},{12,12,12,12,12,12,6,3,9,4,6,7,1,6,9},{14,9,10,9,12,12,12,6,5,12,12,10,5,12,4},{12,2,7,5,12,12,10,9,12,10,13,4,10,9,12},{12,6,9,12,12,12,2,5,10,5,10,11,3,3,13},{12,10,5,12,10,11,3,13,4,10,5,6,3,1,12},{12,6,9,10,5,4,6,9,12,6,9,12,6,3,9},{10,9,6,1,12,10,11,3,9,12,6,13,12,2,5},{6,7,9,6,9,6,3,3,3,9,8,12,12,6,13},{8,12,6,9,6,13,6,3,7,3,3,13,12,12,12},{6,9,10,5,12,12,12,4,10,3,5,8,12,12,8},{14,3,5,12,8,12,10,11,3,5,10,5,12,10,5},{10,1,10,11,3,9,2,3,3,11,1,10,11,3,9}}; public static final int[][] mazeproblemtwo = new int[][] {{2,5,6,5,6,3,1,6,3,3,7,5,2,3,5},{4,10,9,12,14,3,3,9,6,5,12,10,3,3,9},{14,3,3,9,8,6,3,5,12,12,10,3,3,3,5},{12,6,3,3,5,12,4,10,9,10,3,3,3,3,13},{12,12,6,5,12,12,10,3,3,3,5,6,3,3,9},{12,10,9,12,10,9,6,5,6,3,13,10,3,3,5},{10,3,5,12,6,5,12,10,9,4,14,3,3,1,12},{4,6,9,12,12,10,9,6,3,11,13,6,3,3,9},{12,12,2,13,14,3,5,8,6,5,8,10,3,3,5},{12,10,3,9,12,4,12,6,9,12,6,5,6,3,9},{14,7,3,1,10,13,12,12,4,12,12,10,9,6,1},{12,10,3,7,1,12,10,11,9,12,12,2,3,11,5},{12,2,5,12,6,9,2,5,6,9,10,3,3,5,12},{10,5,12,12,12,6,3,13,12,6,7,1,6,9,12},{2,11,9,10,11,9,2,9,10,9,10,3,11,3,9}}; public static final int[][] mazeproblemthree = new int[][] {{2,5,2,3,7,3,3,3,5,6,3,1,6,7,1},{4,10,3,5,12,2,5,6,9,10,3,3,9,14,5},{10,7,5,12,10,5,14,9,6,5,6,3,5,12,12},{6,9,8,10,3,9,12,6,13,12,10,5,12,12,12},{12,6,3,7,1,6,9,12,12,10,3,9,12,8,12},{12,12,4,10,5,10,5,8,14,3,5,2,11,3,13},{12,10,13,4,10,5,10,5,10,5,10,5,6,5,12},{14,1,12,10,5,14,1,12,6,9,4,10,9,12,12},{14,5,10,5,12,10,5,12,12,2,15,3,1,12,12},{8,12,6,9,10,5,12,12,10,3,9,6,3,9,12},{6,9,14,3,3,9,12,10,3,3,3,9,4,6,9},{10,5,12,6,3,3,13,6,3,3,1,6,11,9,4},{6,9,12,10,5,6,11,9,6,3,3,9,6,3,13},{14,5,12,4,12,10,1,6,9,6,5,2,13,4,12},{8,10,11,9,10,3,3,11,3,9,10,3,9,10,9}}; public static final int[][] mazeproblemfour = new int[][] {{2,3,3,5,4,6,7,3,3,5,6,5,6,3,5},{6,3,1,12,12,12,10,3,5,10,9,12,10,5,12},{12,6,3,9,14,9,4,6,9,2,3,11,1,12,12},{14,9,2,3,11,5,12,10,3,5,6,3,3,9,12},{10,5,6,5,2,11,11,5,4,12,12,6,7,1,12},{4,10,9,10,3,5,6,9,12,10,9,8,12,6,13},{14,5,4,6,3,9,12,6,11,5,6,3,9,12,8},{12,10,9,12,4,6,9,10,5,12,14,3,5,10,5},{12,6,5,12,12,12,6,3,9,12,8,6,13,4,12},{14,9,10,9,12,12,10,3,5,10,5,12,10,13,12},{12,2,7,5,10,11,3,3,9,6,9,12,2,9,12},{10,3,9,10,3,3,5,4,6,11,3,9,6,3,13},{6,5,6,7,3,5,12,10,9,6,3,3,9,6,9},{12,10,9,10,5,8,12,6,5,10,5,6,5,10,5},{10,3,3,1,10,3,11,9,10,3,9,8,10,3,9}}; public static final int[][] mazeproblemfive = new int[][] {{2,3,5,6,3,3,7,5,6,3,3,3,7,3,5},{6,5,12,12,6,3,9,8,10,5,4,6,9,4,12},{12,12,12,12,10,3,5,6,5,10,13,10,5,14,9},{12,10,9,10,5,4,10,9,14,1,12,4,12,10,5},{10,5,6,5,10,15,3,1,14,5,14,9,10,5,8},{6,9,12,10,5,8,6,5,8,10,9,6,5,14,5},{10,3,9,4,12,6,9,10,3,3,3,9,12,8,12},{6,3,7,13,12,10,7,5,2,7,7,1,10,3,13},{14,1,12,8,10,5,12,12,6,9,12,6,3,3,9},{8,6,13,6,3,9,12,12,12,2,13,12,6,3,5},{6,9,8,12,4,6,9,12,14,5,8,10,9,4,12},{12,6,3,9,10,9,6,9,8,10,7,5,6,11,9},{12,10,3,3,3,5,14,3,3,5,12,12,10,5,4},{14,1,6,5,6,9,10,3,1,12,12,10,1,10,13},{10,3,9,10,11,3,3,3,3,9,10,3,3,3,9}};  public mazegenerator(int x, int y) {     this.x = x;     this.y = y;     maze = new int[this.x][this.y];     generatemaze(0, 0); }  public void display() {     (int = 0; < y; i++) {         // draw bbc.north edge         (int j = 0; j < x; j++) {             system.out.print((maze[j][i] & 1) == 0 ? "+---" : "+   ");         }         system.out.println("+");         // draw west edge         (int j = 0; j < x; j++) {             system.out.print((maze[j][i] & 8) == 0 ? "|   " : "    ");         }         system.out.println("|");     }     // draw bottom line     (int j = 0; j < x; j++) {         system.out.print("+---");     }     system.out.println("+"); }  private void generatemaze(int cx, int cy) {     dir[] dirs = dir.values();     collections.shuffle(arrays.aslist(dirs));     (dir dir : dirs) {         int nx = cx + dir.dx;         int ny = cy + dir.dy;         if (between(nx, x) && between(ny, y)                 && (maze[nx][ny] == 0)) {             maze[cx][cy] |= dir.bit;             maze[nx][ny] |= dir.opposite.bit;             generatemaze(nx, ny);         }     } }  private static boolean between(int v, int upper) {     return (v >= 0) && (v < upper); }  public int[][] maze() {     return maze; }  private enum dir {     n(1, 0, -1), s(2, 0, 1), e(4, 1, 0), w(8, -1, 0);     private final int bit;     private final int dx;     private final int dy;     private dir opposite;      // use static initializer resolve forward references     static {         n.opposite = s;         s.opposite = n;         e.opposite = w;         w.opposite = e;     }      private dir(int bit, int dx, int dy) {         this.bit = bit;         this.dx = dx;         this.dy = dy;     } };  public static void main(string[] args) {     int x = args.length >= 1 ? (integer.parseint(args[0])) : 8;     int y = args.length == 2 ? (integer.parseint(args[1])) : 8;     mazegenerator maze = new mazegenerator(x, y);     maze.display(); } } 

how fix code correctly clears maze after each button click? missing something?

remove boxblur effect before clear canvas, re-apply it. clearing canvas paints transparent color. boxblur effect affect too.

and jewelsea said. you'd have found if you'd reduced code minimum ;-)


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -