title: Swing simple overlay
body: <p>I'm in the process of making a 2D game in which a player roams around a maze. 
  <a href="https://i.stack.imgur.com/nNipz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nNipz.png" alt="screenshot of the GUI"></a></p>
  
  <p>I want to implement some sort of "darkness", even something as simple as a transparent shape around the player surrounded by black, like this:
  <a href="https://i.stack.imgur.com/BjOGT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BjOGT.png" alt="mock-up screenshot"></a></p>
  
  <p>The problem I've found using Swing is that, while this is possible, it means having to redraw everything, which produces an annoying "flickering" effect every time it happens. Is there a way to make some sort of overlay, or just a good way of doing this in general in Swing? I'm not very experienced with GUI/visual stuff right now so I'd like to stick with Swing if possible.</p>
  
  <p>EDIT: This is my method to paint the background, i.e. the floor, walls and exit:</p>
  
  <pre><code>    public final void paintBG(Graphics g){
      g.setColor(Color.LIGHT_GRAY); // Screen background
      g.fillRect(0, 0, getWidth(), getHeight());
      // Draw the Walls of the maze
      // scalex and y are for scaling images/walls within the maze since I let users specify how big they want the maze
      for (int j = 0; j &lt; this.height; j++, y += scaley) {
          x = 20;
          for (int i = 0; i &lt; this.width; i++, x += scalex) {
              if (!(maze[j][i].northwall.isBroken())) // If the north wall isn't broken
              {
                  g.drawImage(walltile, x, y, scalex, scaley / 5, null); // Draw a wall there (image, xpos, ypos, width, height, observer)
              }
              if (!(maze[j][i].eastwall.isBroken())) // etc
              {
                  g.drawImage(walltile, x + scalex, y, scalex / 5, scaley, null);
              }
              if (!(maze[j][i].southwall.isBroken())) {
                  g.drawImage(walltile, x, y + scaley, scalex, scaley / 5, null);
              }
              if (!(maze[j][i].westwall.isBroken())) {
                  g.drawImage(walltile, x, y, scalex / 5, scaley, null);
              }
  
              if ((j == mazeinfo.getTargetM()) &amp;&amp; (i == mazeinfo.getTargetN())) {
                  // Draw the exit
                  g.drawImage(jeep, x + (scalex / 2), y + (scaley / 2), cx, cy, null);
                  g.setColor(Color.LIGHT_GRAY);
                  if (maze[j][i].northwall.isEdge()) {
                      // Paint over the edge creating a 'way out'
                      g.fillRect(x, y, scalex, scaley / 4);
                  } else if (maze[j][i].eastwall.isEdge()) {
                      g.fillRect(x + scalex, y, scalex / 4, scaley);
                  } else if (maze[j][i].southwall.isEdge()) {
                      g.fillRect(x, y + scaley, scalex, scaley / 4);
                  } else if (maze[j][i].westwall.isEdge()) {
                      g.fillRect(x, y, scalex / 4, scaley);
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>I then have "paintPlayer" and "paintEnemy" methods to paint those sprites each time they move. The background only gets painted once, at the start.</p>