无法将图像移动到我想要的位置(并持续更新)?

发布于 2024-10-20 22:28:52 字数 944 浏览 3 评论 0原文

我基本上正在编写一个简单的游戏引擎,但我遇到了问题,我的精灵/图像在应该出现的时候没有出现......或者根本没有出现!

我会尽力让这个过程尽可能简单。我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断爱 2024-10-27 22:28:52
  1. 回答部分问题:

    来自 Graphics2D JavaDoc
    这个 Graphics2D 类扩展了 Graphics 类,以提供对几何图形、坐标转换、颜色管理和文本布局的更复杂的控制。这是在 Java(tm) 平台上渲染二维形状、文本和图像的基本类。

    本质上,使用 Graphics2D 可以比使用 Graphics 做更多的事情。对于 Sun JVM 1.5+,它应该是安全的将 paint() 中获得的 Graphics 对象转换为 Graphics2D

  2. 我刚刚注意到这一点:For记录中,我在类中确实有一个 Graphics2D 变量,它是通过调用 backbuffer.createGraphics(); 创建的;

    您应该确保您不是在 Graphics[2D] canvas 上绘制(我将使用这个术语来指代 Graphics[2D] 上的可绘制区域>Graphics[2D] 对象提供),您稍后将其丢弃。如果您在单独的画布上绘制图像,则应确保将该图像绘制到实际的显示画布上。

  3. 我对 AffineTransform 并没有很好的解释,但也许这些会有帮助?

  1. 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 with Graphics. And with a Sun JVM 1.5+, it should be safe to cast the Graphics object you get in paint() to Graphics2D

  2. 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 the Graphics[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.

  3. I don't really have a good explanation of AffineTransform but maybe these will help?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文