0

I'm attempting to make a sort of Space Invaders game in Java, starting with making a PNG spaceship move across the screen. It appears where it's supposed to, only it refuses to move. The program compiles fine and eclipse gives me no errors/warnings. Can anyone tell me what I'm doing wrong?

Here is my code:

import java.awt.*;
import java.net.*;
import java.awt.geom.*;
import java.applet.*;
import java.awt.event.*;

public class DrawImage extends Applet implements KeyListener{
    private static final long serialVersionUID = 1L;
    private Image image;
    private int keycode;
    AffineTransform identity = new AffineTransform();
    private boolean keyleft = false;
    private boolean keyright = false;
    private URL getURL(String filename){
        URL url = null;
        try
        {
            url = this.getClass().getResource(filename);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return url;
    }
    public void init()
    {
        image = getImage(getURL("spaceship.png"));
        addKeyListener(this);
    }

    public void paint(Graphics g)
    {
        Graphics2D g2d = (Graphics2D)g;
        //fill background with black
        AffineTransform trans = new AffineTransform();
        g2d.setColor(Color.GREEN);
        g2d.fillRect(0,0,getSize().width,getSize().height);
        g2d.drawImage(image,320,240,this);
        trans.setTransform(identity);
        if(keyright == true)
        {
            trans.translate(-10,0);
            keyleft = false;
        }
        else if(keyleft == true)
        {
            trans.translate(10,0);
            keyright = false;
        }
    }
    public void keyPressed(KeyEvent e)
    {
        keycode = e.getKeyCode();
        if(keycode == KeyEvent.VK_LEFT)
        {
            keyleft = true;
        }
        else if(keycode == KeyEvent.VK_RIGHT)
        {
            keyright = true;
        }
        repaint();
    }
    public void keyTyped(KeyEvent e){}
    public void keyReleased(KeyEvent e){}

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
imulsion
  • 8,820
  • 20
  • 54
  • 84
  • Is the applet focusable? Does it *have* focus? Consider 1) Swing over AWT for the third millennium. 2) Key bindings over `KeyListener` 3) A `JFrame` rather than a `JApplet` 4) Launching the frame from a link using [Java Web Start](http://stackoverflow.com/tags/java-web-start/info). – Andrew Thompson Jan 06 '13 at 13:15
  • I adapted this from a book - have no idea what focusable means as it was not mentioned. Swing over AWT? What does that mean? And I want it to be an applet so it can go on the web – imulsion Jan 06 '13 at 13:17
  • the book was published in 2009 – imulsion Jan 06 '13 at 13:24
  • What book is this, specifically? My opinion of it drops with each new comment. Really, get rid of it - it will waste a lot of your time. – Andrew Thompson Jan 06 '13 at 13:25
  • Beginning Java game programming second edition by Jonathon S. Harbour. Its actually a really good book and has helped me understand a lot. Why don't u actually answer one of my questions with what u think will make them work for once, rather than constantly criticizing my techniques and resources? – imulsion Jan 06 '13 at 13:30
  • *"what u think will make them work"* Did you miss my first comment? 1) Will produce source using components people have used in recent memory. 2) Will quite possibly solve the focus problem (if it is). 3) That will also help solve the focus problem. 4) Is just a tip on how to launch it. -- Why don't you listen to what I have to offer and actually devote thought & research to it, rather than whine plaintively? *Rather than constantly criticizing my techniques and resources"* If you don't want the best strategies and techniques, pay a consultant to tell you whatever you ***want*** to hear. – Andrew Thompson Jan 06 '13 at 13:43

1 Answers1

1

You should set the transform to your g2d in your paint() method:

g2d.setTransform(trans);

So the whole method then is:

  public void paint(Graphics g)
    {
        Graphics2D g2d = (Graphics2D)g;
        //fill background with black
        AffineTransform trans = new AffineTransform();
        g2d.setColor(Color.GREEN);
        g2d.fillRect(0,0,getSize().width,getSize().height);
        trans.setTransform(identity);
        if(keyright == true)
        {
            trans.translate(-10,0);
            keyleft = false;
        }
        else if(keyleft == true)
        {
            trans.translate(10,0);
            keyright = false;
        }
        g2d.setTransform(trans);
        g2d.drawImage(image,320,240,this);
    }
mpaepper
  • 3,952
  • 3
  • 21
  • 28
  • 1
    Have you actually tried that code? It was well spotted that the transform was never used, but it won't have any effect if set after the drawing operations are done. See [this answer](http://stackoverflow.com/a/14170312/418556) for an example of successfully using (3 concatenated) affine transforms. – Andrew Thompson Jan 06 '13 at 13:34
  • 1
    @AndrewThompson You are right, I updated the code. It should work like this, right? – mpaepper Jan 06 '13 at 13:40
  • Looks better, though I've not tested it either. – Andrew Thompson Jan 06 '13 at 13:44
  • @imulsion Maybe the keyright and keyleft variables do not work. Remove the whole if...elseif... block and then put the translation in hardcoded like: trans.translate(-10,0); so it is applied no matter whether you press a button. Then check whether the image is positioned differently if you comment the translation out. – mpaepper Jan 08 '13 at 17:44