title: Object rotation and movement in current facing direction; Java
body: <p>So I am trying to build a top down space shooter in java. </p>
  
  <p>I am fairly new but I have a good grasp on it having perfected making pong. My main problem is that I can't seem to get rotation down. </p>
  
  <p>I want to be able to press right or left on the arrow keys and have, say a basic rectangle, rotate around its center in the direction the user is inputting. </p>
  
  <p>Once I have that down I want to be able to press forward or backward on the arrow keys to make it go in the direction it is currently facing. </p>
  
  <p>Everything I have tried so far affected everything in the frame instead of just a singular object. Having come from as2, rotation and objects are very different in java. </p>
  
  <p>Can someone help explain to me how I would do this?</p>
  
  <pre><code>package spaceroyalgame;
  
  import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import java.awt.Rectangle;
  import java.awt.RenderingHints;
  import java.awt.event.KeyEvent;
  import java.awt.event.KeyListener;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import java.awt.geom.AffineTransform;
  
  
  
  public class SpaceRoyalGame extends JPanel implements KeyListener {
          static double shipRotation=0, shipRotationSpeed=0, shipSpeed=0, TOPSPEED= 20;
  
          static int shipy=50, shipx=50, shipWidth = 10, shipHeight = 20;
          static int WorldWidth = 700, WorldHeight = 700;
          static boolean slowdown = true;
  
      public static void main(String[] args) throws InterruptedException {
          SpaceRoyalGame game = new SpaceRoyalGame();
  
          JFrame frame = new JFrame();
          frame.add(game);
          frame.setSize(WorldWidth,WorldHeight);
          frame.setVisible(true);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.addKeyListener(game);
  
  
          while(true){
              game.moveShip();
  
              game.repaint();
  
              Thread.sleep(10);
          }
  
      }
  
  
  
      public void moveShip(){
          if (shipSpeed != 0) {
              shipx += shipSpeed;
          }
          if (shipRotationSpeed != 0) {
              shipRotation += shipRotationSpeed;
          }
      }
  
  
      public void paint (Graphics g){
          super.paint(g);
          setBackground(Color.black);
          Graphics2D g2d = (Graphics2D) g;
          g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                  RenderingHints.VALUE_ANTIALIAS_ON);
          g.setColor(Color.white);
          Rectangle ship = new Rectangle(shipx,shipy,100,50);
  
          g2d.rotate(Math.toRadians(shipRotation));
          g2d.draw(ship);
          g2d.fill(ship);
  
  
      }
  
      @Override
      public void keyPressed(KeyEvent e) {
          int keyCode = e.getKeyCode();
          slowdown = false;
  
          if (keyCode == KeyEvent.VK_W) {
              //System.out.println("W is pressed");
              shipSpeed = -2;
  
          }
          if (keyCode == KeyEvent.VK_S) {
              //System.out.println("S is pressed");
              shipSpeed = 2;
          }
          if (keyCode == KeyEvent.VK_A) {
              System.out.println("A is pressed");
              shipRotationSpeed = 1;
              shipRotation += shipRotationSpeed * (shipSpeed/TOPSPEED);
  
          }
          if (keyCode == KeyEvent.VK_D) {
              System.out.println("D is pressed");
              shipRotationSpeed = -1;
              shipRotation += shipRotationSpeed * (shipSpeed/TOPSPEED);
  
          }
      }
  
      @Override
      public void keyTyped(KeyEvent e) {
  
      }
  
      @Override
      public void keyReleased(KeyEvent e) {
          shipRotationSpeed = 0;
  
          shipSpeed = 0;
  
      }
  
  }
  </code></pre>