0

I'm quite new in game programming an di have a few doubts about the relation between it and swing in java. I made some user interfaces using swing before, but when reading about making a game with it there are a lot of new points to take into account.

I know that swing and awt should not be fit together, but in many tutorial and sites it's used a canvas with jframe, i guess that would maybe be better the use of a jpanel and override the paint method?

And if i plan to add, let's say, diferent panels to the menu or so (of the game) can be done with the individual painting of different panels, adding to them swing elements (such as jlabels or jbuttons) or i have to make the method to paint and check the containing pixels for the events? I'm not sure if i explained myself correctly there, i mean if, for example, keep the central spot of a borderlayout to display things in a panel and then adding a whole different panel in the east spot to fill it with buttons, or has to be those buttons painted over the first one and build their own events.

Another thing is taht i've been said that the variables are for the methods and have to use getters and setters to get them, but so far i'm seeing all as static to catch them from outside, is this right to be done?

Also know that it should be have a permanent loop for it in the run method, i understand that, but i've seen different thoughs about the use of timers for triggering events. I don't really understand that, should use a counter for it? or use one of the timers already provided by java? and which timer would be used for such tasks? the swing or the util one :/

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • `i guess that would maybe be better the use of a jpanel and override the paint method?` - you should be overriding `paintComponent()` NOT paint(). – camickr Feb 23 '14 at 17:50
  • Woops, yep. Was refering to that, but explained it the wrong way ^^' thank you for the correction :) – user3343618 Feb 26 '14 at 14:34

2 Answers2

0

You can check out my Snake Game which I implemented in Java. It will tell you how to make games from scratch. The code isn't the best looking that I can come up with, but it still is understandable. I wrote it long time ago, and just recently published it to GitHub.

I also planned on pushing my Game Framework which will reduce a lot of boilerplate code, but then before I publish it on Github, I have to refactor it.

Still, the Snake Game can be a good beginning, and you can use various ideas from there.

Also check out Processing Language, which is Java based, and lets you create simple games easily.

There's a popular Java gaming Library too, called LibGDX which you can try it out. It handles lot of boilerplate code for the programmer already, but then has a simple learning curve.

Aman Agnihotri
  • 2,973
  • 1
  • 18
  • 22
  • Thank you for the code, found many interesting things and had to search about the synchronize flag, didn't knew that would be so used ^^ – user3343618 Feb 26 '14 at 14:33
  • Glad you found it useful. Soon I'll put my Game Framework there, and will inform you so that you can use that as well and learn from it. :) – Aman Agnihotri Feb 26 '14 at 14:44
0

"i guess that would maybe be better the use of a jpanel and override the paint method?"

YES. Ditch Canvas go for JPanel

"And if i plan to add, let's say, diferent panels to the menu or so (of the game) can be done with the individual painting of different panels"

Use a CardLayout to switch between JPanel. See How to use CardLayout and see a CardLayout example here

"Another thing is taht i've been said that the variables are for the methods and have to use getters and setters to get them, but so far i'm seeing all as static to catch them from outside, is this right to be done?"

Don't overuse use static unnecessarily for communication between classes. Make use of getters and setters (research encapsulation); Make use of passing by reference; Make use of Interfaces; For more advanced features look into MVC design patterns and observer patterns.

"Also know that it should be have a permanent loop for it in the run method, i understand that, but i've seen different thoughs about the use of timers for triggering events."

javax.swing.Timers are the way to go (not java.util.Timer). See How to use Swing Timers. Also an example can be seen here and here and here and here

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720