title: Java graphics - repaint an image without removing another
body: <p>I am using Graphics g to draw an image for my player and a map. Whenever the player moves I want to update the player image to the new location without updating the map image. How can I do this?</p>
  
  <p>Thanks in advance for any replies!</p>
  
  <p>Sorry about my messy code.</p>
  
  <pre><code>   Main:
  package Main;
  import java.awt.Dimension;
  
  import javax.swing.JFrame;
  
  public class Main {
      public static JFrame frame;
  
      static int size = 32;
      static int width = size*20;
      static int height = size*17;
  
      static playerObj p = new playerObj(200,200);
  
      static tileMap grid = new tileMap();
  
      public static void main(String[] args) {
          frame = new JFrame("Game");
          frame.getContentPane().setPreferredSize(new Dimension(width-9, height-9));
          frame.pack();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setResizable(false);
          frame.add(new drawGraphics());
          frame.setVisible(true);
      }
  }
  
  drawGraphics:
  package Main;
  
  import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import java.awt.image.BufferedImage;
  import java.io.File;
  import java.io.IOException;
  
  import javax.imageio.ImageIO;
  import javax.swing.JPanel;
  
  public class drawGraphics extends JPanel implements Runnable {
      private static final long serialVersionUID = 1L;
  
      boolean running = true;
      boolean renderMap = true;
  
      playerObj p = Main.p;
      int size = Main.size;
      static int x = 0;
      static int y = 0;
  
      Thread thread = new Thread(this);
  
      public drawGraphics() {
          setFocusable(true);
          addKeyListener(new controls());
          start();
      }
  
      public void start() {
          thread.start();
      }
  
      public void paint(Graphics g) {
          Graphics2D d = (Graphics2D) g;
          super.paintComponent(g);
  
          if (renderMap) {
              map(d);
          }
  
          player(g);
  
          repaint();
      }
  
      public void player(Graphics g) {
          try {
              BufferedImage pImg = ImageIO.read(new File("images/player.png"));
              g.drawImage(pImg, p.getX(), p.getY(), null);
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      public void map(Graphics g) {
          System.out.println("2dfsdf");
          Graphics2D d = (Graphics2D) g;
          for (int i = 0; i &lt; tileMap.map.length; i++) {
              for (int j = 0; j &lt; tileMap.map[i].length; j++) {
                  d.setColor(tileMap.map[i][j].getC());
                  d.fillRect(tileMap.map[i][j].getX(), tileMap.map[i][j].getY(),
                          Main.size, Main.size);
                  if (tileMap.map[i][j].solid()) {
                      d.setColor(Color.BLACK);
                      d.drawRect(tileMap.map[i][j].getX(),
                              tileMap.map[i][j].getY(), Main.size, Main.size);
                  }
              }
          }
      }
  
      public void move() {
          if (controls.up) {
              controls.goUp();
              y--;
              repaint();
          }
          if (controls.down) {
              controls.goDown();
              y++;
              repaint();
          }
          if (controls.left) {
              controls.goLeft();
              x--;
              repaint();
          }
          if (controls.right) {
              controls.goRight();
              x++;
              repaint();
          }
          if (controls.place) {
              repaint();
          }
      }
  
      public void run() {
          while (running == true) {
              move();
              try {
                  Thread.sleep(10);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>