2

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?

Fids
  • 151
  • 1
  • 3
  • 10
  • 1
    Consider creating and posting an [SSCCE](http://sscce.org) (please click on the link), a small compilable, runnable program that demonstrates your best attempt at solving this. Then we can inspect your code, run it, modify it and best be able to help you fix it. – Hovercraft Full Of Eels Mar 06 '11 at 15:41

1 Answers1

2
  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?

Community
  • 1
  • 1
no.good.at.coding
  • 20,221
  • 2
  • 60
  • 51