Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Objects in Array and drawing them

    Hello fellow Java scripters,

    I have a question about object in array, and how to draw them.

    Il try to explain this further now:

    for example if i have a Clas Book(int xPos, int yPos)
    i put this clas in an Array in the BookPanel as following:
    Code:
    Book[] BookTabel;
    
    public Book()
    {
    BookTabel = new Book[3]
    
    book[0] = new Book(100,100)
    book[1] = new Book(200,200)
    book[2] = new Book(300,300)
    }
    Now in the draw(Graphics g) i put a for-loop
    like this:
    Code:
    public paintComponent(Graphics g)
    {
        for(x=0 ; x<= BookTabel.length ; x++)
            {
                 /** How do i draw these objects in this paintcomponent??**/
             }
    
    }
    so i need to draw these Books on the panel i have an other way of doing this but i need to do it with the array and the for loop.

    How i draw a Book now is:
    i declare and object of the book like this:

    private Book book1;

    book1 = new Book(int x, int y)

    And draw it in the draw(Graphics g) like this:

    book1.draw(g);

    Then it draws the book on screen with no problem.

    So for short i need the code that draws the objects in the array with the for loop on screen.

    because codes like book.draw(g); doenst work

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You would use BookTable[x].draw(). That assumes that Book is a paintable type.

  • #3
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you now i have a much beter perspective on how i should handle the problem implementing your code (y)


  •  

    Posting Permissions

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