- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试创建一个与此类似的游戏 tutorial ,您可以在其中找到源代码。 GameFrame 内有 GamePanel。 GamePanel 内有 StatsPanel 和 PlayPanel。 GameFrame 和 GamePanel 的尺寸为 1280 x 640,StatsPanel 的尺寸为 1280 x 40,位于 GamePanel 的顶部,PlayPanel 的尺寸为 1280 x 600,位于 GamePanel 的底部。
但是,我希望使游戏全屏,但我不希望更改坐标系,因为不同的屏幕尺寸会存在差异。所以这是我的问题:如何拉伸(stretch)游戏以适应屏幕尺寸并保持坐标系引用?
我相信可以将游戏渲染为图像,然后缩放以适合屏幕。通过这种方式,我仍然可以引用 1280 x 640 边界框中的组件。我不知道在哪里以及如何实现这一点。
到目前为止我所尝试过的(引用教程的源代码):
GameFrame(扩展 JFrame)
构造函数:
this.setLocation((int)((Toolkit.getDefaultToolkit().getScreenSize().getWidth()-WIDTH)/2),
((int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight()-HEIGHT)/2));
this.setSize(WIDTH,HEIGHT);
替换为:
this.setLocation(0,0);
this.setSize(Toolkit.getDefaultToolkit().getScreenSize());
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setUndecorated(true);
GameManager(扩展线程)
run()方法:
gamePanel.repaintGame();
替换为:
gamePanel.repaint();
GamePanel(扩展 JPanel)
repaintGame() 方法:
public void repaintGame(){
playPanel.repaint();
}
替换为paintComponent()方法:
public void paintComponent(Graphics g)
{
super.paintComponent(g);
playPanel.repaint();
BufferedImage img = new BufferedImage(1280, 640, BufferedImage.TYPE_INT_RGB);
g.drawImage(img, 0, 0, getWidth(), getHeight(), 0, 0, 1280, 640, null);
}
这些更改并未拉伸(stretch)游戏以适应屏幕尺寸。虽然 GameFrame 和黑色 GamePanel 跨越了屏幕尺寸,但 StatsPanel 和 PlayPanel 似乎仍保留在 1280 x 640 边界框中。
我在 Java 方面不太有经验,所以请原谅我可能做出的任何疏忽。
编辑:更改后的图像(忽略终端窗口):
正如对 c0der 答案的评论:
My main issue is to maintain the coordinate system. For example, point (500, 300) is different in a 1920 by 1080 screen than its location in a 1280 by 640 screen. As such, I was wondering whether it would be possible to stretch GamePanel, along with its components StatsPanel and PlayPanel, across a full screen GameFrame as an image or some other means. That way, the point (500, 300) will always refer to the (500, 300) within GamePanel, and GamePanel will subsequently be stretched across the screen. I've uploaded a picture to my post. I wish to stretch the game to the screen size.
最佳答案
假设这是布局的良好表示:
import java.awt.Color;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GameFrame extends JFrame {
public static final int WIDTH=1280;
public static final int HEIGHT=640;
public GameFrame(GamePanel gamePanel){
//set frame to appear at the center of the screen
this.setLocation((int)((Toolkit.getDefaultToolkit().getScreenSize().getWidth()-WIDTH)/2),
((int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight()-HEIGHT)/2));
this.setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
this.add(gamePanel);
setVisible(true);
}
public static void main(String[] args) {
new GameFrame(new GamePanel());
}
}
class GamePanel extends JPanel{
public static final int PLAY_PANEL_HEIGHT=640;
public static final int STATS_HEIGHT=40;
public GamePanel(){
this.setSize(WIDTH, HEIGHT);
setLayout(null);
setBackground(Color.BLACK);
JPanel statsPanel= new JPanel();
statsPanel.setSize(GameFrame.WIDTH, STATS_HEIGHT);
statsPanel.setLayout(null);
statsPanel.setBackground(Color.CYAN);
statsPanel.setLocation(0, 0);
this.add(statsPanel);
JPanel playPanel=new JPanel();
playPanel.setSize(GameFrame.WIDTH, PLAY_PANEL_HEIGHT);
playPanel.setLayout(null);
playPanel.setBackground(Color.DARK_GRAY);
playPanel.setLocation(0, STATS_HEIGHT);
this.add(playPanel);
}
}
按照评论中的说明进行更改以使其全屏显示:
public class GameFrame extends JFrame {
public GameFrame(GamePanel gamePanel){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
this.add(gamePanel);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setUndecorated(true);
setVisible(true); //make sure you have means to close the window
}
public static void main(String[] args) {
new GameFrame(new GamePanel());
}
}
class GamePanel extends JPanel{
public static final int STATS_HEIGHT=40;
public GamePanel(){
//using null layout manager requires that you manually handle all size or
//other layout changes. a much better practice is to use layout manager
setLayout(new BorderLayout());//use BorderLayout instead of null
setBackground(Color.BLACK);
JPanel statsPanel= new JPanel();
//width is ignored for NORTH components of BorderLyayout
statsPanel.setPreferredSize(new Dimension(0, STATS_HEIGHT));
statsPanel.setLayout(null);
statsPanel.setBackground(Color.CYAN);
this.add(statsPanel, BorderLayout.NORTH);
JPanel playPanel=new JPanel();
//height is ignored for CENTER components of BorderLyayout
playPanel.setPreferredSize(new Dimension(WIDTH, 0));
playPanel.setBackground(Color.DARK_GRAY);
this.add(playPanel, BorderLayout.CENTER);
}
}
编辑
拉伸(stretch)图像意味着要么使每个像素变大(这是您无法控制的),要么在您的情况下添加像素。
添加像素显然会将 (500,300)
坐标(作为示例)更改为其他值。
我看到两个选择:
a.无论帧大小如何,都将 GamePanel 保持固定大小。
b.根据实际大小动态计算坐标,而不是使用像(500,300)
这样的固定值。
我会推荐选项b。
关于java - 拉伸(stretch)游戏以适应屏幕尺寸并保持坐标系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44212280/
好吧,这是最好用图像来解释的事情...... 我正在寻找一个类似于 StretchBlt 的函数,但我可以将图像复制到定义目标四个角的 Canvas 上,即将图像的梯形/四边形拉伸(stretch)绘
我为 UITextField 制作了一个自定义键盘,其中包含 18 个 UIButton。它是一个 UIView,被设置为文本字段的 inputView。 按钮是在 UIView 的 initWith
我想创建一个 UIButton,它使用可拉伸(stretch)图像作为背景图像,例如我可以轻松调整按钮大小以适应不同的标签等。 所以我创建了以下运行良好的代码: UIImage *bgImage =
我正在尝试拉伸(stretch) UIImageView 中的图像 - 但我惨败了:) 以下设置: 带有 View 和附加到该 View 的 UIImageView 的 NIB 文件。 使用IBOut
我一直在尝试使用 html Canvas ,但由于某种原因,很难让它不拉伸(stretch)东西。我试过只使用 screen.width 和 screen.height,但我通过使用 screen.a
我想拉伸(stretch)上图的左右两侧,并保持中间的箭头不拉伸(stretch)。我怎样才能做到这一点? 最佳答案 如果你不喜欢打扰 Photoshop 并且图像不需要动态调整大小,你可以使用我在
我的页脚在一个容器中有四列。它需要在容器内以与上面的内容对齐。 我的问题是我想让左栏有红色背景,但目前它不会拉伸(stretch),因为它显然在容器中。 我怎样才能将它向左拉伸(stretch)整个宽
有没有办法拉伸(stretch)按钮,使中央部分保持完整? 9-patch 不允许这样做。 编辑:我按照 Benito 的建议使用了 9-patch。它工作正常。奇怪的是,我第一次无法得到它。 最佳答
我想拉伸(stretch)上图的左右两边,中间的箭头不拉伸(stretch)。我该怎么做? 最佳答案 如果你不想打扰 Photoshop 并且如果图像不需要动态调整大小,你可以在 UIImage 上使
我创建了一个 9patch 图像,不知何故它只能垂直拉伸(stretch)。我尝试了其他 9-patch 图像,但它们具有相同的效果,为什么它们在其他情况下工作。所以我认为 9patch 应该没问题。
我需要在边框控件(或类似控件)中绘制一些简单的线条,这些线条始终延伸到边框的边界。有没有办法只拉伸(stretch)线条而不是它的笔?不涉及大量 C#? 在这个版本中,线条延伸:
这是我的 XAML 及其外观:
编辑:答案将允许背景图像根据 body 的大小改变它的高度。如果正文是 500px 高,它应该是 100% 宽度,500px 高度。或 100% 宽度 2500 像素高度。 也许我错过了这件事,但我正
我有一个 UILabel。我设置了尾随和前导空间约束,还添加了对齐 Y 中心约束。我已将行数设置为零。 我的目标是将 UILabel 拉伸(stretch)到某个最大宽度,然后添加新行。在给定的约束条
我正在制作一个网站,对于该网站,我正在使用顶部的导航栏。我想要的是将栏放在页面的顶部,并且它是主页的单独颜色。这是我当前的代码: body { font-family: "Baloo Bhaina
我编写了代码来 reshape 已纹理化的四边形。当我拉伸(stretch)四边形时,图像确实按照我的预期拉伸(stretch)了。我似乎在拉伸(stretch)图像时有两个不同的三角形,图像被拉伸(
我在 div 中嵌入了 YouTube。此 div .entry-content 的 max-width 为 30em。 YouTube 嵌入我想要全窗口宽度,延伸到 #container 之外。为此
看: //UIImageView* stretchTest = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]
我有一个 super 简单的网页,由纯 HTML/CSS 组成。正如预期的那样,当我在本地运行它时,一切正常。 但是,当我部署它时(使用 Droppages,一个通过 Dropbox 的静态页面托管平
我正在尝试将一个 9 色 block 图像设置为我的一项 Activity 的背景,但它不必要地拉伸(stretch),使一切变得奇怪。我使用 draw9patch 创建它,并在左下角添加了一个像素,
我是一名优秀的程序员,十分优秀!