java - Repainting/Revalidating JPanel wont work -


i unclear on how repaint jpanel in existing code. dont understand how paintcomponent being called. firstly, clear jpanel cannot that.

i want able press "next move" button , repaint jpanel given change in grid reading colours from. having trouble changing jpanel @ via calls, revalidate(), removeall(), , repaint() tiles jpanel.

i call these , nothing happens existing jpanel below of tetris grid (tiles) (pictured)..

in below code have nested class extends jpanel , setup function creates window taking in 2d arraylist grid , constructing original grid.

how @ least clear jpanel or better redraw new grid... thinking method:

    public void redraw(grid g) {         removeall();                     getcolor(); //gets new grid of tiles                     repaint();         revalidate();     } 

inside nested jpanel class, doesnt seem change jpanel @ not clear current drawing.

tetris board reading in original grid

 public class boardgraphics {      public arraylist<arraylist<color>> colours;      private final int width;      private int height;      private jframe display;      private tetrissquares tiles;      private container c;      public boardgraphics(int width, int height,         arraylist<arraylist<integer>> grid) {         this.width = width;         this.height = height;         colours = new arraylist<arraylist<color>>(width);         setcolor(grid);         setup();     }      public void setup() {         system.out.println("let tetris begin");         display = new jframe("tetris agent");            final textarea welcome = new textarea("welcome tetris", 2, 10,                 textarea.scrollbars_none);          welcome.setbackground(color.black);         welcome.setforeground(color.red);         welcome.setfont(new font("monospaced", 0, 11));         welcome.seteditable(false);          button btnstart = new button("next move");         btnstart.setfocusable(false);            tiles = new tetrissquares(tetrisboard.board_width, height);          c = new container();         c.setlayout(new borderlayout());         c.add(welcome, borderlayout.north);         c.add(tiles);         c.add(btnstart,borderlayout.south);          btnstart.addactionlistener(new actionlistener()         {            public void actionperformed(actionevent e)            {              tiles.removeall();              tiles.revalidate();            }         });           final container main = new container();          main.setlayout(new gridlayout(1, 2));         display.add(c);            display.pack();          display.addwindowlistener(new windowadapter() {             public void windowclosing(windowevent e) {                 system.exit(0);             }         });          display.setvisible(true);     }      public arraylist<arraylist<color>> getboard() {         return colours;     }      public void setcolor(arraylist<arraylist<integer>> grid) {         listiterator<integer> li;          (int = 0; < width; i++) {             colours.add(new arraylist<color>());             li = grid.get(i).listiterator();             (int j = 0; j <= height; j++) {                 int n = 0;                 if(li.hasnext()) {                     n = li.next();                  }                 switch (n) {                 case 1:                     colours.get(i).add(color.red);                     break;                 case 2:                     colours.get(i).add(color.pink);                     break;                 case 3:                     colours.get(i).add(color.orange);                     break;                 case 4:                     colours.get(i).add(color.yellow);                     break;                 case 5:                     colours.get(i).add(color.green);                     break;                 case 6:                     colours.get(i).add(color.cyan);                     break;                 case 7:                     colours.get(i).add(color.blue);                     break;                 default:                     colours.get(i).add(color.gray);                     break;                 }             }         }     }        public class tetrissquares extends jpanel {          public int width;         public int height;          public tetrissquares(int width, int height) {             this.width = width;             this.height = width;             this.setpreferredsize(new dimension(width * 20, height * 20));         }          public void redraw() {             removeall();             //add elements             //revalidate();             //repaint();         }           public void paintcomponent(graphics g) {              super.paintcomponent(g);              graphics2d graphics = (graphics2d) g;             graphics.setcolor(color.black);             /*              * tiles.width = getsize().width / width; tiles.height =              * getsize().height / 800;              *               * insets.left = (getsize().width - width * tiles.width) / 2;              * insets.right = insets.left; insets.top = 0; insets.bottom =              * getsize().height - height * tiles.height;              */             dimension size = getsize();             insets insets = getinsets();              int w = size.width - insets.left - insets.right;             int h = size.height - insets.top - insets.bottom;              int tilewidth = w / width;             int tileheight = w / width;              listiterator<color> li;              (int = 0; < width; i++) {                 li = colours.get(i).listiterator();                 int n = 20;                  while(li.hasnext()) {                     --n;                     int x = (int) (i * tilewidth);                     int y = (int) (n * tileheight);                     graphics.setcolor(li.next());                     graphics.fillrect(x, y, tilewidth, tileheight);                     graphics.setcolor(color.black);                     graphics.drawrect(x, y, tilewidth, tileheight);                 }             }          }      }  } 

start having read through painting in awt , swing gain better understanding of painting system.

removeall removes child components container, isn't going in situation...

revalidate deals layout subsystem , updating container hierarchy in response changes in container contents, again, not going help...

in paintcomponent method, referencing arraylist called colours, used paint squares. need reset list stop paintcomponent method filling in colors

your redaw method should more like...

public void redraw() {     colours.clear();     repaint(); } 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -