title: GUI manipulations for board game
body: <p>I am totally new to java and I want to create a single-player board game in Java. </p>
  
  <p>I have already many classes that define the game, but, for my question, I will limit the question to creating a GUI board for my board game. <br/>
  My very concrete questions are, based on the code below : <br/></p>
  
  <ol>
  <li>How can I move a player on the board game ? (class Player exists)</li>
  <li>Is this the correct way to create a Java board with GUI, are there better alternatives?  </li>
  <li>How can I only set a specific part of the board game as visible ? (and this needs to change when the player moves!) </li>
  </ol>
  
  <p>Thanks in advance for decent answers. Because I am very new to java, please explain your answers in a clear way. </p>
  
  <p>this is the output of the code below: </p>
  
  <p><a href="https://i.stack.imgur.com/e7IRi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/e7IRi.png" alt="output of the code"></a></p>
  
  <pre><code>import javax.swing.SwingUtilities;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.BorderFactory;
  import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.Graphics;
  import java.awt.event.MouseEvent;
  import java.awt.event.MouseListener;
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseMotionListener;
  import java.awt.event.MouseMotionAdapter;
  
  public class SwingPaintDemo3 {
  
      public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  createAndShowGUI(); 
              }
          });
      }
  
      private static void createAndShowGUI() {
          System.out.println("Created GUI on EDT? "+
          SwingUtilities.isEventDispatchThread());
          JFrame f = new JFrame("Single Player Game");
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
          f.add(new MyPanel());
          f.pack();
          f.setVisible(true);
      } 
  }
  
      class MyPanel extends JPanel {
  
  
          private int squareX = 50;
          private int squareY = 50;
          private int squareW = 20;
          private int squareH = 20;
  
          public MyPanel() {
  
              setBorder(BorderFactory.createLineBorder(Color.black));
  
  
     public Dimension getPreferredSize() {
          return new Dimension(250,200);
      }
  
      protected void paintComponent(Graphics g) {
          super.paintComponent(g);       
          g.drawString("This is the game board ",10,20);
          g.setColor(Color.GREEN);
          g.fillRect(squareX,squareY,squareW,squareH);
          g.setColor(Color.BLACK);
          g.drawRect(squareX,squareY,squareW,squareH);
          g.drawRect(squareX+20,squareY,squareW,squareH);
          g.drawRect(squareX+40,squareY,squareW,squareH);
          g.drawRect(squareX+60,squareY,squareW,squareH);
          g.drawRect(squareX+60,squareY,squareW,squareH);
          g.drawRect(squareX+80,squareY,squareW,squareH);
          g.drawRect(squareX,squareY+20,squareW,squareH);
          g.drawRect(squareX+20,squareY+20,squareW,squareH);
          g.drawRect(squareX+40,squareY+20,squareW,squareH);
          g.drawRect(squareX+60,squareY+20,squareW,squareH);
          g.drawRect(squareX+80,squareY+20,squareW,squareH);
          g.drawRect(squareX,squareY+40,squareW,squareH);
          g.drawRect(squareX+20,squareY+40,squareW,squareH);
          g.drawRect(squareX+40,squareY+40,squareW,squareH);
          g.drawRect(squareX+60,squareY+40,squareW,squareH);
          g.drawRect(squareX+80,squareY+40,squareW,squareH);
  
      }  
  } 
  </code></pre>