Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating my first game

 
Andy Joness
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I've decided to try and create a simple 2D game in Java to learn about graphics and animation etc. However first I need some tips on the interface design.

What is the simplest/cleanest way of creating "pages"? I can't decide between jFrames (just setting visibility) and CardLayout. Is there a better way?

Secondly I am going to run it in fullscreen. How would I go about getting the frames/panels to automatically resize to the specified resolution? I plan on having a settings page with several resolutions available. The page contents(controls) need to automatically position themselves relative to the borders.

Lastly my method of using background images on frames involves instantiating a frame using this code:


Is this good, or is there a more efficient way? Cheers
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Joness wrote:What is the simplest/cleanest way of creating "pages"? I can't decide between jFrames (just setting visibility) and CardLayout. Is there a better way?

I wouldn't use JFrames. The user will notice the current frame disappearing and a new one reappearing. CardLayout is designed for this type of work, where you replace (a part of) the current user interface with something else.

Secondly I am going to run it in fullscreen. How would I go about getting the frames/panels to automatically resize to the specified resolution? I plan on having a settings page with several resolutions available. The page contents(controls) need to automatically position themselves relative to the borders.

Use GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(frame) to set the the JFrame called "frame" as the fullscreen window on your default screen.

Lastly my method of using background images on frames involves instantiating a frame using this code:


Is this good, or is there a more efficient way? Cheers

Looks good, except for the creation of the image. That piece of code won't compile. Change it into this instead:


I'll move this thread to our gaming forum.
 
Andy Joness
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I've got full screen working now. The problem I have now is when I change the resolution, the position of the swing components change. I need a way to make sure they stay in a certain position relative to the screen size. BoxLayout seems to automatically do this, however this limits where on the panel I can place things.
 
Seabook Chen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Joness wrote:Thanks, I've got full screen working now. The problem I have now is when I change the resolution, the position of the swing components change. I need a way to make sure they stay in a certain position relative to the screen size. BoxLayout seems to automatically do this, however this limits where on the panel I can place things.


You could use WindowToolKit, I can't remember the exact name, just give a google. You got what you need.
 
Seabook Chen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought it was not a completed answer, so I checked the API.
And you can use the following code to get the screen size. From my experince, gaming needs lots of position calculation. So good luck.

 
Andy Joness
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the interface set up and I've created a settings screen which allows the choice of fullscreen or windowed, and automatically sets the fullscreen resolution to the users' native resolution.

However I'm having real trouble with the layout manager in Netbeans, it's horrible to work with. When I change between 1680x1050 down to 1280x800 the buttons/labels/panels etc move position. If there were a layout which allowed for relative position, like in web design, it would be so much easier. Grid Bag Layout looks promising, however once again Netbeans makes it so much hassle to customise.
 
Andy Joness
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edit: Double post
 
Andy Joness
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've come up with a clunky way of manually positioning everything so it stays in the same place regardless of resolution or if it's windowed.

I'm sure it's definitely not the best way of doing this, however I despise the layout manager and I need to crack on with things, so it'll do unless anyone can offer some insight on a better method. I've been searching Google for hours with no luck.



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic