Error Setting Object Array Values - java

package net.gfx;
public class TileSet {
public final int TILES = 627;
class Tiles {
int x = 0, y = 0;
int w = 0, h = 0;
}
public Tiles tiles[] = new Tiles[TILES];
public TileSet() {
for (int i = 0, y = 0; i < TILES; i++) {
for (int x = 0; x < 1280; x =+ 25) {
if (x > 1280) {
x = 0;
y += 40;
}
else {
tiles[i].x = x; //ERROR CAUSED HERE
tiles[i].y = y; //TO HERE *Unknown reason*
tiles[i].w = 40;
tiles[i].h = 40;
}
}
}
}
}
The Error im getting:
Exception in thread "main" java.lang.ExceptionInInitializerError
at net.jump.Jump.<clinit>(Jump.java:8)
Caused by: java.lang.NullPointerException
at net.gfx.TileSet.<init>(TileSet.java:24)
at net.gfx.Graphics.<clinit>(Graphics.java:10)
... 1 more
What I'm trying to do is basically create an array of tiles on a screen. Everyting else works besides the setting the Object array Values.
I've searched almost everywhere and haven't found anything. I bet its probaly some simple thing i missed.

You have to create an instance of Tiles before you can do any operation on it.
for (int i = 0, y = 0; i < TILES; i++) {
for (int x = 0; x < 1280; x =+ 25) {
if (x > 1280) {
x = 0;
y += 40;
}
else {
tiles[i] = new Tiles(); //instance created here.
tiles[i].x = x;
tiles[i].y = y;
tiles[i].w = 40;
tiles[i].h = 40;
}
}
}

You aren't initializing each Tiles in your array, as the default value is null.
Perhaps you should try initializing each Tiles:
for(int i = 0; i < tiles.length; i++){
tiles[i] = new Tiles();
}
After that, you could perform operations with each Tiles element in the array.

Related

List of blocks to a whole matrix - java

