Results 1 to 6 of 6
  1. #1
    geowizard is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Smile 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);

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    geowizard is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default 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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  5. #5
    geowizard is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    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

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Seeing as how you know about this EasyGraphics library and we don't, you have us at a disadvantage. Much luck though with this project.

Similar Threads

  1. Creating an Array of Objects
    By int80 in forum New To Java
    Replies: 4
    Last Post: 08-09-2011, 12:40 PM
  2. Creating and using java objects at runtime...
    By baran_khan in forum Advanced Java
    Replies: 5
    Last Post: 08-28-2008, 05:36 PM
  3. Creating objects based on a String value
    By lvh in forum New To Java
    Replies: 4
    Last Post: 04-30-2008, 02:00 PM
  4. Creating an array of nonprimitive objects
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:46 PM
  5. Creating objects question
    By sergm in forum New To Java
    Replies: 2
    Last Post: 12-27-2007, 04:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •