简体   繁体   English

调整窗口大小并移动内容

[英]Resize window and move contents within

I have a simple game I made in java. 我有一个用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? 有没有简单的方法来做到这一点?

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. 一个射击游戏的游戏空间并不一定能很好地调整大小。 EG When the game is twice as wide, should the left/right arrow presses move the ship twice as fast? EG当游戏的宽度是两倍时,左/右箭头按下的速度应该是船的两倍吗? 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. 我读这个问题的方式,你只想把游戏空间集中在窗口中。 EGs of that can be seen in this answer . 其中的EG可以在这个答案中看到。

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) . 这就像调用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. 调用frame.pack()以确保框架的大小适合显示内容。
  4. (Finally) call frame.setResizable(false) . (最后)调用frame.setResizable(false)

To stretch the game, first render it on to an Image . 要拉伸游戏,首先将其渲染到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. 而且大多数游戏都喜欢禁用与我的引擎一起调整大小的窗口。

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) (如果那是错的,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? 还有什么用来创建窗口?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM