title: JPanel Black screen because of componentResize and long repaint command
body: <p>The problem is obvious: I have a big paint command with a very large for loop and a componentResized that runs the paint command a huge amount of time, which makes my JPanel a black screen and unable to exit even when I press the x button(I must terminate it on eclipse), represented here:</p>
  
  <pre><code>package Testing;
  
  import java.awt.Graphics;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.ComponentEvent;
  import java.awt.event.ComponentListener;
  
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.Timer;
  
  public class DrawTest extends JPanel implements ComponentListener {
  public DrawTest(){
      this.addComponentListener(this);
  }
      @Override
      public void paint(Graphics g) {
              int gridsize=8;
      double width=getWidth()/gridsize;
      for(double i=0;i&lt;=getWidth();i+=width){
          System.out.println("1");
          g.drawLine((int)i,0,(int)i,getHeight());
      }
  
      double height=getHeight()/gridsize;
      for(double i=0;i&lt;=getHeight();i+=height){
          System.out.println("2");
          g.drawLine(0,(int)i,getWidth(),(int)i);
      }
      }
      public static void main(String[] args){
          JFrame frame=new JFrame("Fill all the squares with Ls");
          DrawTest FillSquare=new DrawTest();
          frame.add(FillSquare);
          frame.setExtendedState( frame.getExtendedState()|frame.MAXIMIZED_BOTH );
          frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
  
      @Override
      public void componentHidden(ComponentEvent e) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void componentMoved(ComponentEvent e) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void componentResized(ComponentEvent e) {
             ActionListener taskPerformer = new ActionListener() {
                  public void actionPerformed(ActionEvent evt) {
                      //...Perform a task...
  
                      System.out.println("Reading SMTP Info.");
                  }
              };
              Timer timer = new Timer(100 ,taskPerformer);
              timer.setRepeats(false);
              timer.start();
          repaint();
  
      }
  
      @Override
      public void componentShown(ComponentEvent e) {
          // TODO Auto-generated method stub
  
      }
  }
  </code></pre>
  
  <p>I googled the problem and looked it up on stackoverthrow, and what came up was to use the Timer which prevent the componentResize to run a crazy amount of times when the user resizes the screen, but after implementing it, as shown in the code, it still doesn't seem to be working. I don't know if I did it wrong or what, it is my first time using it. After playing around and testing with the System.out.println();, I seem to run into another problem. The paint command's second loop seem to be going infinitely after the user moves the frame.<img src="https://i.stack.imgur.com/iBuIi.jpg" alt="enter image description here"></p>
  
  <p>Thanks in advance!</p>