title: Lines are not being redrawn on JPanel
body: <p>So I'm creating a program that allows the user to draw multiple shapes on a JPanel, similar to a paint program. The problem is that when I attempt to add a line to the ArrayList that stores all the shapes and then redraw them on the JPanel, the line is being stored but not redrawn.</p>
  
  <p>So, for example, say that I select the line tool from the JMenu at the top. Then I click and drag, and the line appears, from the point where I clicked to the mouse's current position. I release the mouse and the line still remains. When I go to draw another line, clicking and dragging, the first one disappears once I release the mouse. How can I get it so that the line will remain on the JPanel?</p>
  
  <pre><code>import ...
  
  public class GraphicsTest {
  
      public GraphicsTest() {
          JFrame frame = new JFrame("");
          Painter painter = new Painter();
          frame.setLayout(new BorderLayout());
      //  frame.setJMenuBar(painter.createMenuBar());
          frame.add(painter,BorderLayout.CENTER);        
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(750,488);
          frame.setVisible(true);
      }
  
      public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  new GraphicsTest();
              }
          });
      }
  
      public class Painter extends JPanel implements MouseListener, MouseMotionListener {
          private java.awt.geom.Line2D.Double line = new java.awt.geom.Line2D.Double();
  
          private List&lt;Shape&gt; shapeList = new ArrayList&lt;Shape&gt;();
          private List&lt;Integer&gt; opNumList = new ArrayList&lt;Integer&gt;();
  
          // Initial color
          private Color color = Color.black;
  
          // int variables that determine the position of various on-screen objects
          private int dragX = 0, dragY = 0, downX = 0, downY = 0, upX = 0, upY = 0;
  
          // Fields that determine which tool should be used
          private int PEN = 0, LINE = 1, RECTANGLE = 2, OVAL = 3;
  
          // Initial tool used
          private int toolNum = LINE;
  
          private Point clickPoint, releasePoint;
  
          /**
           * Sole and default constructor of this class
           **/
          public Painter() {
              // Adding listeners so that the mouse may be used to draw various objects
              addMouseListener(this);
              addMouseMotionListener(this);
          }
  
          @Override
          protected void paintComponent(Graphics g) {
              super.paintComponent(g);
              int index = 0;
              Graphics2D g2 = (Graphics2D) g.create();
              g2.setColor(color);
  
              if (toolNum == LINE) {
                  g2.draw(new java.awt.geom.Line2D.Double(downX,downY,dragX,dragY));
              }
  
              System.out.println("Size: " + shapeList.size());
              if (!opNumList.isEmpty()) {
                  for (Shape s : shapeList) {
                //    System.out.println("Index: " + index);
                //    System.out.println("Element: " + opNumList.get(index));
                //    System.out.println(s);
                      System.out.println();
                      switch (opNumList.get(index)) {
                          case 41: g2.setColor(Color.black);  g2.draw(s); break;
                          default: return;
                      }
                      index++;
                  }
              }
          }
  
          /**
           * MouseListener interface methods
           **/
          @Override
          public void mouseClicked(MouseEvent ev) {}
  
          @Override
          public void mouseEntered(MouseEvent ev) {}
  
          @Override
          public void mouseExited(MouseEvent ev) {}
  
          @Override
          public void mousePressed(MouseEvent ev) {
              clickPoint = new Point(ev.getPoint());
              upX = ev.getX();
              upY = ev.getY();
              downX = ev.getX();
              downY = ev.getY();
          }
  
          @Override
          public void mouseReleased(MouseEvent ev) {
              upX = ev.getX();
              upY = ev.getY();
              releasePoint = new Point(ev.getPoint());
  
              line.x1 = clickPoint.x;
              line.y1 = clickPoint.y;
              line.x2 = releasePoint.x;
              line.y2 = releasePoint.y;
  
              if (toolNum == LINE)
                  shapeList.add(line);
  
              if (color == Color.black) {
                  if (toolNum == LINE)
                      opNumList.add(41);
              }
              repaint();
          }
  
          /**
           * MouseMotionListener interface methods
           **/
          @Override
          public void mouseMoved(MouseEvent ev) {}
  
          @Override
          public void mouseDragged(MouseEvent ev) {
              dragX = ev.getX();
              dragY = ev.getY();
  
              repaint();
          }
      }
  }
  </code></pre>