开发者

Cannot get image to move where I want it to (and update continuously)?

开发者 https://www.devze.com 2023-02-15 07:08 出处:网络
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'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 开发者_StackOverflow社区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?


  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?

    • http://www.javalobby.org/java/forums/t19387.html
    • 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号