Java Multi-Dimentional Arrays. Null Pointer - java

Sorry if I'm not explaining good for it's 12:34 am and i'm doing late night programming but I need help. Btw this is in LWJGL Here's my Code:
I keep getting a null pointer error for the addAt() and the draw(); Basicly there is a couple classes that make it so when I click it will run addAt(mousex,mousey); and in the render loop it will keep drawing. The class that is new Block(x,y) is a class that will draw the QUAD.
//beggining
public class Grid {
Block[][] blocks = new Block[25][25];
public Grid(){
for (int x = 0; x < 25 - 1; x++) {
for (int y = 0; y < 16 - 1; y++) {
blocks[x][y] = new Block(x,y);
}
}
}
public void draw(){
for (int x = 0; x < 25;x++){
for (int y = 0; y < 25;y++){
blocks[x][y].draw();
}
}
}
public void addAt(int x,int y){
blocks[x][y] = new Block(x,y);
}
}
//end
basicly the Main is just making a Display and running the draw loop and the input listener.
Then the Block class is just making a quad at the defined x and y.
Sorry if I disobeyed a stack overflow rule. This is my First post and it's at a late time.:) Thanks in advance!

While adding to block array your looping is from 0 to (25-1) and (16-1). While processing the block the looping is from 0 to 25. That would most probably lead to NPE. Try initializing you blocks from 0 to 25 (for both x and y values).

I tried your code and it works fine (I don't get any exception). Here is my code:
public class Test {
private class Block {
int x, y;
private Block(int x, int y) {
this.x = x;
this.y = y;
}
void draw() {
}
}
Block[][] blocks = new Block[25][25];
public Test(){
for (int x = 0; x < 25 - 1; x++) {
for (int y = 0; y < 16 - 1; y++) {
blocks[x][y] = new Block(x,y);
}
}
}
public void draw(){
for (int x = 0; x < 25;x++){
for (int y = 0; y < 25;y++){
blocks[x][y].draw();
}
}
}
public void addAt(int x,int y){
blocks[x][y] = new Block(x,y);
}
public static void main(String[] args) {
Test t = new Test();
t.addAt(4,5);
}
}
=> The problem seems to be the late night programming ;-)

Related

How do I loop through all the pixels in an image starting from the bottom?

I'm trying to create a program that will detect a blue line and black out all of the pixels above it, but not below. See method below:
public static void findBlueLine(Picture p) {
Pixel[][] pixels = p.getPixels2D();
for(int y = 0; y < p.getHeight(); y++) {
for (int x = 0; x < p.getWidth(); x++) {
if (isBlueLine(pixels[y][x])) {
pixels[y][x].setColor(Color.BLACK);
//for(int m = y; m < p.getHeight(); m++) {
//pixels[m][x].setColor(Color.BLACK);
//}
}
}
}
}
I am making the assumption that you have one blue line on your screen. This blue line can be at any angle and start and end at any coordinates. I am assuming the line does not exist off of the screen.
This code should work for all lines.
public static void findBlueLine(Picture p) {
Pixel[][] pixels = p.getPixels2D();
Bool aboveLine = false;
for (int x = 0; x < p.getWidth(); x++) {
aboveLine = false;
for(int y = p.getHeight-1; 0 <= y ; y--) {
if(aboveLine == true) {
pixels[y][x].setColor(Color.BLACK);
}
if (isBlueLine(pixels[y][x])) {
aboveLine = true;
}
}
}
}

Bouncing Ball (in class) - need help coding array

