Building a Grid on the screen

  ... the case you feel the need for a new feature or want to submit one.

Building a Grid on the screen

Postby lukaum » Sat Mar 24, 2012 5:48 pm

Hi everyone!

I'm a beginner in AndEngine developing, but I want to share this function that I made, to build a Grid on the screen. :D

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public Line buildGrid(int pWidth, int pHeight, float pRed, float pGreen, float pBlue){
  2.         Line grid = new Line(0, 0, 0, pHeight);
  3.         grid.setColor(0.5f, 0.5f, 0.5f);
  4.         int cont = 0;
  5.        
  6.         while(cont < pWidth){
  7.                 cont += 10;
  8.                 grid.attachChild(new Line(cont, 0, cont, pHeight));
  9.                 grid.getLastChild().setColor(pRed, pGreen, pBlue);                     
  10.         }
  11.        
  12.         cont = 0;
  13.         while (cont < pHeight){
  14.                 cont += 10;
  15.                 grid.attachChild(new Line(0, cont, pWidth, cont));
  16.                 grid.getLastChild().setColor(pRed, pGreen, pBlue);
  17.         }
  18.        
  19.         return grid;
  20. }
  21.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


The pWidth and pHeight params indicates the size of the Grid (the entire screen, for example), and the other params indicates the color of the lines.
You can use this just attaching the result to the scene.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. scene.attachChild(buildGrid(CAMERA_WIDTH, CAMERA_HEIGHT, 0.9f, 0.9f, 0.9f));
  2.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Enjoy!
lukaum
 
Posts: 1
Joined: Fri Jan 06, 2012 2:56 pm

Re: Building a Grid on the screen

Postby redcloud » Mon Sep 24, 2012 10:25 am

What about extending this code to build a Board component? Something like this...
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Board extends Rectangle {
  2.  
  3.         private boolean drawBorders;
  4.         private int COLS = 8;
  5.         private int ROWS = COLS;
  6.        
  7.         public Grid(float pX, float pY, float pWidth, float pHeight, IRectangleVertexBufferObject pRectangleVertexBufferObject) {
  8.                 super(pX, pY, pWidth, pHeight, pRectangleVertexBufferObject);
  9.                 draw();
  10.         }
  11.  
  12.         public Grid(float pX, float pY, float pWidth, float pHeight, int cols, int rows, IRectangleVertexBufferObject pRectangleVertexBufferObject) {
  13.                 super(pX, pY, pWidth, pHeight, pRectangleVertexBufferObject);
  14.                
  15.                 COLS = cols;
  16.                 ROWS = rows;
  17.                
  18.                 draw();
  19.         }
  20.  
  21.         public void drawBorders(boolean drawBorders) {
  22.                
  23.         }
  24.        
  25.         public void setDefaultBackground(Color color) {
  26.                
  27.         }
  28.        
  29.         public void setDefaultBackground(Image image) {
  30.                
  31.         }
  32.        
  33.         public void setCellBackground(int x, int y, Color color) {
  34.                
  35.         }
  36.        
  37.         public void setCellBackground(int x, int y, Image image) {
  38.                
  39.         }
  40.        
  41.         public void setCellBackgroundText(int x, int y, Text text) {
  42.                
  43.         }
  44.        
  45.         public void setCellForeground(int x, int y, Color color) {
  46.                
  47.         }
  48.        
  49.         public void setCellForeground(int x, int y, Image image) {
  50.                
  51.         }
  52.        
  53.         public void setCellForeground(int x, int y, Text text) {
  54.                
  55.         }
  56.        
  57.         public void setGridLinesWidth(int lineWidth) {
  58.                
  59.         }
  60.        
  61.         private void draw() {
  62.                 if (drawBorders) {
  63.                        
  64.                 }
  65.         }
  66.        
  67. }
  68.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
redcloud
 
Posts: 10
Joined: Sun Sep 23, 2012 6:29 pm


Return to Features

Who is online

Users browsing this forum: No registered users and 10 guests