java - How do I color individual cells of a JTable based on the value in the cell? -


i attempting make tetris clone. game uses jtable representation of board. board 2d integer array.

i trying make so, when cell has value, cell change color. thought had working correctly, isn't working. i'd appreciate help.

thank you.

here code:

board:

import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.io.ioexception;  import javax.swing.timer; import javax.swing.table.*; /**  * @author _______________  *  * board.java  *   * board class gives information initializejtable class regarding   * data-type of jtable elements, how many rows/columns in jtable, etc;  *   * extended board abstracttablemodel, class allows me display data-type   * inside elements of jtable.  *   * class has method declarations required abstracttablemodel, these methods are:  *      - getrowcount()  *      - getcolumncount()  *      - getvalueat(int, int)  *   */  @suppresswarnings("serial") public class board extends abstracttablemodel {  tablecolorsetter tcs; boolean lost = false; initializegui igui; initializejtable ijt; public int speed = 1000; l l = new l(); public block currentblock = l; public int[][] boxes;//a 2d array of booleans defines status of tetris board. used initializejtable , initializepreviewjtable    /*  * boardtype integer works constructor.   *   * if boardtype equal 0, constructor generates 22x10 array (this used main board)  * however, if boardtype equal 1, constructor generates 4x4 array (this used preview/hold board, smaller board on side)  */  public board(int boardtype) {      if(boardtype == 0) {         boxes = new int[22][10];         loop();     } else if(boardtype == 1) boxes = new int[4][4]; }  /*  * getrowcount()  *   * getrowcount required declaration abstracttablemodel class.   * @return          amount of rows in boxes boolean  */ @override public int getrowcount() {     return boxes.length; }  public int gettetristype(block block) {     return block.gettetristype(); }  public void letpiecedown(int origa, int origb, int tetristype) {     boxes[origa][origb] = 0;     boxes[origa + 1][origb] = tetristype; }  /*  * getcolumncount()  *   * getcolumncount() required declaration abstracttablemodel class.   * @return          amount of columns in boxes boolean  */ @override public int getcolumncount() {     return boxes[0].length; }  @override public void setvalueat(object avalue, int rowindex, int columnindex) {     boxes[rowindex][columnindex] = (int)avalue;     firetablecellupdated(rowindex, columnindex); }  /*  * getvalueat()  *   * getvalueat() required declaration abstracttablemodel class.   * returns amount of columns in boxes boolean  *   * @param   int rowindex    row index of boolean want return.  *          int columnindex column index of boolean want return.  *   * @return      value of boolean @ specified location in boolean array.  */ @override public object getvalueat(int rowindex, int columnindex) {     return boxes[rowindex][columnindex]; }  public int getval(int row, int col) {     return boxes[row][col]; }  /**  *   * loop();  *   * loop() method rough draft in regards placing tetris objects on board , having them move, , stack.  *   * todo:   */  public void loop() {      timer timer = new              timer(speed, new actionlistener() {         int x = 0;         int y = 0;           @override           public void actionperformed(actionevent e) {             while(lost = false) {                 for(int = 0; < getcolumncount(); a++) {                     for(int b = 0; b < getrowcount(); b++) {                         tcs.gettablecellrenderercomponent(ijt.gettable(), "", false, false, b, a);                     }                 }              }             if(x == getrowcount() - 1) {                  x = 0;             } else if(boxes[x + 1][y] != 0 && x == 0) {                 boxes[x][y] = gettetristype(currentblock);                 firetabledatachanged();                 //system.out.println("game over!");                 //y++; //just testing.                 playsound sound = new playsound();                 sound.setsoundtype(1);                 sound.start();                 ((timer)e.getsource()).stop();             } else if(boxes[x + 1][y] != 0) {                 x = 0;             } else {             //system.out.println("pos: " + x + ", 0. row count: " + getrowcount() + " -- " + x);               letpiecedown(x, y, gettetristype(currentblock));               firetabledatachanged();               x++;             }           }       });       timer.start();               }   } 

initializejtable:

import javax.swing.table.defaulttablecellrenderer; import javax.swing.table.tablecellrenderer; import javax.swing.table.tablecolumnmodel;  import java.awt.color; import java.awt.component;  import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtable;  /**  * @author _________________  *  * initializejtable.java  *   * initializejtable class sets jtable tetris board.  *  * @todo: add more commenting  */  @suppresswarnings("serial") public class initializejtable extends jpanel {  private int cols = 10; private int height = 30; color white = color.white; color black = color.black; board tetrisboard; jtable table;  public board returnboard() {     return tetrisboard; }  public jtable gettable() {     return table; }  public initializejtable() { //the initializejtable constructor     tetrisboard = new board(0); //create new board of 22x10 booleans, boardtype of 0.     table = new jtable(tetrisboard); //apply information board constructor jtable. (information like, how many rows/cols in jtable, data-type jtable elements are, etc)     //setlayout(new gridbaglayout()); //sets layout of jtable gridbaglayout. means, makes jtable grid.     table.setcellselectionenabled(false); //disables dragging/selection of columns on jtable mouse. won't needing that.     tablecolumnmodel columns = table.getcolumnmodel(); //gets information columns of jtable. need set length of columns (to make them square)      table.setrowheight(height); //sets height of jtable rows height int, set 30. can changed.     //table.setforeground(white); //use make blocks white. still need make work correctly.     /*      * sadly, there isn't -- table.setcolumnheight(int) -- method in jtable api. (to set length of columns specified integer)      *       * have manually set width of each column. why declared tablecolumnmodel above      */     for(int = 0; < cols; a++) {         columns.getcolumn(a).setpreferredwidth(height); //sets height of jtable columns height int, set 30. can changed easily.     }       add(table);//the table's set correctly, add it.      } } 

there relationship between data/model , view/table. model maintains "what", view controls "how".

jtable provides means can behaviour of "how" (stuff gets rendered) through use of tablecellrenderers, these responsible determining how cell should "painted" based on value "what" model.

start having @ how use tables , using custom renderers

now, example uses double value determine distance 1 cell value is, represents color (black = 0; white = 1) cell should painted. accomplish this, uses custom tablecellrenderer converts value in model (the "what") color (or "how")

public class painttablecellrenderer extends defaulttablecellrenderer {      @override     public component gettablecellrenderercomponent(jtable table, object value, boolean isselected, boolean hasfocus, int row, int column) {         super.gettablecellrenderercomponent(table, "", isselected, hasfocus, row, column);         if (value instanceof double) {             double distance = (double) value;             int part = (int) (255 * distance);             color color = new color(part, part, part);             setbackground(color);         } else {             setbackground(color.white);         }         return this;     }  } 

smile

it demonstrates couple of other things might need know jtable well.

import java.awt.borderlayout; import java.awt.color; import java.awt.component; import java.awt.dimension; import java.awt.eventqueue; import java.util.enumeration; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtable; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.table.abstracttablemodel; import javax.swing.table.defaulttablecellrenderer; import javax.swing.table.tablecolumn;  public class smile {      public static void main(string[] args) {         new smile();     }      public smile() {         eventqueue.invokelater(new runnable() {             @override             public void run() {                 try {                     uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());                 } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) {                     ex.printstacktrace();                 }                  jframe frame = new jframe("testing");                 frame.setdefaultcloseoperation(jframe.exit_on_close);                 frame.add(new testpane());                 frame.pack();                 frame.setlocationrelativeto(null);                 frame.setvisible(true);             }         });     }      public class testpane extends jpanel {          private double[][] smily = {             {1, 1, 1, 1, 1, 0.996, 1, 0.843, 0.784, 0.788, 0.773, 0.769, 0.765, 0.765, 0.788, 0.784, 0.847, 1, 0.996, 1, 1, 1, 1, 0.996},             {1, 1, 1, 1, 1, 0.871, 0.733, 0.761, 0.847, 0.941, 0.941, 0.941, 0.941, 0.941, 0.933, 0.843, 0.761, 0.733, 0.871, 1, 1, 1, 1, 1},             {1, 1, 1, 1, 0.784, 0.733, 0.902, 0.941, 0.941, 0.941, 0.945, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.898, 0.733, 0.784, 1, 1, 1, 1},             {1, 1, 1, 0.765, 0.773, 0.945, 0.945, 0.941, 0.929, 0.937, 0.941, 0.941, 0.945, 0.941, 0.957, 0.91, 0.941, 0.941, 0.941, 0.78, 0.761, 1, 1, 1},             {1, 1, 0.808, 0.773, 0.941, 0.941, 0.941, 0.941, 0.294, 0.447, 0.941, 0.941, 0.941, 0.941, 0.702, 0.239, 0.886, 0.941, 0.945, 0.941, 0.78, 0.8, 1, 1},             {1, 0.89, 0.725, 0.945, 0.941, 0.941, 0.945, 0.843, 0, 0, 0.922, 0.945, 0.941, 0.941, 0.408, 0, 0.663, 0.941, 0.941, 0.945, 0.941, 0.725, 0.89, 1},             {0.992, 0.753, 0.902, 0.941, 0.945, 0.945, 0.941, 0.725, 0.051, 0, 0.808, 0.941, 0.941, 0.945, 0.294, 0.051, 0.553, 0.941, 0.941, 0.941, 0.941, 0.91, 0.741, 0.984},             {0.871, 0.78, 0.941, 0.941, 0.945, 0.945, 0.941, 0.694, 0.051, 0, 0.784, 0.945, 0.941, 0.941, 0.278, 0, 0.518, 0.941, 0.941, 0.945, 0.941, 0.941, 0.78, 0.878},             {0.816, 0.855, 0.941, 0.945, 0.945, 0.945, 0.941, 0.737, 0, 0.051, 0.82, 0.941, 0.941, 0.941, 0.302, 0.051, 0.565, 0.941, 0.949, 0.945, 0.941, 0.941, 0.863, 0.804},             {0.8, 0.945, 0.941, 0.945, 0.945, 0.941, 0.941, 0.875, 0, 0, 0.937, 0.941, 0.941, 0.941, 0.443, 0, 0.694, 0.945, 0.945, 0.945, 0.945, 0.949, 0.941, 0.765},             {0.769, 0.941, 0.945, 0.957, 0.961, 0.941, 0.945, 0.941, 0.443, 0.565, 0.945, 0.941, 0.941, 0.941, 0.769, 0.388, 0.918, 0.941, 0.941, 0.941, 0.945, 0.941, 0.941, 0.78},             {0.753, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.953, 0.941, 0.941, 0.941, 0.941, 0.945, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.788},             {0.741, 0.945, 0.839, 0.427, 0.624, 0.941, 0.941, 0.945, 0.941, 0.941, 0.941, 0.949, 0.945, 0.945, 0.941, 0.941, 0.941, 0.941, 0.941, 0.6, 0.376, 0.941, 0.945, 0.784},             {0.749, 0.941, 0.914, 0.345, 0.647, 0.941, 0.945, 0.949, 0.945, 0.945, 0.941, 0.941, 0.945, 0.941, 0.945, 0.945, 0.945, 0.945, 0.941, 0.702, 0.384, 0.941, 0.941, 0.78},             {0.796, 0.945, 0.941, 0.627, 0.592, 0.941, 0.941, 0.941, 0.945, 0.945, 0.945, 0.941, 0.949, 0.945, 0.941, 0.945, 0.945, 0.937, 0.945, 0.58, 0.631, 0.941, 0.937, 0.776},             {0.812, 0.859, 0.941, 0.855, 0.384, 0.957, 0.941, 0.945, 0.945, 0.945, 0.941, 0.953, 0.941, 0.945, 0.945, 0.945, 0.941, 0.941, 0.941, 0.384, 0.941, 0.941, 0.867, 0.812},             {0.871, 0.788, 0.941, 0.941, 0.533, 0.51, 0.941, 0.941, 0.941, 0.945, 0.949, 0.945, 0.945, 0.945, 0.941, 0.937, 0.941, 0.945, 0.522, 0.522, 0.941, 0.941, 0.792, 0.886},             {0.992, 0.761, 0.914, 0.941, 0.941, 0.325, 0.612, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.624, 0.318, 0.941, 0.945, 0.91, 0.765, 0.988},             {1, 0.882, 0.741, 0.941, 0.941, 0.922, 0.337, 0.475, 0.894, 0.941, 0.941, 0.941, 0.941, 0.941, 0.945, 0.894, 0.49, 0.325, 0.925, 0.941, 0.941, 0.753, 0.894, 1},             {1, 1, 0.796, 0.78, 0.941, 0.941, 0.941, 0.592, 0.447, 0.565, 0.667, 0.737, 0.737, 0.667, 0.565, 0.451, 0.588, 0.941, 0.941, 0.941, 0.796, 0.808, 1, 1},             {1, 1, 0.996, 0.753, 0.788, 0.941, 0.941, 0.941, 0.941, 0.702, 0.584, 0.557, 0.553, 0.592, 0.698, 0.906, 0.941, 0.945, 0.941, 0.796, 0.769, 0.996, 1, 1},             {1, 1, 1, 1, 0.769, 0.745, 0.922, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.918, 0.741, 0.776, 1, 1, 0.996, 1},             {1, 0.996, 1, 1, 1, 0.851, 0.733, 0.773, 0.867, 0.945, 0.941, 0.941, 0.941, 0.941, 0.945, 0.867, 0.769, 0.725, 0.851, 1, 1, 1, 1, 1},             {1, 1, 1, 1, 1, 1, 0.984, 0.843, 0.78, 0.761, 0.78, 0.796, 0.796, 0.784, 0.765, 0.78, 0.835, 0.984, 1, 1, 1, 1, 1, 1}         };          public testpane() {             asciitablemodel model = new asciitablemodel();             model.setdata(smily);              jtable table = new jtable(model);             table.setrowheight(24);             enumeration<tablecolumn> columns = table.getcolumnmodel().getcolumns();             while (columns.hasmoreelements()) {                 tablecolumn col = columns.nextelement();                 col.setwidth(24);                 col.setpreferredwidth(24);                 col.setminwidth(24);                 col.setmaxwidth(24);             }             table.setrowheight(24);             table.setautoresizemode(jtable.auto_resize_off);             table.setdefaultrenderer(object.class, new painttablecellrenderer());              setlayout(new borderlayout());             add(new jscrollpane(table));         }          @override         public dimension getpreferredsize() {             return new dimension(200, 200);         }      }      public class painttablecellrenderer extends defaulttablecellrenderer {          @override         public component gettablecellrenderercomponent(jtable table, object value, boolean isselected, boolean hasfocus, int row, int column) {             super.gettablecellrenderercomponent(table, "", isselected, hasfocus, row, column);             if (value instanceof double) {                 double distance = (double) value;                 int part = (int) (255 * distance);                 color color = new color(part, part, part);                 setbackground(color);             } else {                 setbackground(color.white);             }             return this;         }      }      public class asciitablemodel extends abstracttablemodel {          private double[][] data;          public asciitablemodel() {             data = new double[24][24];         }          public void setdata(double[][] value) {             data = value;             firetabledatachanged();         }          @override         public int getrowcount() {             return 24;         }          @override         public int getcolumncount() {             return 24;         }          @override         public object getvalueat(int rowindex, int columnindex) {             return data[rowindex][columnindex];         }      }  } 

i had intended provide "sad" face switch between, daughter wanted me go paint her, sorry ;)


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -