I’ve been working with Java graphics programming and I feel that I understands how it works.
One thing that I don’t like/haven’t grapsed yet is how to manage drawing lots of graphics (shapes, images, etc) on a panel.
All the books/tutorials I’ve currently being going through have you put all the drawing you want done in the paintCompnents/paint function. Ex,
public void paint(Graphics g){
//do stuff here....
}
Thing is I have so much I want to draw that putting a bunch of code in one function is ugly, hard to read, and makes it hard to manage/edit…
I understands OOP but I’m new to graphics programming. How should I manage all the “data” I want drawn on my canvas (JPanel to exact). I’m currently calling functions inside the paint(..) method. Ex,
public void paint(Graphics g){
drawCars(g, x,y,wid,len,etc,etc);
}
then…
drawCars(Graphics g, int x, int y, int wid, etc etc){
g.drawSomething(x,y,wid,len,)
g.drawAnotherThing(x+90,y+60,etc, etc);
}
The problem is then in my JPanel class I have all these stray functions i have to manage.
And this is only for drawing “one page” of stuff.. I can’t imagine having to switch views or a game screen where I could have a bunch of things animating on a page..
I’m just looking for direction on how to organize code drawn by the paint() method in java so I don’t go crazy looking at my old work. Any tutorials, books (esp if they’re free), or maybe good code to read would be very very appreciated.
Everything I’ve found thus far is about teaching what you “can” do with graphics/graphics2D/java but i’m looking for organization/style tips as suggested by experts..
Make classes for logical things you draw like cars. Each object would know all it needs to know to paint it self like position, size and color. If you let all these objects implement a common interface you can all put them together in a list. When time comes to draw them you iterate over the list and call each objects draw method.
Apart from that document the drawing code well so that for each draw statement it is clear what part it is actual drawing.