This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with Iterating through objects to draw on panel  RSS feed

 
Steve Le
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I currently have the code set up to iterate through objects and keep track of them but how do I do the same and draw the objects on the panel window? I'm confused on what to do and how to start it.

Here's the code I have so far, the 'Car' which I want to draw extends to another class which extends to GameObject. I currently have a 'draw()' method under the 'Car' class but nothing in the 'draw()' method yet. From what I understand i'm suppose to have a 'draw()' method under 'GameObjects' class also?






Mostly I don't understand how to go through the iterator again to paint the objects. Wouldn't I need to put another iterator under 'paintComponent()' or can it be done under 'update()' ?

If more information is needed I can provide it, just didn't want to post too much code. I just need help understand or getting a jump start on how to get some shapes from the GameObjects onto my panel.
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know how to paint ordinary shapes, e.g. rectangles on the panel?

I shall move this thread to our GUIs forum.

And welcome to the Ranch
 
Steve Le
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Do you know how to paint ordinary shapes, e.g. rectangles on the panel?

I shall move this thread to our GUIs forum.

And welcome to the Ranch


Yes I do, I can do it fine on the same panel but putting them in separate classes is where I'm not quite understanding it. Each class needs a different `draw()` since there are different shapes, etc. How would I do that and be able to iterate that through the list?

I can draw shapes, add them and move them on that single class I provided but say I have a class `Car` that has a `draw()` method. I need to take that and draw it with a provided list of objects.
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make each class implement a Drawable interface, rather like this. Remember that the documentation comments are the biggest and bestest part of any interface (at least before Java8).You pass the same Graphics object that you have in the paintComponent method to that draw() method. Start with one Drawable object, then consider using a List<Drawable>.
By the way: paintComponent should have protected access. Use the @Override annotation whenever you think you are overriding a method.
 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't I need to put another iterator under 'paintComponent()' or can it be done under 'update()' ?


Yes, the Iterator is required in paintComponent();


or can it be done under 'update()' ?


I have no idea why you need this method. Custom painting is done in the paintComponent() method.

Check out the DrawOnCompent example from Custom Painting Approaches that shows how this can be done.
 
Steve Le
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Make each class implement a Drawable interface, rather like this. Remember that the documentation comments are the biggest and bestest part of any interface (at least before Java8).You pass the same Graphics object that you have in the paintComponent method to that draw() method. Start with one Drawable object, then consider using a List<Drawable>.
By the way: paintComponent should have protected access. Use the @Override annotation whenever you think you are overriding a method.


Ok so each class has a draw() method. If you look under the update() method on the OP it has a while loop. That while loop loops through the list and prints out each object's information. That works perfect, it prints out all the objects available on the list. I tried that same loop in the paintComponent to draw the shapes but it gives me null pointer, how would I do this? I can't figure out what I did wrong, I must be doing something wrong in the other while loop since it cannot get the 'o' from 'IObservable'
 
Steve Le
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
Wouldn't I need to put another iterator under 'paintComponent()' or can it be done under 'update()' ?


Yes, the Iterator is required in paintComponent();


or can it be done under 'update()' ?


I have no idea why you need this method. Custom painting is done in the paintComponent() method.

Check out the DrawOnCompent example from Custom Painting Approaches that shows how this can be done.


So I'm trying to get the iterator through paintComponent() but it's giving me a null pointer. From my understanding the iterator from update() should be essentially the same in paintComponent to draw the shapes.

Also I have the update() method because my program calls this method every time it goes through, so my repaint() is also in the update().
 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The point is why does you program invoke the update() method. That method doesn't do anything. If the GameWorldProxy class contains the objects you want to paint, then that object should be part of the class, not passed to the class as a parameter.

Also, the paintComponent() method should NOT start a Timer. The painting methods are for painting only.

 
Steve Le
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:The point is why does you program invoke the update() method. That method doesn't do anything. If the GameWorldProxy class contains the objects you want to paint, then that object should be part of the class, not passed to the class as a parameter.

Also, the paintComponent() method should NOT start a Timer. The painting methods are for painting only.



I went redid that and didn't fix it in the OP. I put timer.start() in the method MapView(). The update() will handle the repaint according to where the objects will be, maybe that'll clear it up a bit. So I also took out actionPerformed and update should update the objects every time something happens. I have everything set up correctly now but I can't iterate through the objects to draw under paintComponent. If you look under the update() method there's a while loop there that prints out the objects information but when I use that same while loop to draw instead in paintComponent it won't work.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!