Ansichten: QuotePaste - CodePaste - NoPaste
Codesnippet eingetragen am 1.1.2012 um 17:48
Von: Michael
Sprache: Java
Beschreibung: Mock-Up of a Tetris Board, field strukture is based off an array :)
CodeSnippet:
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. import javax.microedition.lcdui.*;
  6. /**
  7.  *
  8.  * @author mirrakor
  9.  */
  10. class GCanvas extends Canvas{
  11.  
  12. public GCanvas() {
  13. super();
  14. }
  15.  
  16. public void paint(Graphics g){
  17. int height = getHeight();
  18. int width = getWidth();
  19. int sidepanelwidth=0;
  20. int cellwidth=0;
  21. int cellheight=0;
  22.  
  23. final int NUM_OF_ROWS = 20;
  24. final int NUM_OF_COLS = 10;
  25. int field[][] = new int[][] { {1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0},{0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0},{0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0},{0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0},{0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0},{0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1} }; // [X][Y] [10][20]
  26.  
  27. // For Regular phones, this works :)
  28. sidepanelwidth = width - (height/2);
  29. cellwidth = (width-sidepanelwidth)/NUM_OF_COLS;
  30. cellheight = height/NUM_OF_ROWS;
  31.  
  32. g.setColor(0xFFFFFF); // WHITE
  33. g.fillRect(0, 0, width, height); // clear the screen with WHITE
  34.  
  35.  
  36. // for the start we only color the pieces in one color (later we can simply change this behaviour by assinging different numbers in the array, IMPORTANT IS, that 0 is NO BLOCK!
  37. g.setColor(255, 0, 0);
  38. // testdraw our array
  39. for(int i=0; i<NUM_OF_COLS; i++){ // for better readability we leave the 10, although it only goes from 0-9!
  40. for(int j=0; j<NUM_OF_ROWS; j++){
  41. if(field[i][j]==1){
  42. g.fillRect(i*cellwidth, j*cellheight, cellwidth, cellheight);
  43. }
  44. }
  45. }
  46.  
  47.  
  48. // grey bottom block
  49. g.setColor(0x8D8D8D);
  50. g.fillRect(0, NUM_OF_ROWS*cellheight, NUM_OF_COLS*cellwidth, height-(NUM_OF_ROWS*cellheight));
  51.  
  52. g.setColor(0x000000); // BLACK
  53. // draw vertical lines (Senkrecht :D)
  54. for(int i=1; i<=NUM_OF_COLS; i++){
  55. g.drawLine(i*cellwidth, 0, i*cellwidth, height);
  56. }
  57.  
  58. // draw horizontal lines
  59. for(int i=1; i<=NUM_OF_ROWS; i++){
  60. g.drawLine(0, i*cellheight, NUM_OF_COLS*cellwidth, i*cellheight);
  61. }
  62.  
  63. g.drawString(\"Height: \"+height+\" Width: \"+width, 50, 50, 0);
  64. }
  65. }
  66.