View Full Version : Objects in Array and drawing them

03-20-2012, 01:05 PM
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:

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:

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:


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

03-20-2012, 04:06 PM
You would use BookTable[x].draw(). That assumes that Book is a paintable type.

03-20-2012, 11:09 PM
Thank you now i have a much beter perspective on how i should handle the problem implementing your code (y)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum