title: Variables not changed when in paintComponent() method
body: <p>Lets say I make a simple graphical application. It has two classes, the main class and a second class called "panel" that extends JPanel.
  The main class is just a JFrame that contains the JPanel. Here are its contents:</p>
  
  <pre><code>public static Panel panel = new Panel();
  
  public static void main(String[] args) {
      JPanel p = new Panel();
      JFrame fr = new JFrame();
      fr.setSize(600, 600);
      fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      fr.setLocationRelativeTo(null);
      fr.setVisible(true);
      fr.add(p);
      while (true) {
          fr.repaint();
          if (panel.test) panel.g2d.drawRect(30, 30, 40, 40);
      }
  }
  </code></pre>
  
  <p>The second class uses Graphics2D to create the contents of the JPanel. It also has variables that are changed within the paintComponent() method. Here are its contents:</p>
  
  <pre><code>Graphics2D g2d;
  boolean test = false;
  
  @Override
  public void paintComponent(Graphics g) {
      super.paintComponent(g);
      g2d = (Graphics2D) g;
      test = true;
      g2d.setColor(Color.RED);
      g2d.drawRect(10, 10, 20, 20);
  }
  </code></pre>
  
  <p>However, here's the problem: neither <code>test</code> nor <code>g2g</code> appear to change after the paintComponent() method has executed. The code in the while loop of the main method never ends up drawing a rectangle though the paintComponent() method draws its rectangle fine. In the main method, <code>test</code> and <code>g2g</code> appear to always be <code>false</code> and <code>null</code> respectively. What am I missing here? Is there anyway to use the graphics2D outside the paintComponent() method?</p>