Note that there are some explanatory texts on larger screens.

plurals
  1. POAffineTransform doesn't move image on key presses
    primarykey
    data
    text
    <p>I'm having a bit of trouble developing my first Java game using images instead of vector graphics. Currently I'm experimenting with moving an Image on a key press with the <code>AffineTransform</code> object. Only it won't move. What am I doing wrong? I haven't tried using threads yet, but I am not sure how to do this. If you think it would help, please include it in your answer.</p> <p>Here is my code:</p> <pre><code>import java.awt.*; import java.net.*; import java.awt.event.*; import java.awt.geom.*; import java.applet.*; public class DisplayImage extends Applet implements KeyListener{ AffineTransform identity = new AffineTransform(); boolean left = false; boolean right = false; public URL getURL(String filename) { URL url = null; try { url = this.getClass().getResource(filename); } catch(Exception e){e.printStackTrace();} return url; } private Image image; public void init() { image = getImage(getURL("spaceship.png")); addKeyListener(this); } public void update(Graphics g){paint(g);} public void paint(Graphics g) { AffineTransform trans = new AffineTransform(); trans.setTransform(identity); Graphics2D g2d = (Graphics2D)g; g2d.setColor(Color.BLACK); g2d.fillRect(0,0,getSize().width,getSize().height); if(left==true) { trans.translate(-10,0); left = false; repaint(); } if(right==true) { trans.translate(10,0); right = false; repaint(); } g2d.drawImage(image,0,0,this); } public void keyPressed(KeyEvent e) { int keycode = e.getKeyCode(); switch(keycode) { case KeyEvent.VK_RIGHT: right = true; repaint(); case KeyEvent.VK_LEFT: left = true; repaint(); } } public void keyTyped(KeyEvent e){} public void keyReleased(KeyEvent e){} } </code></pre> <p>Can someone please comment or answer? I feel a "tumbleweed" badge heading my way. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload