title: Java Graphics how to draw image in keyPressed
body: <p>I am learning something new and somewhat difficult in Java-which is graphics! Below I explain what the two classes do. My new obstacle right now is coming up with a way to draw a different image for (ie: a projectile like a laser) coming from the ball by only pressing Z. </p>
  
  <p>The problem is if I write a method for example: "g.drawImage(laser,laser_dx,laser_dy,this) in the if statement that contains "KeyEvent.VK_Z", my keyPressed method all of sudden says "this method is not used locally". What are my approaches to solving such an obstacle?</p>
  
  <p>What I've done so far is written a nested class inside the "GameBoard" class that contains all the keyboard events of my program.</p>
  
  <pre><code>       private class Adapter extends KeyAdapter
         {
  
                  public void keyPressed(KeyEvent e)
              {
                  int keyCode = e.getKeyCode();
                  if(keyCode == KeyEvent.VK_RIGHT)
                  {
                      ball_dx += ball_velocity;
  
                  }
                  if(keyCode == KeyEvent.VK_LEFT)
                  {
                      ball_dx -= ball_velocity;
                  }
                  if(keyCode == KeyEvent.VK_Z){
  
                  }
  
              }
  
          }
  </code></pre>
  
  <p>Here's the drawing graphics method in a separate class called "Gameboard": This class just draws the image of a green ball(which is a .png image) and it can move left and right with the arrow keys!</p>
  
  <pre><code>    public class GameBoard extends JPanel implements ActionListener
      {
  
          public void paintComponent(Graphics g)
              {
  
              super.paintComponent(g);
  
              g.drawImage(ball, ball_dx, ball_dy, this);
  
              Toolkit.getDefaultToolkit().sync();
  
          }
  
          public void actionPerformed(ActionEvent arg0) 
          {
  
              repaint();
          }
  
      }
  </code></pre>