So I'm having the following problem: I have a method that breaks a big matrix into smaller blocks of the same size. After I do some operations on the blocks, I want to reconstruct the big matrix in the right order, but I'm going wrong at it somehow.
The following code reconstructs correctly a 4x4 matrix that breaks into 2x2, but for any other dimensions, it's not working properly.
public long[][] blocksToMatrix(List<long[][]> blocks, int blockDimension, int width, int height ){
long[][] yuvMatrix = new long[height][width];
int heightPos = 0;
int widthPos = 0;
for (int i = 0; i < blocks.size(); i++) {
long[][] yuvBlock = blocks.get(i);
int heightPosTemp = heightPos;
for (int j = 0; j < blockDimension * blockDimension; j++) {
yuvMatrix[heightPos][widthPos] = yuvBlock[j / blockDimension][j % blockDimension];
widthPos++;
if (widthPos >= width){
widthPos = (i * blockDimension) % width;
heightPos++;
}
if (widthPos == ((i + 1) * blockDimension) % width){
widthPos = (i * blockDimension) % width;
heightPos++;
}
}
if (heightPos == height ){
heightPos = heightPosTemp;
}
else {
heightPos = (i * blockDimension) % height;
}
widthPos = ((i + 1) * blockDimension) % width;
}
return yuvMatrix;
}
The method I used to break the matrix:
public List<long[][]> matrixToBlocks(long[][] yuvMatrix, int blockDimension, int width, int height){
int blocksSize = width / blockDimension * (height / blockDimension);
List<long[][]> blocks = new ArrayList<long[][]>();
for (int i = 0; i < blocksSize; i++) {
long[][] subBlock = new long[blockDimension][blockDimension];
int heightPos = (blockDimension * (i / blockDimension)) % height;
int widthPos = (blockDimension * i) % width;
if (widthPos + blockDimension > width) {
widthPos = 0;
}
for (int row = 0; row < blockDimension; row++) {
for (int col = 0; col < blockDimension; col++) {
subBlock[row][col] = yuvMatrix[heightPos + row][col + widthPos];
}
}
blocks.add(subBlock);
}
return blocks;
}
The way I tested it:
public static void testareMatBlo(int height, int width, int blockdim){
long[][] test = new long[height][width];
int val = 1;
for (int i = 0; i < height; i++){
for (int j = 0; j < width; j++){
test[i][j] = val;
val++;
}
}
List<long[][]> blocks = matrixToBlocks(test, blockdim, width, height);
long[][] matrix = blocksToMatrix(blocks, blockdim, width, height);
if (Arrays.deepEquals(test, matrix)){
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
This works:
testareMatBlo(4, 4, 2);
But anything else doesn't. Can anyone explain what I did wrong?
I didn't thoroughly read your code for matrixToBlocks(...) but all those calculations like int blocksSize = width / blockDimension * (height / blockDimension); are very likely to introduce hard to spot errors - and you actually don't need them:
public static List<long[][]> matrixToBlocks(long[][] yuvMatrix, int blockDimension){
//Check matrix and block dimension match
if( yuvMatrix.length == 0 || yuvMatrix.length % blockDimension != 0
|| yuvMatrix[0].length == 0 || yuvMatrix[0].length % blockDimension != 0 ) {
throw new IllegalArgumentException("whatever message you like");
}
List<long[][]> blocks = new ArrayList<long[][]>();
//Iterate over the blocks in row-major order (down first, then right)
for( int c = 0; c < yuvMatrix.length; c += blockDimension ) {
for( int r = 0; r < yuvMatrix[c].length; r += blockDimension ) {
long[][] subBlock = new long[blockDimension][blockDimension];
//Iterate over the block in row-major order
for(int bc = 0; bc < blockDimension; bc++ ) {
for(int br = 0; br < blockDimension; br++ ) {
subBlock[bc][br]=yuvMatrix[c+bc][r+br];
}
}
blocks.add(subBlock);
}
}
return blocks;
}
That method doesn't look shorter but it is: discounting the preliminary check yours is missing there are only 8 actual lines of code compared to 13 in your code. That's not the point however. What's more important is that the logic is easier since there are only a few calculations involved (like c+bc).
You might think this is inefficient but it isn't: you're accessing each element only once and thus even though there are 4 nested loops the overall complexity is still O(n) with n being the size of the matrix.
Constructing the matrix back is equally easy. The major thing you need to take care of is the ordering of the blocks: if you create them in row-major order (blocks below each other are next to each other in the list) you need to recreate the matrix in the same way:
public static long[][] blocksToMatrix( List<long[][]> blocks, int width, int height ) {
long[][] yuvMatrix = new long[width][height];
int c = 0;
int r = 0;
for( long[][] block : blocks ) {
int blockWidth = block.length;
int blockHeight = block[0].length;
for( int bc = 0; bc < block.length; bc++ ) {
for( int br = 0; br < block[bc].length; br++ ) {
yuvMatrix[c + bc][r + br] = block[bc][br];
}
}
//calculate the next offset into the matrix
//The blocks where created in row-major order so we need to advance the offset in the same way
r += blockHeight;
if( r >= height ) {
r = 0;
c += blockWidth;
}
}
return yuvMatrix;
}

Array Sort Stops Too Soon

I'm trying to create two classes, one to define a point and another for array operations. I'm trying to create a method to sort an array of coordinates in ascending order based on y coordinates. I've tried following examples, but I keep running into a runtime error where the array is only partially sorted.
public class Point
{
private double x;
private double y;
public Point(double x_coord, double y_coord)
{
x = x_coord;
y = y_coord;
}
public boolean lessThan(Point anotherPoint)
{
if(y < anotherPoint.y)
{
if(x < anotherPoint.x)
{
return true;
}
}
return false;
}
}
public class PointArray
{
private Point[] points = new Point[count];
public PointArray(double[] doubleArray)
{
if(doubleArray.length % 2 == 0)
{
for(int i = 0, j = 0; i < 3; i++, j += 2)
{
double x = doubleArray[j];
double y = doubleArray[j + 1];
points[i] = new Point(x, y);
}
}
else
{
System.out.println("Error: The given array must be even.");
System.exit(0);
}
}
public void sort()
{
double x = 0;
double y = 0;
Point newPoint = new Point(x, y);
Point temp = new Point(x, y);
for (int i = 0; i < points.length - 1; i++)
{
for(int j = i + 1; j < points.length; j++)
{
int minIndex = i;
if(points[minIndex].lessThan(points[j]) == false)
{
temp = points[minIndex];
points[minIndex] = points[j];
points[j] = temp;
}
}
}
}
This code causes the array {5.6, 7.1, 4.9, 13.17, 9.3, 2.9} to first be stored as ordered pairs {(5.6, 7.1), (4.9, 13.17), (9.3, 2.9)}. but it does not sort them properly. After the first and third points are swapped, the second and third are not, even though the y coordinate of the third is smaller.
[(9.3, 2.9), (4.9, 13.17), (5.6, 7.1)]
EDIT: Another issue appeared related to the same assignment. This method is supposed to take two PointArray objects and compare them for equality by the x and y components. My idea was to sort both arrays and then compare the components using a method in the Point class, but I'm not sure how to define each PointArray in terms of an (x, y) point.
public boolean equals(Point anotherPoint)
{
if(x == anotherPoint.x && y == anotherPoint.y)
{
return true;
}
return false;
}
public boolean equals(PointArray anotherPointArray)
{
double x = 0;
double y = 0;
double xAnother = 0;
double yAnother = 0;
Point newPoint = new Point(x, y);
Point newAnotherPoint = new Point(xAnother, yAnother);
anotherPointArray.sort();
for(int i = 0; i < points.length; i++)
{
for(int j = 0; i < points.length; j++)
{
if(newPoint.equals(newAnotherPoint))
{
return true;
}
}
}
return false;
}
Your current lessThan method will give true only if both x and y are smaller. To sort by y alone use
public boolean lessThan(Point anotherPoint)
{
return y < anotherPoint.y;
}

How can I make this ImageBrightener method function properly?

This ImageBrightener method is supposed to brighten the image by increasing the color values. Each value should increase half the distance between it and 255. Thus, 155 would go to 205 while 205 would go to 230 and so on. Can anyone help figure out the issue with ImageBrightener! Thanks
import squint.SImage;
public class ImageBrightener implements ImageTransformer {
#Override
public SImage transform(SImage picture) {
return BrightenImage(picture);
}
private static SImage BrightenImage(SImage si) {
int[][] newReds = BrightenImageSingleChannel(si.getRedPixelArray());
int[][] newGreens = BrightenImageSingleChannel(si.getGreenPixelArray());
int[][] newBlues = BrightenImageSingleChannel(si.getBluePixelArray());
return new SImage(newReds, newGreens, newBlues);
}
// Here is the code to brighten the image and is not functioning properly
private static int[][] BrightenImageSingleChannel(int[][] pixelArray) {
private static int[][] BrightenImageSingleChannel(int[][] pixelArray) {
int columns = pixelArray.length;
int rows = pixelArray[0].length;
int[][] answer = new int[columns][rows];
for (int x = 0; x < columns; x++) {
for (int y = 0; y < rows; y++) {
answer[x][y] = 255 - pixelArray[x][y] ;
answer[x][y] = answer[x][y] + pixelArray[x][y] ;
}
}
return answer;
}
}
// Here is the properly functioning code for darkening my image.
private static int[][] DarkenImageSingleChannel(int[][] pixelArray) {
int columns = pixelArray.length;
int rows = pixelArray[0].length;
int[][] answer = new int[columns][rows];
for (int x = 0; x < columns; x++) {
for (int y = 0; y < rows; y++) {
answer[x][y] = (255 * 2) / 3 - pixelArray[x][y];
}
}
return answer;
}
}
The problem is here
answer[x][y] = 255 - pixelArray[x][y] ;
answer[x][y] = answer[x][y] + pixelArray[x][y] ;
answer[x][y] will always be 255.
Try this
answer[x][y] = (pixelArray[x][y] + 255) / 2;

Cut an 2d Array down to needed part

I have an array with some numbers in it, that are procedurally generated through the Code and I want to remove the part of the array that is not necessary.
So until now I tried my best to create my own code, but it didn't work quite well (Thats the reason why im here).
This was my try:
private void cutPlacementMap() {
int firstNeededX = Integer.MAX_VALUE;
int firstNeededZ = Integer.MAX_VALUE;
int lastNeededX = -1;
int lastNeededZ = -1;
for (int x = 0; x < placementMap.length; x++) {
for (int z = 0; z < placementMap[0].length; z++) {
if (placementMap[x][z] != 0) {
if (x < firstNeededX)
firstNeededX = x;
if (z < firstNeededZ)
firstNeededZ = z;
if (x > lastNeededX)
lastNeededX = x;
if (z > lastNeededZ)
lastNeededZ = z;
}
}
}
int lengthX = lastNeededX - firstNeededX;
int lengthZ = lastNeededZ - firstNeededZ;
int[][] newPlacementMap = new int[lengthX + 1][lengthZ + 1];
System.out.println("lengthX: " + lengthX);
System.out.println("lengthZ: " + lengthZ);
int conX = 0;
int conZ = 0;
for (int x = firstNeededX; x <= lastNeededX; x++) {
for (int z = firstNeededZ; x <= lastNeededZ; z++) {
newPlacementMap[conX][conZ] = placementMap[x][z];
conZ++;
}
conZ = 0;
conX++;
}
placementMap = newPlacementMap;
}
This Code throws an ArrayIndexOutOfBoundException. That is pointing to the line newPlacementMap[conX][conZ] = placementMap[x][z]; as reason for the exception.
Every help is appreciated. Thanks.
Debugging this yourself should be pretty easy.
Take a look at your loop:
for (int z = firstNeededZ; x <= lastNeededZ; z++)
This will loop forever (or until you get the ArrayIndexOutOfBoundsException), because your stop condition contains a typo.
x <= lastNeededZ must of course be z <= lastNeededZ.

Null Pointer Exception in an Array of Strings

I am creating a shadow of createTriangle() in the method createPathingTriangle(). createTriangle worked just fine, but when I created the identically-dimensioned createPathingTriangle that used String instead of int, the new triangle started throwing NPE's.
The line that's throwing it is-
pathingTriangle[y][x] = new String("00" + String.valueOf(x));
i.e. the first line that populates it. I looked it up and have liberally sprinkled "new" around the code in createPathingTriangle, but it didn't seem to solve the problem.. I am assuming that it has something to do with the fact that int is a primitive but String is not, but hours of fiddling and nothing gives.
private int[][] createTriangle() {
triangle = new int[triangleSize][];
for (int y = 0; y < triangle.length; y++){
int [] xAxis = new int[triangle.length - y];
for (int x = 0; x < xAxis.length; x++){
xAxis[x] = (int) (Math.random() * 100);
}
triangle[y] = xAxis;
}
printTriangle(triangle);
return triangle;
}
private String[][] createPathingTriangle() {
pathingTriangle = new String[triangleSize][];
for (int y = 0; y < pathingTriangle.length; y++){
for (int x = 0; x < pathingTriangle.length - y; x++){
if (x < 10){
pathingTriangle[y][x] = new String("00" + String.valueOf(x));
}
else if (x < 100){
pathingTriangle[y][x] = new String("0" + String.valueOf(x));
}
else{
pathingTriangle[y][x] = new String(String.valueOf(x));
}
}
}
return pathingTriangle;
}
You never initialize the second dimension of the array pathingTriangle = new String[triangleSize][];
which is done in the first funnction here int [] xAxis = new int[triangle.length - y];

Categories

Resources