I'm trying make a 2 dimensional array. An array of object. I'm trying to make a 126 by 94 array of green SpotInField objects.

import javax.swing.*;

class FarmField {
	
	public static void main(String[] args) {
	
	int width = 126;
	int length = 94;
	}
	public void FarmField(SpotInField myField[][], int dwell){
	int displayDurationInSeconds = 5;
	 myField = new SpotInField[126][94];
	
	for (int i=0; i<myField.length; i++) {
		for (int j=0; j<myField[i].length; j++) {
			
			myField[i][j] = new SpotInField(0,100,0,0,100,0);
	
		}
	}
	
	 FarmField(myField, displayDurationInSeconds);
	}
	
}

class for coloring

class SpotInField {
	
	public int fR, fG, fB, bR, bG, bB;
	
	public SpotInField(int fillR, int fillG, int fillB, int borderR, int borderG, int borderB) {
		
		fR = fillR;
		fG = fillG;
		fB = fillB;
		bR = fillR;
		bG = fillG;
		bB = fillB;
		
	}
}

Recommended Answers

All 14 Replies

What exactly is your question?

one thing is that your not calling any method from within main(), which would be why it doesn't do anything (if that's your problem.

Another thing I notice that in your constructor, you put myfield = new SpotInField[126][94]; which will not do what I think you expect it to...


if you call that with a variable of type SpotInField[][], your variable will not take on the new array that your creating, if you change the array itself such as myfield[33][56]=new SpotInField(...) it will change the array that was passed, but calling new replaces the memory address of the local variable in the method, not the one that was passed.

if none of this is helps please elaborate on your problem

My question is how do I create a 2 dimensional array of object. That's the code I have so far. I'm still working on it. I don't follow what your saying. I'm confused.

I tried printing letters. It works fine, it prints 126 by 94 letters. Now, the problem is how do I change it to array of objects?

leoeroy, you can create 2d array of objects like this

SpotInField myField[][] = new SpotInField[length][width];
	
	for (int i = 0 ; i<myField.length; i++) {
		for (int j = 0; j<myField[i].length; j++) {
			myField[i][j] = new SpotInField(0,100,0,0,100,0);
		}
	}

Put this code in main method and enjoy results :)

tried the above codes, still nothing happens.

I think something happens. 2d Array creates. You don't do with them anything, that's why you think nothing happens.

You can add show() method to class SpotInField, than in method main() in loop look at array just for check it created.

Sorry for my English, not native for me.

it should be in the loop because I'm trying to make a "Field" like object. It should be a 126 by 94 "Field".

can you give an example how this "show( )" method is done?

I don't exactly understand the purpose of your program.

Nevertheless do you want to create a class which is an 126 by 94 array?
Or do you want to create a class SpotInField and then in method main() create 126 by 94 array of objects?

class FarmField {
	
	final static int width = 126;
	final static int length = 94;
	static SpotInField myField[][] = new SpotInField[width][length];
	
	public static void main(String[] args) {
	
	
	
	for (int i = 0 ; i<width; i++) {
		for (int j = 0; j<length; j++) {
			myField[i][j] = new SpotInField(0,100,0,0,100,0);
			myField[i][j].show();
		}
	}
	
	}
}


class SpotInField {
	
	public int fR, fG, fB, bR, bG, bB;
	
	public SpotInField(int fillR, int fillG, int fillB, int borderR, int borderG, int borderB) {
		
		fR = fillR;
		fG = fillG;
		fB = fillB;
		bR = fillR;
		bG = fillG;
		bB = fillB;
	}
	
	public void show() {
		System.out.println("fR = " + fR + "; fG = " + fG + "; fB = " + fB + ";");
		System.out.println("bR = " + bR + "; bG = " + bG + "; bB = " + bB + ";\n");
	}
}

this code creates a static field which is 126x94 array. This field is static just because we operates with it in static method main().

Im not printing out the array. The purpose of this program is creating an array of objects. the result of this program should be a box filled with green color.

I wrote method show just to convince you of array is created

Im not printing out the array. The purpose of this program is creating an array of objects. the result of this program should be a box filled with green color.

Then you need to show that somehow. So, your question is no longer how to create a 2d array, but rather how to display it? If so, take a look at GridLayout and JPanel and setBackground, or Canvas.

Yes the problem now is how can i display it. I did reading grid layout and all that stuff. It didn't help me at all. Can u give me a sample code on how to display this stuff?

what i want to make is a 126x94 array that will fill a 126x94 display with green color

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.