title: In Java, why doesn't frame.repaint work in this code?
body: <p>I'm not sure why the <code>fr.repaint();</code> method isn't working here. If I use the set visible <code>false</code>, then set it <code>true</code> after making changes it makes the screen go black which isn't what I'm trying to do.</p>
  <p>Any help?</p>
  <p>This code is for a school project, so if it's possible please provide somewhat detailed explanation to understand.</p>
  <pre><code>import javax.swing.JFrame;
  import javax.swing.JPanel;
  import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.event.*;
  
  class Main {
      static int startingposx;
      static int startingposy;
  
      static int endingposx;
      static int endingposy;
  
      public static void main(String[] args) {
          JFrame fr = new JFrame();
          fr.setBounds(10,10,512,512);
          
          fr.addMouseMotionListener(new MouseMotionListener() {
              @Override
              public void mouseDragged(MouseEvent e) {
              }
              @Override
              public void mouseMoved(MouseEvent e) {
              }
          });
  
          fr.addMouseListener(new MouseListener() {
              @Override
              public void mouseClicked(MouseEvent e) {
                  
                  
              }
  
              @Override
              public void mousePressed(MouseEvent e) {
  
                  startingposx = e.getX();
                  startingposy = e.getY();
              }
  
              @Override
              public void mouseReleased(MouseEvent e) {
  
                  endingposx = e.getX();
                  endingposy = e.getY();
                  //fr.setVisible(false);// i tried to use this to refresh it but it makes the frame black for a second
                  
                  JPanel x = new JPanel(){
                      @Override
                      public void paint(Graphics g){
                              g.setColor(Color.RED);
                              g.fillRect(startingposx-(32-15),startingposy-32,30,30);
                              g.setColor(Color.BLUE);
                              g.fillRect(endingposx-(32-15),endingposy-32,30,30); 
                      }
                  };
                  
                  fr.add(x);
                  fr.repaint();// why does this not work??
                  //fr.setVisible(true);
          
                  
                  System.out.print(&quot;\033[H\033[2J&quot;);
                  System.out.println(&quot;starting pos: &quot; + startingposx+ &quot;,&quot;+ startingposy+&quot;\n&quot; + &quot;ending pos: &quot; + endingposx+ &quot;,&quot; + endingposy);
              }
  
              @Override
              public void mouseEntered(MouseEvent e) {
              }
              
              @Override
              public void mouseExited(MouseEvent e) {
              }
          });
          
          fr.setVisible(true);        
          fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
  }
  </code></pre>