We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Grid, cells don't line up correctly
Page Index Toggle Pages: 1
Grid, cells don't line up correctly (Read 424 times)
Grid, cells don't line up correctly
Apr 30th, 2009, 1:31am
 
Hi Everyone

I am trying to make a simple grid class. Currently is is a grid with the following
   - 400x400
   - 15 columns, 22 rows
   - Each cell is green with 100 transpency.

I am using eclipse. I am not using the processing IDE.

I have the cells being drawn, but when at the settings above, they do not line up correctly. This is shown by the bright green lines (i'm assuming this when when the cell overlaps, cancelling the transparency).

These are my two classes.

RenderableGrid.java

Code:
package net.dlangford.processing;
import processing.core.PApplet;

public class RenderableGrid {
private PApplet applet;

protected static final int defaultNumberOfColumns = 10;
protected static final int defaultNumberOfRows = 10;
protected static final double defaultWidth = 100;
protected static final double defaultHeight = 100;

protected int numberOfColumns;
protected int numberOfRows;
protected double width;
protected double height;
protected RenderableCell[][] cells;


public RenderableGrid(int numberOfColumns, int numberOfRows, double width, double height, PApplet appletHolder)
{

// validate data
if (width == 0 || height == 0)
throw new IllegalArgumentException("When creating a Grid object the width and/or height can not be 0");

// Set values
this.numberOfColumns = numberOfColumns;
this.numberOfRows = numberOfRows;
this.width = width;
this.height = height;

if (appletHolder == null)
throw new IllegalArgumentException("RenderableGrid must be created with non-null AppletHolder");

this.applet = appletHolder;
//addAppletHolderToCells();

createCells();
}

// Create the cells needed in this grid
public void createCells() {

// need cell width and height;
double cellWidth = width/numberOfColumns;
double cellHeight = height/numberOfRows;

cells = new RenderableCell[numberOfColumns][numberOfRows];

for (int x=0; x<numberOfColumns; x++)
{
for (int y=0; y<numberOfRows; y++)
{
cells[x][y] = new RenderableCell(x*cellWidth, y*cellHeight, cellWidth, cellHeight);
cells[x][y].setRenderingDestination(applet);
}
}

environmentCheck();
}

public void renderGrid() {

for (int x=0; x<numberOfColumns; x++)
{
for (int y=0; y<numberOfRows; y++)
{
cells[x][y].renderCell();
}
}
}

protected void environmentCheck() {
// Check there are cells to render
if (cells == null)
throw new IllegalStateException("No cells to render. Please use createCells() first");

// Check columns
if (cells.length != numberOfColumns)
throw new IllegalStateException("cells.length (" + cells.length + ")"
+ " does not equal numberOfColumns (" + numberOfColumns + ")");
if (numberOfColumns < 1)
throw new IllegalStateException("number of columns (" + numberOfColumns + ") cannot be less than 1");

// Check rows
if (cells[0].length != numberOfRows)
throw new IllegalStateException("cells[0].length (" + cells[0].length + ")"
+ " does not equal numberOfRows (" + numberOfRows + ")");
if (numberOfRows < 1)
throw new IllegalStateException("number of rows (" + numberOfRows + ") cannot be less than 1");
}

public String toString() {
int numberOfCells;
if (cells != null)
numberOfCells = cells.length*cells[0].length;
else
numberOfCells = 0;

return "Grid instance. "
+ "Columns: " + numberOfColumns
+ "\tRows: " + numberOfRows
+ "\tWidth: " + width
+ "\tHeight: " + height
+ "\tNumber of Cells: " + numberOfCells;
}


}


and

RenerableCell.java

Code:
package net.dlangford.processing;
import processing.core.*;

public class RenderableCell {
private PApplet applet;
protected double x;
protected double y;
protected double width;
protected double height;

public RenderableCell(double x, double y, double width, double height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}

public void renderCell() {
applet.fill(0, 255, 0, 100);
applet.rect((float) x,(float)  y,(float)  width,(float)  height);
}

public void setRenderingDestination(PApplet appletHolder) { this.applet = appletHolder; }
public PApplet getRenderingDestination() { return applet; }
public void setX(double x) { this.x = x;}
public double getX() {return x; }
public void setY(double y) { this.y = y; }
public double getY() { return y; }
public void setWidth(double width) { this.width = width; }
public double getWidth() { return width; }
public void setHeight(double height) { this.height = height; }
public double getHeight() { return height; }
}


Is it something in my code, or is it something in the processing library that can't handle such accurate positiong?

If no one else gets this problem I have an image, but i couldn't see the upload botton Sad

This is driving me insane and any help would be fantastic!

Regards


Re: Grid, cells don't line up correctly
Reply #1 - Apr 30th, 2009, 4:18am
 
First thought, I think you can remove the stroke from the cell and draw the grid itself with line() calls. Might not fit your needs, can't judge by the current code.
Page Index Toggle Pages: 1