I'm all new to this and doing a beginner's lecture on Java (with Processing). Our assignment this time is the bouncing ball. I've (sort of) successfully gotten it to move the way it's supposed to and put it into a class, but I can't get the array right.
Here's the version using the class:
class Ball {
float x;
float y;
float ySpeed;
float gravity;
int counter = 0;
Ball()
{
x = 300;
y = 300;
ySpeed = 2.5;
gravity = 0.2;
}
void move()
{
y = y + ySpeed;
ySpeed = ySpeed + gravity;
if (y > height-25 )
{ySpeed = ySpeed * -0.85;
y = height-25;
counter++;
}
println(counter);
if(counter >= 17)
{ySpeed=0;
y=height-25;}
}
void display()
{
ellipse(x,y,50,50);
}
}
//using the class:
Ball b1;
void settings()
{
size(800,600);
}
void setup()
{
b1 = new Ball();
}
void draw()
{
background(0);
b1.move();
b1.display();
}
Here's what I ended up with after messing it up completely.
//class Ball
class Ball {
float[] x;
float[] y;
float[] ySpeed;
float[] gravity;
int i;
int counter = 0;
//constructor
Ball()
{
x[i] = 300;
y[i] = 300;
ySpeed[i] = 2.5;
gravity[i] = 0.2;
}
void move()
{
y[i] = y[i] + ySpeed[i];
ySpeed[i] = ySpeed[i] + gravity[i];
//changes direction; (-25) to avoid movement beyong boundary
if (y[i] > height-25 )
{ySpeed[i] = ySpeed[i] * -0.85;
y[i] = height-25;
}
println(counter);
if(counter >= 17)
{ySpeed[i]=0;
y[i]=height-25;}
}
void display()
{
//draw ellipse
ellipse(x[i],y[i],50,50);
}
}
//using the class
final int i = 9;
Ball[] Balle = new Ball[10];
void settings()
{
size(800,600);
}
void setup()
{
Balle[i] = new Ball();
for (int i = 0; i < Balle.length; i++)
{Balle[i] = new Ball(x[i],y[i],50,50), i*4);
}
}
I suppose this looks weird because it's picked together from several different help pages... the current error is "variable x does not exist" on
{Balle[i] = new Ball(x[i],y[i],50,50), i*4);
I'm aware there are several other problems.
Right now I'm quite lost in figuring out how it works. Could somebody give me a hint?
variable x does not exist - It's because you don't have x as a global variable, but as a class variable, which is therefore only accessible from within the Ball class or by an instance of the Ball class. I don't exactly know why you're using arrays for x and y, primitives would do just fine. And don't forget to add the draw() method, as in processing, that serves as the 'main loop', it gets executed every 0.x sec (frameRate) so you MUST use that as well. Your other functions like display() etc.. can also be called from within the draw function.
Geez, what did I even do there?
Here it is:
Ball[] balls = new Ball[10];
void settings()
{
size(600,800);
}
void setup()
{
for (int i = 0; i < balls.length; i++)
{
balls[i] = new Ball();
}
}
void draw()
{ background(0);
for (int i = 0; i < balls.length; i++)
{
balls[i].move();
balls[i].display();
}
}
Is programming like that, stumbling around in the dark until you get hit by a brick...?
Here's a pseudo code.
If ball <= bottom, reverse the speed of ball.
(That means when it hits the floor, the speed reverses. Multiply it with -1)
Other wise, speedOfBall = speed - (gravity*time);
You can easily calculate the displacement in terms of coordinates as s = velocity*time + .5*gravity*time^2.

Java - printing a 2D array for Conways Game of Life

