Components or paint?

I know that the title is ambiguous. I will explain.

So for game graphics, I know 2 ways to draw the whole thing.

  1. You may use JPanel, JLabel or other components to draw your UI even your in-game things.
  2. You may define your own class, own variable and override the paintComponent() method in you canvas class to draw your variables.

Game are of many types, action, strategy, shooting, chess, board game etc… Some game can purely use the 2nd way of drawing, but I don’t think all game fits (especially in board game, the position of many components are fixed).

So my question is, which is better in different type of games. Which is better for different part of the game (item bar, score label, menu). Then the advantages and the disadvantages of these 2.

Never, ever use the first way to create games. Its slow and just… gross to use code designed to create UIs to make games with.

@opiop65: Ya, thats true. Did that without better knowing once and it was absolutely not a good idea.

I made a tetris game. On the left is the game zone, I used a JPanel as the canvas. On the right there are score pane, “next block pane” and else. The main frame is fixed, I don’t need to refresh the other-than-Canvas-parts so often. I reckoned it would be OK to use components there.

Yeah, that’s fine. That’s called a gui, and you should use gui components to build it, that’s what they’re for.
What I think opiop said not to do (which you shouldn’t) is to draw to a JPanel or something and move the JPanel around the screen instead of simply drawing an image moving in a canvas.
In short: you’re doing it right.

Imaging a board game, the game zone is not independent in the framework. But many parts are fixed, sometimes you even need to move these parts around in gui designing.

Sure, there are even drag and drop editors for many gui frameworks (including swing, netbeans has one built in), but that’s the gui. For the actual game’s graphics, besides maybe a HUD, you want to render that all yourself. There are of course also many ways of doing that rendering too.