paint() in swing

Hi.
I overrided paint() to draw start menu for one game that I’m working on. I work with .png pictures by drawing background picture and then tekst picture on it, dependable where I am in start menu (at begining, in options submenu or somewhere else). When I click on some of the buttons, variable that remembers where I am in start menu changes accordingly and I call repaint(). In overriden paint() that is done with switch statement for different cases depending where I am.

So at begining of game start manu is drawn (start game, options, quit …), if I click on let’s say options then where_am_i variable changes to flag that I’m in options submenu and in paint() I use switch (where_am_i) to draw adequate picture for options submenu. Think sort of works, but not entirely…

When I click anywhere on button insted of previous picture of menu to disappear and new one to appear, the new one appears over the previous one. When I minimize and restore game it’s drawn as it shuold, only one picture (right one). Clearly I don’t know how (re)paint() works… I don’t understand why when I call repaint() the new picture is drawn on top of everything else instead that application draws everything from square?
One more thing, is there a better or simpler way to do start menus for games? The game itself is run in seperate thread and has active rendering, and I don’t want to do that on something so simple as start menu.
Thanks everybody.

What sort of game is this?

In general games dont use paint() and repaint(). They use active rendering instead for lots of reasons having to do with controllability and performance.

If you are stuck woth Swing for some reason, then you dont override paint() like you do in AWT, there is a different method to override. Its in the docs. Look at JFrame and JComponent.

[quote=“Jeff,post:2,topic:26224”]
paintComponent(Graphics g);

It’s a simple 2D soccer game, no AI, just 2 players over network. I’m just using it for example to learn basics of network use and developing a game overall. So I see, active rendering for all… preferably OpenGL as I see from reading forums. Well since I started it I’ll try to finish it this way. I found official tutorial on painting in AWT and Swing, http://java.sun.com/products/jfc/tsc/articles/painting/, I’ll post after I’m finish reading it.

public void paintComponent(Graphics g){
    super.paintComponent(g);
    if(dbImage != null){
        g.drawImage(dbImage, 0, 0, null);
    }
}

if you override the paintComponent method in a JPanel implementing class like i do above,
why is ‘super.paintComponent(g);’ necessary? i saw it like this in a book, but it works fine without that line…

btw jeff, what do you mean with active rendering? i don’t see how you can animate something without repaint()…
or do you mean you should have a separate render function?

could you please tell me what’s the general idea behind active rendering?

The super.paintComponent(…) call ensures that there are no visual artifacts. Also it draws any borders or other basic elements.

Active rendering means that you run a game loop and request the graphics object to draw with every iteration through the loop. You also disable event painting. Calling repaint() only schedules a call to paint(…). This could delay a paint call while your loop continues to loop around a few times before the graphics are drawn.

that was very helpfull, thanks a lot!

active rendering:

http://java.sun.com/docs/books/tutorial/extra/fullscreen/rendering.html

btw. as for main problem itself, I did workaround with clearRect(…) before every painting