无法将图像移动到我想要的位置(并持续更新)?
我基本上正在编写一个简单的游戏引擎,但我遇到了问题,我的精灵/图像在应该出现的时候没有出现......或者根本没有出现!
我会尽力让这个过程尽可能简单。我有一个 Sprite、GameEngine 和 Display 类。在游戏循环中,我有一个方法可以设置 Sprite 的新位置(因此它只设置 x 和 y 变量)。接下来,我调用一个转换方法,该方法执行以下操作:
public void transform() {
affineTransform.setToIdentity();
affineTransform.translate(x, y);
}
接下来,我在 Sprite 中调用一个绘制方法:
public void draw() {
graphics2D.drawImage(image, affineTransform, jFrame);
}
最后,在我的线程中,我对 JFrame(Display 类)调用 repaint()。我对该类的绘制方法如下:
public void paint(Graphics g) {
g.drawImage(backbuffer, insets.left, insets.top, this);
}
但是除了黑屏之外什么也没有出现!
我也对 Graphics g 和 Graphics2D 以及何时使用其中之一感到困惑。 (重写的绘制方法使用 Graphics g)。作为记录,我在类中确实有一个 Graphics2D 变量,该变量是通过调用 backbuffer.createGraphics(); 创建的。
另一件让我困惑的事情是这个 AffineTransform...我已经阅读了文档,但我仍然完全困惑如何以及何时使用它 - 以及它到底做什么。有没有比较简单的解释?
当然这应该有效......我在这里错过了什么吗?
I'm Basically programming a simple game engine but I'm having problems with my sprites/images not appearing when they should... or at all!
I'll try and keep this as simple as possible. I have a Sprite, GameEngine and Display class. In the gameloop, I have a method that sets the new position of my Sprite (so it just sets the x and y variables). Next I call a transform method which does the following:
public void transform() {
affineTransform.setToIdentity();
affineTransform.translate(x, y);
}
Following that, I then call a draw method in the Sprite:
public void draw() {
graphics2D.drawImage(image, affineTransform, jFrame);
}
Finally, in my thread I then call repaint() on the JFrame (the Display class). My paint method for that class is as follows:
public void paint(Graphics g) {
g.drawImage(backbuffer, insets.left, insets.top, this);
}
But nothing is appearing, apart from a black screen!
I'm also getting confused between Graphics g, and Graphics2D and when to use either. (The overridden paint method uses Graphics g). For the record, I do have a Graphics2D variable in the class that is created by calling backbuffer.createGraphics();
Another thing that is confusing me is this AffineTransform... I've read the documentation but I'm still utterly confused on how and when to use it - and what exactly it seems to do. Is there any explanation in relatively simple terms?
Surely this should be working... am I missing something out here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回答部分问题:
来自 Graphics2D JavaDoc
这个 Graphics2D 类扩展了 Graphics 类,以提供对几何图形、坐标转换、颜色管理和文本布局的更复杂的控制。这是在 Java(tm) 平台上渲染二维形状、文本和图像的基本类。
本质上,使用
Graphics2D
可以比使用Graphics
做更多的事情。对于 Sun JVM 1.5+,它应该是安全的将paint()
中获得的Graphics
对象转换为Graphics2D
我刚刚注意到这一点:For记录中,我在类中确实有一个 Graphics2D 变量,它是通过调用 backbuffer.createGraphics(); 创建的;
您应该确保您不是在
Graphics[2D]
canvas 上绘制(我将使用这个术语来指代Graphics[2D]
上的可绘制区域>Graphics[2D] 对象提供),您稍后将其丢弃。如果您在单独的画布上绘制图像,则应确保将该图像绘制到实际的显示画布上。我对 AffineTransform 并没有很好的解释,但也许这些会有帮助?
https://secure.wikimedia.org/wikipedia/en/维基/Affine_transformation
来自维基百科 - 一般来说,仿射变换由线性变换(旋转、缩放或剪切)和平移(或“移位”)组成。多个线性变换可以组合成一个线性变换。基本上,您使用此类来执行旋转、平移、缩放等操作。
To answer part of your question:
From the Graphics2D JavaDoc
This Graphics2D class extends the Graphics class to provide more sophisticated control over geometry, coordinate transformations, color management, and text layout. This is the fundamental class for rendering 2-dimensional shapes, text and images on the Java(tm) platform.
Essentially, with
Graphics2D
you can do much more than you can withGraphics
. And with a Sun JVM 1.5+, it should be safe to cast theGraphics
object you get inpaint()
toGraphics2D
I just noticed this: For the record, I do have a Graphics2D variable in the class that is created by calling backbuffer.createGraphics();
You should make sure you're not drawing on a
Graphics[2D]
canvas (I'll use this term to refer to the drawable area that theGraphics[2D]
object provides) that you later throw away. If you're drawing your image on a separate canvas, you should ensure that you then draw that image onto your actual display canvas.I don't really have a good explanation of
AffineTransform
but maybe these will help?https://secure.wikimedia.org/wikipedia/en/wiki/Affine_transformation
From Wikipedia - In general, an affine transformation is composed of linear transformations (rotation, scaling or shear) and a translation (or "shift"). Several linear transformations can be combined into a single one. Basically, you use this class to perform operations such as rotation, translation, zoom etc.