Processing Graphics Error - java

I made a program that uses a 2D array to draw a grid, but I am getting a null pointer exception. Here is my code:
Cell[][] Cells;
void setup(){
size(500,500);
background(255);
for (int x = 0; x < 50; x++) {
for (int y = 0; y < 50; y++) {
Cells[x][y] = new Cell(round(random(255)),x * 10,y * 10,10,10);
}
}
}
void draw(){
for (int x = 0; x < 50; x++) {
for (int y = 0; y < 50; y++) {
Cells[x][y].Draw();
}
}
}
class Cell{
int X;
int Y;
int Width;
int Height;
int Color;
Cell(int C,int X,int Y,int Width,int Height){
Color = C;
X = X;
Y = Y;
Width = Width;
Height = Height;
}
void Draw(){
fill(Color);
rect(X,Y,Width,Height);
}
}
and here is the error message:
Exception in thread "Animation Thread" java.lang.NullPointerException
at Grid.setup(Grid.java:27)
at processing.core.PApplet.handleDraw(PApplet.java:2103)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:190)
at processing.core.PApplet.run(PApplet.java:2006)
at java.lang.Thread.run(Thread.java:662)
what am I doing wrong?

You missed to create the array:
Cell[][] cells = new Cell[50][50];

Related

How do i make all circles flicker

i am trying to make all the circles i draw flicker in my code. i am currently able to make the latest circle that was drawn to flicker but the goal is to make all the circles on the canvas flicker.
here is my code;
float colour = random(256);
final int DIAM = 20;
final int MAX_NUM = 1000;
int numPointsX = 0;
int numPointsY = 0;
int [] xPos = new int[MAX_NUM];
int [] yPos = new int [MAX_NUM];
boolean start = false;
void setup() {
size (500, 500);
}
void draw() {
if (start) {
if (xPos[numPointsX-1] > 0 || (yPos[numPointsY-1]>0)) {
fill(random(256), random(256), random(256));
circle(xPos[numPointsX-1], yPos[numPointsY-1], DIAM);
}
}
println(xPos[0]);
}
void mouseClicked() {
insertXandY();
}
void insertXandY() {
int x = mouseX;
int y = mouseY;
xPos[numPointsX] = x;
yPos[numPointsY] = y;
numPointsX += 1;
numPointsY += 1;
start = true;
}
You need to draw all the circles in a loop with a different color in each frame:
void draw() {
background(255);
fill(random(256), random(256), random(256));
for (int i = 0; i< numPointsX; ++i) {
circle(xPos[i], yPos[i], DIAM);
}
}
Complete example:
float colour = random(256);
final int DIAM = 20;
final int MAX_NUM = 1000;
int numPointsX = 0;
int numPointsY = 0;
int [] xPos = new int[MAX_NUM];
int [] yPos = new int [MAX_NUM];
boolean start = false;
void setup() {
size (500, 500);
}
void draw() {
background(255);
fill(random(256), random(256), random(256));
for (int i = 0; i< numPointsX; ++i) {
circle(xPos[i], yPos[i], DIAM);
}
}
void mouseClicked() {
insertXandY();
}
void insertXandY() {
int x = mouseX;
int y = mouseY;
xPos[numPointsX] = x;
yPos[numPointsY] = y;
numPointsX += 1;
numPointsY += 1;
start = true;
}

error message: "Pixels and SIZE cannot be resolved or are not a field"

I've run this code this in the newest version of Eclipse but in that, it did not specify this error message but did point at the pixels[x + y * width] = Sprite.grass.pixels[(x & 15) + (y & 15) *Sprite.grass.SIZE]; line of the code. So I didn't know what was the error so I ran it on an older version of the IDE, Kepler. In the IDE it gave this error message. I searched a lot on stackoverflow, there were several cases where people got the error message but most of them were related to importing android.R or something of that sort. But I'm not doing this for any android application, this is just for a normal project. So none of the solutions matched my problem.
The error message is pointing towards pixels and SIZE in Sprite class. If you need the SpriteSheet class code too let me know. There I was trying to load an image. This is all I can explain for now. So could anyone help me out, please?
package com.thecherno.rain.graphics;
import java.util.Random;
public class Screen {
private int width, height;
public int[] pixels;
final int MAP_SIZE = 64;
final int MAP_SIZE_INT = MAP_SIZE - 1;
public int[] tiles = new int[MAP_SIZE * MAP_SIZE];
public Random random = new Random();
public Screen(int width, int height) {
this.width = width;
this.height = height;
pixels = new int[width * height];
for(int i = 0; i < MAP_SIZE * MAP_SIZE; i++) {
tiles[i] = random.nextInt(0xffffff);
tiles[0] = 0;
}
}
public void clear() {
for(int i = 0; i < pixels.length; i++) {
pixels[i] = 0;
}
}
public void render(int xoffset, int yoffset) {
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
pixels[x + y * width] = Sprite.grass.pixels[(x & 15) + (y & 15) *Sprite.grass.SIZE];
}
}
}
}
package com.thecherno.rain.graphics;
public class Sprite {
public final int SIZE;
private int x, y;
public int[] pixels;
private SpriteSheet sheet;
public static Sprite grass = new Sprite(16, 0, 0, SpriteSheet.tiles);
public Sprite(int size, int x, int y, SpriteSheet sheet) {
SIZE = size;
pixels = new int[SIZE * SIZE];
this.x = x * size;
this.y = y * size;
this.sheet = sheet;
load();
}
private void load() {
for(int y = 0; y < SIZE; y++) {
for(int x = 0; x < SIZE; x++) {
pixels[x + y * SIZE] = sheet.pixels[(x + this.x) + (y + this.y) * sheet.SIZE];
}
}
}
}

Create multiple of 1 objects next to each other (grid like)

Hey if i have a simple rectangle class, how can i make it so that it creates that rectangle next to each other in a grid like pattern? maybe like 10 rows 10 columns?
public class Vak {
private int posX = 0;
private int posY = 0;
private int width = 50;
private int height = 50;
private Color colour;
public Vak(Color c, int x, int y){
this.colour = c;
this.posX = x;
this.posY = y;
}
public int vakPosY(){
return this.posY;
}
public int vakPosX(){
return this.posX;
}
public void draw (Graphics g){
g.setColor(this.colour);
g.drawRect(posX, posY, width, height);
}
public void move(int numberPixelsX, int numberPixelsY){
this.posX = this.posX + numberPixelsX;
this.posY = this.posY + numberPixelsY;
}
}
this is my code for rectangle "vak"
Is this what you are looking for?
int mapWidth = 10;
int mapHeight = 10;
// tileWidth and tileHeight should probably be public static const fields or static readonly properties of some class, but I put them here for now.
int tileWidth = 50; // Pixels
int tileHeight = 50; // Pixels
// tiles should probably be a field of a Map class (if you have one)
Vak[][] tiles = new Vak[mapWidth][mapHeight];
for(int x = 0; x < mapWidth; x++)
{
for(int y = 0; y < mapHeight; y++)
{
tiles[x][y] = new Vak(Color.white, x*tileWidth, y*tileHeight);
}
}
And then in the drawing part of the main loop:
for(Vak[] row : tiles)
{
for(Vak tile : row)
{
tile.draw(g);
}
}

ERROR while trying to run my java game

Below is the code that i received in the console when trying to run my game download the code here.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 64
at com.comli.blah.rainofwrath.graphics.Screen.<init>(Screen.java:22)
at com.comli.blah.rainofwrath.game.<init>(game.java:38)
at com.comli.blah.rainofwrath.game.main(game.java:123)
Code:
public class Screen {
private int width, height;
public int[] pixels;
public final int MAP_SIZE = 64;
public final int MAP_SIZE_MASK = MAP_SIZE - 1;
public int[] tiles = new int[8 * 8];
private Random random = new Random();
public Screen (int width, int height) {
this.width = width;
this.height = height;
pixels = new int[width * height]; // 50,400
for (int i = 0; i < MAP_SIZE * MAP_SIZE; i++) {
tiles[i] = random.nextInt(0xffffff);
}
}
public void clear() {
for (int i = 0; i < pixels.length; i++) {
pixels[i] = 0;
}
}
public void render(int xOffset, int yOffset) {
for (int y = 0; y < height; y++) {
int yy = y + yOffset;
// if (yy < 0 || yy >= height) break;
for (int x = 0; x < width; x++) {
int xx = x + xOffset;
// if (xx < 0 || xx >= width) break;
int tileIndex = ((xx >> 4) & MAP_SIZE_MASK) + ((yy >> 4) & MAP_SIZE_MASK) * MAP_SIZE;
pixels[x + y * width] = Sprite.grass.pixels[(x&15) + (y&15) * Sprite.grass.SIZE];
}
}
}
}
The problem here is in the tiles array. It has 64 (8 * 8) elements during initialization, whilst by specifying i < MAP_SIZE * MAP_SIZE (MAP_SIZE = 64) as stop-rule at you loop you are trying to go beyond this limit. I belive it should be:
public int[] tiles = new int[MAP_SIZE * MAP_SIZE]

How to make circle clickable

I am developing game using canvas. I have drawn grid of circles using loop. I want to get x,y coordinates of circles. For that I have to make those circles clickable so that whenever player click on a circle it should return its coordinates. I will pass those coordinates to lineDraw() method for drawing line between those circles.
This loop is defining grid of circles:
for (int y=0;y<rows;y++)
{
for (int x=0;x<cols;x++)
{
canvas.drawCircle((x + 1) * dw, (y + 1) *(3* dh), 20, pDot);
}
}
Here dw is (displayWidth) and dh is (displayHeight). Please suggest how I should make these circles clickable?
I had played with this project a little after your previous questions. I'm sure this isn't the most optimized way to do this, but without knowing how you plan to implement the game logic, I think this is flexible enough to adapt. The following is your custom View class, which I've renamed Board, in keeping with Java naming conventions.
public class Board extends View
{
Paint pBack = new Paint();
Paint pDot = new Paint();
Paint pLine = new Paint();
int cols = 5;
int rows = 6;
// Default initialization = false
boolean[][] dots = new boolean[cols][rows];
int canWidth = 0;
int canHeight = 0;
float xStep = 0;
float yStep = 0;
float[] xCoords = new float[cols];
float[] yCoords = new float[rows];
public Board(Context context)
{
super(context);
pBack.setARGB(255, 255, 102, 0);
pDot.setARGB(255, 255, 255, 255);
pLine.setStrokeWidth(5);
pLine.setARGB(255, 90, 10, 0);
}
public void setDots(boolean[][] dotSelected)
{
this.dots = dotSelected;
}
public boolean[][] getDots()
{
return dots;
}
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
canWidth = w;
canHeight = h;
xStep = canWidth / (cols + 1);
yStep = canHeight / (rows + 1);
for (int y = 0; y < rows; y++)
{
yCoords[y] = (y + 1) * yStep;
}
for (int x = 0; x < cols; x++)
{
xCoords[x] = (x + 1) * xStep;
}
}
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawPaint(pBack);
// Grid, lines and box markings
for (int y = 0; y < rows; y++)
{
canvas.drawLine(xStep, yCoords[y], cols * xStep, yCoords[y], pDot);
for (int x = 0; x < cols; x++)
{
if (y == 0)
{
canvas.drawLine(xCoords[x], yStep, xCoords[x], rows * yStep, pDot);
}
if (dots[x][y])
{
boolean left = x > 0 && dots[x - 1][y];
boolean up = y > 0 && dots[x][y - 1];
if (left)
{
canvas.drawLine(xCoords[x], yCoords[y], xCoords[x - 1], yCoords[y], pLine);
}
if (up)
{
canvas.drawLine(xCoords[x], yCoords[y], xCoords[x], yCoords[y - 1], pLine);
}
if (left && up && dots[x - 1][y - 1])
{
canvas.drawCircle(xCoords[x] - xStep / 2, yCoords[y] - yStep / 2, 10, pLine);
}
}
}
}
// Dots
for (int y = 0; y < rows; y++)
{
for (int x = 0; x < cols; x++)
{
canvas.drawCircle(xCoords[x], yCoords[y], 20, pDot);
if (dots[x][y])
{
canvas.drawCircle(xCoords[x], yCoords[y], 15, pBack);
}
}
}
}
public boolean onTouchEvent(MotionEvent event)
{
super.onTouchEvent(event);
if (event.getAction() != MotionEvent.ACTION_DOWN)
return true;
int xNear = 0;
int yNear = 0;
float xMin = canWidth;
float yMin = canHeight;
for (int x = 0; x < cols; x++)
{
if (Math.abs(xCoords[x] - event.getX()) < xMin)
{
xMin = Math.abs(xCoords[x] - event.getX());
xNear = x;
}
}
for (int y = 0; y < rows; y++)
{
if (Math.abs(yCoords[y] - event.getY()) < yMin)
{
yMin = Math.abs(yCoords[y] - event.getY());
yNear = y;
}
}
dots[xNear][yNear] = !dots[xNear][yNear];
invalidate();
return true;
}
}
Store the data of your circles (for instance the position and the radius) in a list and check each of them for mouse collision(register a mouseeventlistener).
public class Circle
{
private int radius;
private int positionX;
private int positionY;
public Circle(int positionX, int positionY, int radius)
{
this.radius = radius;
this.positionX = positionX;
this.positionY = positionY;
}
public int getPositionX()
{
return this.positionX;
}
public int getPositionY()
{
return this.positionY;
}
public boolean contains(int posX, int posY)
{
int distanceX = this.positionX - posX;
int distanceY = this.positionY - posY;
return Math.sqrt((distanceX * distanceX) + (distanceY * distanceY)) <= this.radius;
}

Categories

Resources