I started today to program Conways Game of Life. In a first step, I just want the user to input the length of the (squadratic) field which is then displayed on the screen. But I'm getting a NullPointerException in the printGrid() method. Here are the necessary code examples:
public class Grid {
private Cell[][]grid;
public Grid (int feldlänge) {
grid = new Cell[feldlänge][feldlänge];
int x, y;
for (y = 0; y < feldlänge; y = y + 1) {
for (x = 0; x < feldlänge; x = x + 1) {
Cell cell;
cell = new Cell(x,y);
cell.setLife(false);
} // for
} // for
} // Konstruktor Grid
public String printGrid () {
String ausgabe = "";
int x, y;
for (y = 0; y < grid.length; y = y + 1) {
for (x = 0; x < grid.length; x = x + 1) {
if (grid[x][y].isAlive()) { // Here's the NullPointerException
ausgabe = ausgabe + "■";
}
if (!grid[x][y].isAlive()) {
ausgabe = ausgabe + "□";
}
}
ausgabe = ausgabe + "\n";
}
return ausgabe;
}
public class Cell {
private int x, y;
private boolean isAlive;
public Cell (int pX, int pY) {
x = pX;
y = pY;
} // Konstruktor Cell
public void setLife (boolean pLife) {
isAlive = pLife;
} // Methode setLife
public int getX () {
return x;
} // Methode getX
public int getY () {
return y;
} // Methode getY
public boolean isAlive () {
return isAlive;
}
}
It's kind of embarrassing I can't find the mistake by myself. I guess I'm overlooking something simple.
Already thanks a lot for any help!
Update: Already solved!
I just didn't add the cell to the array. It works now.
You don't seem to add the cell into your grid array.
public Grid (int feldlänge) {
grid = new Cell[feldlänge][feldlänge];
int x, y;
for (y = 0; y < feldlänge; y = y + 1) {
for (x = 0; x < feldlänge; x = x + 1) {
Cell cell;
cell = new Cell(x,y);
cell.setLife(false);
grid[x][y] = cell; //put the cell in the grid.
} // for
} // for
} // Konstruktor Grid
You have to add cell to your array. (german field = english array)
Also: instead of
if( someBoolean){}
if( !someBoolean){}
you should use
if( someBoolean){}
else {}
This makes it more clear what the code does

Game lags near display list data

I have a chunk of cubes that is generated using a display list, and ever time I come near it, the frame rate drops significantly. Why is this? Here's some code:
public class Chunk implements GameObject {
private int sx, sy, sz, lx, ly, lz, vertID;
private Tile[][][] tiles;
public Chunk(int sx, int sy, int sz) {
this.sx = sx;
this.sy = sy;
this.sz = sz;
this.lx = sx + 16;
this.ly = sy + 16;
this.lz = sz + 16;
init();
}
#Override
public void init() {
this.tiles = new Tile[lx][ly][lz];
vertID = glGenLists(1);
glNewList(vertID, GL_COMPILE);
for (int x = sx; x < lx; x++) {
for (int y = sy; y < ly; y++) {
for (int z = sz; z < lz; z++) {
tiles[x][y][z] = new Tile("grass.jpg");
}
}
}
glEndList();
}
public void rebuild() {
glNewList(vertID, GL_COMPILE);
for (int x = sx; x < lx; x++) {
for (int y = sy; y < ly; y++) {
for (int z = sz; z < lz; z++) {
tiles[x][y][z].getVertices(x, y, z, 16);
}
}
}
glEndList();
}
#Override
public void update() {
}
#Override
public void render() {
glCallList(vertID);
}
#Override
public void dispose() {
}
}
Your problem lies probably herein:
glNewList(vertID, GL_COMPILE);
for (int x = sx; x < lx; x++) {
for (int y = sy; y < ly; y++) {
for (int z = sz; z < lz; z++) {
tiles[x][y][z] = new Tile("grass.jpg");
}
}
}
glEndList();
If I had to make an educated guess your Tile class generate and initialiizes a texture. Well, you see, display lists don't just take on geometry and drawing calls. They also encapsulate the creation and setting of texture data. Before OpenGL-1.1 (i.e. OpenGL-1.0) there were no texture objects (glGenTextures, glBindTexture) and you used display lists as a drop in replacement for that. And that little anachronism is still present today in all compatibility OpenGL profiles. That's also why Windows calls the function to share OpenGL context data wglShareLists, although it goes far beyond lists.
To make a long story short, you probably perform a quite expensive operation right in the middle of a display list. No wonder it's slow. But seriously, why are you allocating a new texture for each tile? You should load your images into your textures only once and then only make references to them.

I need some help understanding arrays of pixels

I'm using Processing to divide a large image into a series of smaller, rectangular nodes.
Processing stores the color value for the pixels of a PImage in a pixels array, which I am accessing to break up the image into smaller parts. For some reason, I am getting this output, when my intent was for the entire image to be displayed when the nodes are arranged in draw().
Here is my main class:
ArrayList node = new ArrayList();
PImage grid;
PVector nodeDimensions = new PVector(210, 185);
PVector gridDimensions = new PVector(2549, 3300);
String name = "gridscan.jpeg";
void setup() {
size(500, 500);
grid = loadImage(name);
grid.loadPixels();
fillPixels();
noLoop();
}
void fillPixels() {
int nodeNum = 0;
for (int startX = 0; startX < 2549 - nodeDimensions.x; startX += nodeDimensions.x) {
for (int startY = 0; startY < 3300 - nodeDimensions.y; startY += nodeDimensions.y) {
node.add(new Node());
sendPixels(new PVector(startX, startY), nodeNum);
nodeNum++;
}
}
}
void sendPixels(PVector start, int nodeNum) {
for (int x = int(start.x); x < start.x + nodeDimensions.x; x++) {
for (int y = int(start.y); y < start.x + nodeDimensions.y; y++) {
Node _node = (Node) node.get(node.size() - 1);
_node.fillPixel(new PVector(x, y), grid.pixels[int(y*gridDimensions.x+x)]);
}
}
}
void draw() {
drawNodes();
}
void drawNodes() {
int nodeNum = 0;
for (int x = 0; x < width; x += nodeDimensions.x) {
for (int y = 0; y < height; y += nodeDimensions.y) {
Node _node = (Node) node.get(nodeNum);
_node.drawMe(new PVector(x - (nodeDimensions.x/2), y - (nodeDimensions.y/2)));
nodeNum++;
}
}
}
And here is the Node class:
class Node {
color[] pixel;
Node() {
pixel = new color[int(nodeDimensions.x * nodeDimensions.y)];
}
void fillPixel(PVector pos, color pixelValue) {
if(int(pos.y * nodeDimensions.y + pos.x) < 38850) pixel[int(pos.y * nodeDimensions.y + pos.x)] = pixelValue;
}
void drawMe(PVector centerPos) {
pushMatrix();
translate(centerPos.x, centerPos.y);
for(int x = 0; x < nodeDimensions.x; x++) {
for(int y = 0; y < nodeDimensions.y; y++) {
stroke(getPixelColor(new PVector(x, y)));
point(x,y);
}
}
popMatrix();
}
color getPixelColor(PVector pos) {
return pixel[int(pos.y * nodeDimensions.x + pos.x)];
}
}
Hopefully my code makes sense. I suspect the issue is in the sendPixels() method of the main class.
I used this this page from the Processing reference as a guide for creating that function, and I'm not sure where my logic is wrong.
Any help would be appreciated, and please let me know if I can clarify something.
According to getPixelColor(), it seems that it uses rows.
So if you have a 5x5 square image then 2x2 would be 7.
To get the index you use this formula:
index = (y - 1) * width + x
Explained this way it's look pretty simple, doesn't it?
Alternatively, you may be able to use getSubimage() on the BufferedImage returned by the getImage method of PImage. There's a related example here.

Categories

Resources