title: Changing JPanel Graphics g color drawing line
body: <p>i have a program similar to paint. and that i am trying to implement a change pen color however when i change the color, everything currently drawn is changed to the color RED for example in my program,how can i make it such that it will not repaint everything currently drawn to the currently changed color?Below code will compile and run</p>
  
  <p>Class for the JPanel drawing area</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.Graphics;
  import java.awt.Point;
  import java.awt.Rectangle;
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseEvent;
  import java.util.ArrayList;
  import javax.swing.BorderFactory;
  import javax.swing.JPanel;
  //refer to http://jkost.ergoway.gr/jnkjavaconnection/freedraw.html for the algorithm.
  public class STDrawingArea extends JPanel {
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
      ArrayList&lt;Rectangle&gt; dPoint = new ArrayList&lt;Rectangle&gt;();
      Point point = new Point(-1,-1);
      private Color currentColor;
  
      public STDrawingArea()
      {
           setBorder(BorderFactory.createLineBorder(Color.black));
           setBackground(Color.WHITE);
  
           addMouseMotionListener(new MouseAdapter() {
              public void mouseDragged(MouseEvent e) 
              {
                  dPoint.add(new Rectangle(point.x,point.y,e.getX(),e.getY()));
                  point.x = e.getX();
                  point.y = e.getY();
                  repaint();
              }
  
               });
  
           addMouseListener(new MouseAdapter(){
               public void mousePressed(MouseEvent e)
               {
                   System.out.println("mousePressed X: "+e.getX()+"mousePressed Y: "+e.getY());
                   dPoint.add(new Rectangle(e.getX(),e.getY(),-1,-1));
                   point.x = e.getX();
                   point.y = e.getY();
               }
           });
  
           addMouseListener(new MouseAdapter(){
               public void mouseReleased(MouseEvent e)
               {
                   System.out.println("mouseReleased X: "+e.getX()+"mouseReleased Y: "+e.getY());
                   repaint();
               }
           });
      }
  
      @Override
      public Dimension getPreferredSize() {
          return new Dimension(700,500);
      }
  
      @Override
      protected void paintComponent(Graphics g) {
          super.paintComponent(g);
         g.setColor(getCurrentColor());
          for (int i=0; i &lt; dPoint.size(); i++) {  
              Rectangle r = dPoint.get(i);
              if (r.width != -1)
              {
                g.drawLine(r.x, r.y, r.width, r.height);  
              }
             }  
              /* Draw current point.*/ 
              g.drawLine(point.x, point.y, point.x, point.y);  
      }
  
      //set current drawing color
      public void changePenColor(Color color)
      {
          if (color == null)
          setCurrentColor(Color.BLACK);
          else
          setCurrentColor(color);
      }
  
      //clear drawings method
      public void clearDrawings()
      {
          if(!(dPoint==null))
          {
               dPoint.clear();
               repaint();
          }
  
      }
  
      private void setCurrentColor(Color currentColor) {
          this.currentColor = currentColor;
      }
  
      private Color getCurrentColor() {
          return currentColor;
      }
  }
  </code></pre>
  
  <p>Test main class.</p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.Color;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  
  
  public class STTestMain extends JFrame {
      STDrawingArea drawingArea = new STDrawingArea();
      public STTestMain()
      {
          //JFrame settings
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setTitle("Spelling Trainer");
          setResizable(false);
          setVisible(true);
  
  
          //Panel of buttons
          JPanel buttonContainer = new JPanel();
          JButton btnPenColor = new JButton("Red Pen");
  
          buttonContainer.add(btnPenColor);
          //Drawing Area instantiation
  
  
          //Adding things to JFrame
          getContentPane().add(drawingArea);
          getContentPane().add(buttonContainer,BorderLayout.PAGE_END);
          pack();
  
  
          //button listener
          btnPenColor.addActionListener(new ActionListener() {
  
              @Override
              public void actionPerformed(ActionEvent e) {
                  // TODO Auto-generated method stub
                  drawingArea.changePenColor(Color.RED);
              }
          });
      }
  
  
      public static void main(String args[])
      {
          STTestMain test = new STTestMain();
      }
  
  }
  </code></pre>