Results 1 to 6 of 6
Thread: Creating an array of objects
- 11-15-2009, 11:41 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
Creating an array of objects
Hi, i am trying to make an array that creates a 6 slot grid of squares that i can reference and change the colours.
EasyGraphics class creates a graphics window and it has two methods setColour that changes the shape colour and fillRectangle that creates the shape.
I tried this, but i does not work properly! I think i need to pass a reference to the grpahics window to the method in the array ?? Please Help:)
EasyGraphics[]grid=new EasyGraphics[6];
grid[0] = new EasyGraphics(400,400);
grid[1].setColour(0,0,255);
grid[1].fillRectangle(20,20,50,50);
grid[2].setColour(255,255,100);
grid[2].fillRectangle(80,20,50,50);
grid[3].setColour(255,0,0);
grid[3].fillRectangle(140,20,50,50);
-
You must initialize each item in the array before you can use it. Here you only call new EasyGraphics once on the item in array position [0], so this is the only array item that has been constructed. The other array items will all be null, and trying to use them will generate a NullPointerException.
- 11-16-2009, 12:07 AM #3
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
OBJECTs IN ARRAY
Thanks
I actually need to only create one EasyGraphics window and put rectangles into that window, if i initialise each dataitem it creates numerous graphics windows with rectangles within them. I think i need to reference the original graphics window and call methods to put a new rectangle into each data item location in the array.
I appreciate your help, i have been confused all weekend over this and would love to resolve the problem.
-
Then you most definitely don't want to use an array of EasyGraphics if you only will use one EasyGraphics object, that's for sure.
You probably should create a Square class that holds all the information necessary to draw a Square including it's location, size, and color. Then create an array of 6 of these.
- 11-16-2009, 12:26 AM #5
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
Sounds tricky,
If i create graphics window like this
EasyGraphics g = new EasyGraphics(400,400);
it creates a 400x400 graphics window, i would like to create an array of rectangles in this graphics window using a method of the EasyGraphics class which is FillRectangle. How is this done....
one object (EasyGraphics)needs to be referenced in each array location and a different rectangle needs to be created using fillRectangle method
I think it should be simpler than creating a new class for rectangle
-
Similar Threads
-
Creating an Array of Objects
By int80 in forum New To JavaReplies: 4Last Post: 08-09-2011, 12:40 PM -
Creating and using java objects at runtime...
By baran_khan in forum Advanced JavaReplies: 5Last Post: 08-28-2008, 05:36 PM -
Creating objects based on a String value
By lvh in forum New To JavaReplies: 4Last Post: 04-30-2008, 02:00 PM -
Creating an array of nonprimitive objects
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:46 PM -
Creating objects question
By sergm in forum New To JavaReplies: 2Last Post: 12-27-2007, 04:10 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks