title: How to shoot "bullets" in java swing animation game
body: <p>I have created a simple animation game in which the user can move a circle with the arrow keys. I have drawn a square that 'follows' the center of the circle around. My goal is for there to be an infinite supply of squares (bullets) in the center of the circle (that follow it around), that can shoot left right up down using asdf while moving around. I am trying to figure out how to type code that will detach the bullet from following the circle around once shot. Thanks in advance. I am a beginner so i apologize in advance for the messy code:)</p>
  
  <pre><code>public class Game extends JPanel implements ActionListener, KeyListener
  {
  
      Timer t = new Timer(5, this);
  
      int x, y, velx, vely, movx, movy = 0; 
  
      int shootx, shooty;
  
      int xaim = 0;
  
      int yaim = 0;
  
      int xpts[], ypts[];
  
      int aim;
  
      Polygon bullet;
  
      public Game(){
  
          t.start();
  
          addKeyListener(this);
          setFocusable(true);
          setFocusTraversalKeysEnabled(false);
      }
      public void paintComponent(Graphics g){
  
          Graphics2D g2d = (Graphics2D) g;
          //Background
          g2d.setColor(Color.BLUE);
          g2d.fillRect(0, 0, 600, 600);
          g2d.fillRect(x, y, 30, 30);
          //Borders
          g2d.setColor(Color.GREEN);
          g2d.fillRect(0, 0, 20, 600);
          g2d.fillRect(0, 0, 600, 20);
          g2d.fillRect(580, 0, 20, 600);
          g2d.fillRect(0,560,600,20);
          //Player
          g2d.setColor(Color.RED);
          g2d.fillOval(x, y, 30, 30);
          //Bullet
          g2d.setColor(Color.BLACK);
          g2d.fillRect(xaim+28,yaim+28,15,15);    
      }
      public void actionPerformed(ActionEvent e) {
            checkbounds();
            x += movx;
            y += movy;
            xaim += velx;
            yaim += vely;
  
          repaint();
      }
      public void keyPressed(KeyEvent e) {
            int code = e.getKeyCode();
            if (code == KeyEvent.VK_UP)
            {
                up();
            }
            if (code == KeyEvent.VK_DOWN)
            {
                down();
            }
            if (code == KeyEvent.VK_LEFT)
            {
                left();
            }
            if (code == KeyEvent.VK_RIGHT)
            {
                right();
            }
            if (code == KeyEvent.VK_A)
            {
               //code to shoot left
            }
            if (code == KeyEvent.VK_W)
            {
                //code to shoot up
            }
            if (code == KeyEvent.VK_D)
            {
                //code to shoot right
            }
            if (code == KeyEvent.VK_S)
            {
                //code to shoot down
            }
  
      }
      public void up(){
          movy = -1;
          movx = 0;
          velx = 0;
          vely = -1;
      }
      public void down(){
          movy = 1;
          movx = 0;
          velx = 0;
          vely = 1;
  
      }
      public void right(){
          movx = 1;
          movy = 0;
          velx = 1;
          vely= 0;
  
      }
      public void left(){
          movx = -1;
          movy = 0;
          velx = -1;
          vely = 0;
      }
      public void checkbounds(){
          if(x &lt; 20){
              x = 20;
          }
          if(x &gt; 550){
              x = 550;
          }
          if(y &lt; 20){
              y=20;
          }
          if(y &gt; 530){
              y = 530;
          }
      }
      public void keyTyped(KeyEvent e) {}
      public void keyReleased(KeyEvent e) {}
  
  }
  </code></pre>