Resize window and move contents within

不问归期 提交于 2019-12-23 17:00:47

问题


I have a simple game I made in java. Essentially, my programming ability is relatively basic, and I would like to be able to resize the window the game is in and not have the images stay where they are and just display whiteness in the larger area. Here's a Screenshot. Is there any simple way to do this?


回答1:


The two existing answers seem to presume you wish for the game space to resize.

Resize the game space

A game space that is a shoot'em'up does not necessarily resize well. E.G. When the game is twice as wide, should the left/right arrow presses move the ship twice as fast? Does stretching the window vertically increase gravity, or the speed of shots?

Pad the game space

The way I read the question, you simply wish to center the game space in the window. E.G.s of that can be seen in this answer.

Fix the size of the top-level container.

Many games fix the size to avoid the types of problems created by the 1st approach, and avoid the 'big blank bands' around the 2nd approach. This is as simple as calling setResizable(false).

For this technique to work best:

  1. Ensure the component doing the game rendering has a preferred size set.
  2. Add it (and all other controls) to the frame.
  3. Call frame.pack() to ensure the frame is the right size to display the content.
  4. (Finally) call frame.setResizable(false).



回答2:


To stretch the game, first render it on to an Image. Then draw the image while stretching it instantly.

Image dbImage; // The backbuffer
graphics.drawImage(dbImage, 0, 0, getWidth(), getHeight());

And most games prefer to disable the window resizing along with my engine.




回答3:


Based on what I understand of your question, you want to "stretch" your game beyond the boundaries and extend it to the rest of the window. (If that's wrong, oops :s)

In order to accomplish this, you have to make some decisions. You can either scale the entire game to the frame, or just extend the background. But depending on what direction you want to move in, you'll have to do different things.

In the end this decision is entirely up to you.

Also what are you using to create the window?



来源:https://stackoverflow.com/questions/12310544/resize-window-and-move-contents-within

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!