title: Why my projectile(Image object) is not moving? Java Swing
body: <p>I'm trying to make a demo spaceship shooter. Every time i press the space bar I draw a new projectile(<code>Image</code>) in the <code>paintComponent(Graphics g)</code> method and call a <code>moveProjectile()</code> method. The problem is the <code>moveProjectile()</code> method seems to be off.</p>
  
  <p><strong>MyJPanel.java</strong></p>
  
  <pre><code>import java.awt.Cursor;
  import java.awt.Graphics;
  import java.awt.Image;
  import java.awt.Point;
  import java.awt.Toolkit;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.KeyAdapter;
  import java.awt.event.KeyEvent;
  import java.awt.event.MouseEvent;
  import java.awt.event.MouseMotionListener;
  import java.awt.image.BufferedImage;
  import java.util.ArrayList;
  
  import javax.swing.ImageIcon;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.Timer;
  
  public class MyJPanel extends JPanel implements ActionListener
  {
      private static final long serialVersionUID = 1L;
  
      private Timer timer;
      private Image backgroundImage;
      private Image player;
      private int playerX, playerY;
      private int projectileX,projectileY;
      private Image projectileImage;
      private ArrayList&lt;Image&gt; projectiles = new ArrayList&lt;Image&gt;();
  
      boolean flag = false;
  
      public MyJPanel(Image backgroundImage, Image player,Image projectileImage)
      {
          this.backgroundImage = backgroundImage;
          this.player = player;
          this.projectileImage = projectileImage;
          this.setLayout(null);
  
          timer = new Timer(50, this);
          timer.start();
  
          this.addKeyListener(new KeyAdapter() // Listens for a keyboard event
          {
              public void keyPressed(KeyEvent e) 
              {
                  if (e.getKeyCode() == KeyEvent.VK_SPACE) // If pressing space - shoot
                  {
                      flag = true;
                      moveProjectile();
                  }
                  repaint();
              }
          });
  
          // Mouse listener
          this.addMouseMotionListener(new MouseMotionListener()
          {   
              @Override
              public void mouseMoved(MouseEvent e)
              {
                  playerX = e.getX();
                  playerY = e.getY();
              }
  
              @Override
              public void mouseDragged(MouseEvent e)
              {
  
              }
          });
          hideMouseCursor();
  
          this.setFocusable(true);
          this.setVisible(true);
      } // End of JPanle constructor
  
      public void paintComponent(Graphics graphics)
      {
          super.paintComponent(graphics);
  
          graphics.drawImage(backgroundImage,0,0,this.getWidth(),this.getHeight(),null); // Draw the background
          graphics.drawImage(player,playerX,playerY,null); // Draw the player
          if (flag)
          {
              projectileX = playerX + player.getWidth(null);
              projectileY = playerY + player.getHeight(null) / 2 - 27;
              graphics.drawImage(projectileImage,projectileX,projectileY,null);
          }
      }
  
      public void moveProjectile()
      {
          while (projectileX &lt; this.getWidth())
          {
              this.projectileX += 2;
              repaint();
          }
      }
  
      public void  hideMouseCursor() // Hides the mouse cursor
      {
          //Transparent 16 x 16 pixel cursor image.
          BufferedImage cursorbackgroundImgage = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
  
          // Create a new blank cursor.
          Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(
                  cursorbackgroundImgage, new Point(0, 0), "Blank Cursor");
  
          // Set the blank cursor to the JPanel.
          this.setCursor(blankCursor);
      }
  
      @Override
      public void actionPerformed(ActionEvent actionEvent) // Without the method and the repaint() the mouse listener will not work 
      {
          repaint();
      }
  
      public class ProjectileThread extends Thread
      {
          @Override
          public void run()
          {
              projectileX = playerX + player.getWidth(null);
              projectileY = playerY + player.getHeight(null) / 2;
  
          }
      }
  
      public static void main(String[] args)
      {
          JFrame frame = new JFrame("A Game by me");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setExtendedState(JFrame.MAXIMIZED_BOTH); // Making the frame take a full screen
  
          ImageIcon backgroundImageIcon = new ImageIcon("space_background_2.jpg");
          Image backgroundImgage = backgroundImageIcon.getImage();
          ImageIcon playerImageIcon = new ImageIcon("spaceship_1.png");
          Image playerImage = playerImageIcon.getImage();
          ImageIcon projectileIcon = new ImageIcon("spaceship_projectile_1.png");
          Image projectileImage = projectileIcon.getImage();  
  
          frame.add(new MyJPanel(backgroundImgage,playerImage,projectileImage));
          frame.setVisible(true);
      }
  } // End of MyJPanel
  </code></pre>
  
  <p>There some variables and methods i don't use so don't mind them please. The points to notice in the code:</p>
  
  <pre><code>if (e.getKeyCode() == KeyEvent.VK_SPACE) // If pressing space - shoot
  {
      flag = true;
      moveProjectile();
  }
  repaint();
  </code></pre>
  
  <p>If pressing space -> flag = true which means </p>
  
  <pre><code>if (flag)
  {
      projectileX = playerX + player.getWidth(null);
      projectileY = playerY + player.getHeight(null) / 2 - 27;
      graphics.drawImage(projectileImage,projectileX,projectileY,null);
  }
  </code></pre>
  
  <p>Draw the projectile and move it to the right. The problem is it's never moved to the right. Instead it's following the spaceship which moves by the mouse.</p>
  
  <p>Any suggestions will be very appreciated.</p>
  
  <pre><code>public class ProjectileThread extends Thread
  {
      public ProjectileThread(int playerX,int playerY)
      {
          projectileX = playerX + player.getWidth(null);
          projectileY = playerY + player.getHeight(null) / 2;
      }
      @Override
      public void run()
      {
          while (projectileX &lt; getWidth())
          {
              projectileX += 2;
          }
      }
  }
  
  
  
  public void keyPressed(KeyEvent e) 
          {
              if (e.getKeyCode() == KeyEvent.VK_SPACE) // If pressing space - shoot
              {
                  ProjectileThread projectileThread = new ProjectileThread(playerX,playerY);
                  projectileThread.start();
              }
              repaint();
          }
  </code></pre>