the program draws lines, but every time I draw a new line, the previous one disappears. How can I let it show all the lines drawn? I know my problem is in the array, but how to fix it? thanks for help

import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class RubberLinesPanel extends JPanel
{
   /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
   private Point point1 = null, point2 = null;
   private ArrayList<Point> pointList;

   //-----------------------------------------------------------------
   //  Constructor: Sets up this panel to listen for mouse events.
   //-----------------------------------------------------------------
   public RubberLinesPanel()
   {
	  pointList = new ArrayList<Point>();
	   
 
      LineListener listener = new LineListener();
      addMouseListener (listener);
      addMouseMotionListener (listener);

      setBackground (Color.black);
      setPreferredSize (new Dimension(400, 200));
   }

   //-----------------------------------------------------------------
   //  Draws the current line from the intial mouse-pressed point to
   //  the current position of the mouse.
   //-----------------------------------------------------------------
   public void paintComponent (Graphics page)
   {
      super.paintComponent (page);

      page.setColor (Color.yellow);
     
      for(Point spot : pointList )
      {    	  
      if (point1 != null && point2 != null  )
         page.drawLine (point1.x, point1.y, point2.x, point2.y);
   
      }
   }

   //*****************************************************************
   //  Represents the listener for all mouse events.
   //*****************************************************************
   private class LineListener implements MouseListener,
                                         MouseMotionListener
   {
      //--------------------------------------------------------------
      //  Captures the initial position at which the mouse button is
      //  pressed.
      //--------------------------------------------------------------
      public void mousePressed (MouseEvent event)
      {
         point1 = event.getPoint();
        pointList.add(point1);
       
      }

      //--------------------------------------------------------------
      //  Gets the current position of the mouse as it is dragged and
      //  redraws the line to create the rubberband effect.
      //--------------------------------------------------------------
      public void mouseDragged (MouseEvent event)
      {
         point2 = event.getPoint();
         pointList.add(point2);
         repaint();
      }

      //--------------------------------------------------------------
      //  Provide empty definitions for unused event methods.
      //--------------------------------------------------------------
      public void mouseClicked (MouseEvent event) {}
      public void mouseReleased (MouseEvent event) {}
      public void mouseEntered (MouseEvent event) {}
      public void mouseExited (MouseEvent event) {}
      public void mouseMoved (MouseEvent event) {}
   }
}

import javax.swing.JFrame;

public class RubberLines
{
   //-----------------------------------------------------------------
   //  Creates and displays the application frame.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      JFrame frame = new JFrame ("Rubber Lines");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

      frame.getContentPane().add (new RubberLinesPanel());

      frame.pack();
      frame.setVisible(true);
   }
}

Read carefully what you are doing in the paintComponent() method. You are looping through every point in the array list and each time drawing a line between point1 and point2 - which are completely separate from your list of points.

Perhaps you want to loop the array list by it's index and for each pair of points draw a line and increment by two.

Alternately you could create a Line object when the mouse is released and add that to an ArrayList instead of points. Then your for:each loop could draw each line in the collection.

public int joinedInMonth( int month) 
    {
        if (month>0 && month<13){
           return clubMembers.size(); }
        else{
            System.out.println("Error: Month must be between 1 and 12");
            return= 0;}
    }

I get an error of illegal start of expression pointing to the return=0 line. Any idea why?

Read carefully what you are doing in the paintComponent() method. You are looping through every point in the array list and each time drawing a line between point1 and point2 - which are completely separate from your list of points.

Perhaps you want to loop the array list by it's index and for each pair of points draw a line and increment by two.

Alternately you could create a Line object when the mouse is released and add that to an ArrayList instead of points. Then your for:each loop could draw each line in the collection.

thanks , I revised my code but got a couple of error messages, which I indicated in the following code. what could my problems be? thanks again for any help : )

import javax.sound.sampled.Line;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Line2D;
import java.util.ArrayList;

public class RubberLinesPanel extends JPanel
{
   /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
   private Point point1 = null, point2 = null;
   private ArrayList<Line> lineList;

   //-----------------------------------------------------------------
   //  Constructor: Sets up this panel to listen for mouse events.
   //-----------------------------------------------------------------
   public RubberLinesPanel()
   {
	lineList = new ArrayList<Line>();
	   
 
      LineListener listener = new LineListener();
      addMouseListener (listener);
      addMouseMotionListener (listener);

      setBackground (Color.black);
      setPreferredSize (new Dimension(400, 200));
   }

   //-----------------------------------------------------------------
   //  Draws the current line from the intial mouse-pressed point to
   //  the current position of the mouse.
   //-----------------------------------------------------------------
   public void paintComponent (Graphics page)
   {
      super.paintComponent (page);

      page.setColor (Color.yellow);
     
      for(int i=0;i<lineList.size();i++)
      {
    	  if(lineList[i]!=null)// here i got error message
    	  {
    	  page.drawLine(lineList[i].x,lineList[i].y,lineList[i+1].x,lineList[i+1].y);// error message again
    	  }
      }
    	  

   }

   //*****************************************************************
   //  Represents the listener for all mouse events.
   //*****************************************************************
   private class LineListener implements MouseListener,
                                         MouseMotionListener
   {
      //--------------------------------------------------------------
      //  Captures the initial position at which the mouse button is
      //  pressed.
      //--------------------------------------------------------------
      public void mousePressed (MouseEvent event)
      {
         point1 = event.getPoint();
  
       
      }

      //--------------------------------------------------------------
      //  Gets the current position of the mouse as it is dragged and
      //  redraws the line to create the rubberband effect.
      //--------------------------------------------------------------
      public void mouseDragged (MouseEvent event)
      {
         point2 = event.getPoint();
    
         Line2D.Double newLine = new Line2D.Double(point1,point2);
         lineList.add((Line) newLine);

         repaint();
      }

      //--------------------------------------------------------------
      //  Provide empty definitions for unused event methods.
      //--------------------------------------------------------------
      public void mouseClicked (MouseEvent event) {}
      public void mouseReleased (MouseEvent event) {}
      public void mouseEntered (MouseEvent event) {}
      public void mouseExited (MouseEvent event) {}
      public void mouseMoved (MouseEvent event) {}
   }
}

start quote:

public int joinedInMonth( int month) 
{
    if (month>0 && month<13){
       return clubMembers.size(); }
    else{
        System.out.println("Error: Month must be between 1 and 12");
        return= 0;}
}

I get an error of illegal start of expression pointing to the return=0 line. Any idea why?

I think you probably need to use "return 0; " instead of "return = 0;"

thanks , I revised my code but got a couple of error messages, which I indicated in the following code. what could my problems be? thanks again for any help : )

You're trying to access the elements of lineList like an array, but it's an ArrayList. Use lineList.get(i) instead.

I think you probably need to use "return 0; " instead of "return = 0;"

thank you, that worked! I am new at this, but really would like to learn it. Thanks again:)

You're trying to access the elements of lineList like an array, but it's an ArrayList. Use lineList.get(i) instead.

thanks Ezzaral : ) that problem is solved,but I got another error in the for loop, can you take a look for me please? thank you

for(int i=0;i<lineList.size();i++)
      {
    	  if(lineList.get(i) != null)
    	  {
    	    page.drawLine(lineList.get(i).x,lineList.get (i).y,lineList.get(i+1).x,lineList.get(i+1).y;// error message here
    	  }
      }

Since I didn't make it work, now I go back to arrayList<Point>, but it doesn't seem to work well. can anyone help please? thanks

import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class RubberLinesPanel extends JPanel
{
   /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
   private Point point1 = null, point2 = null;
   private ArrayList<Point> pointList;

   //-----------------------------------------------------------------
   //  Constructor: Sets up this panel to listen for mouse events.
   //-----------------------------------------------------------------
   public RubberLinesPanel()
   {
	pointList = new ArrayList<Point>();
	   
 
      LineListener listener = new LineListener();
      addMouseListener (listener);
      addMouseMotionListener (listener);

      setBackground (Color.black);
      setPreferredSize (new Dimension(400, 200));
   }

   //-----------------------------------------------------------------
   //  Draws the current line from the intial mouse-pressed point to
   //  the current position of the mouse.
   //-----------------------------------------------------------------
   public void paintComponent (Graphics page)
   {
      super.paintComponent (page);

      page.setColor (Color.yellow);
     
      for(int i=0;i<pointList.size();i=i+2)
      {
    	  if(pointList.get(i) != null && pointList.get(i+1) != null)
    	  {
    	  page.drawLine(pointList.get(i).x,pointList.get(i).y,pointList.get(i+1).x,pointList.get(i+1).y);
    	  }
      }
    	  

   }

   //*****************************************************************
   //  Represents the listener for all mouse events.
   //*****************************************************************
   private class LineListener implements MouseListener,
                                         MouseMotionListener
   {
      //--------------------------------------------------------------
      //  Captures the initial position at which the mouse button is
      //  pressed.
      //--------------------------------------------------------------
      public void mousePressed (MouseEvent event)
      {
         point1 = event.getPoint();
         pointList.add(point1);
       
      }

      //--------------------------------------------------------------
      //  Gets the current position of the mouse as it is dragged and
      //  redraws the line to create the rubberband effect.
      //--------------------------------------------------------------
      public void mouseDragged (MouseEvent event)
      {
         point2 = event.getPoint();   
         pointList.add(point2);

         repaint();
      }

      //--------------------------------------------------------------
      //  Provide empty definitions for unused event methods.
      //--------------------------------------------------------------
      public void mouseClicked (MouseEvent event) {}
      public void mouseReleased (MouseEvent event) {}
      public void mouseEntered (MouseEvent event) {}
      public void mouseExited (MouseEvent event) {}
      public void mouseMoved (MouseEvent event) {}
   }
}
import javax.swing.JFrame;

public class RubberLines
{
   //-----------------------------------------------------------------
   //  Creates and displays the application frame.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      JFrame frame = new JFrame ("Rubber Lines");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

      frame.getContentPane().add (new RubberLinesPanel());

      frame.pack();
      frame.setVisible(true);
   }
}

thanks Ezzaral : ) that problem is solved,but I got another error in the for loop, can you take a look for me please? thank you

I think you are getting more detailed information than "error message". I can guess what it is, but actually posting those error messages would be helpful. They are usually very specific in telling you the problem.

edit: posted while you were posting this most recent version.

I think you are getting more detailed information than "error message". I can guess what it is, but actually posting those error messages would be helpful. They are usually very specific in telling you the problem.

edit: posted while you were posting this most recent version.

there is not error while I compile the last version of my code I posted here, but as soon as I run and start to draw, messages in console is the following:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at RubberLinesPanel.paintComponent(RubberLinesPanel.java:43)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

So examine your loop and the code in it and try to figure out how it could access an index that is too high for your collection.

I revised my code again. it compiles and runs, but when i draw a single line, it looks like mulitiple lines together. what the pronblem is? can anyone help please ? thank you : )

import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Line2D;
import java.util.ArrayList;
public class RubberLinesPanel extends JPanel
{
   /**
     * 
     */
    private static final long serialVersionUID = 1L;
   private Point point1 = null, point2 = null;
   private ArrayList<Line2D.Double> lines;

   //-----------------------------------------------------------------
   //  Constructor: Sets up this panel to listen for mouse events.
   //-----------------------------------------------------------------
   public RubberLinesPanel()
   {
    lines = new ArrayList<Line2D.Double>();
       

      LineListener listener = new LineListener();
      addMouseListener (listener);
      addMouseMotionListener (listener);

      setBackground (Color.black);
      setPreferredSize (new Dimension(400, 200));
   }

   //-----------------------------------------------------------------
   //  Draws the current line from the intial mouse-pressed point to
   //  the current position of the mouse.
   //-----------------------------------------------------------------
   public void paintComponent (Graphics page)
   {
      super.paintComponent (page);

      page.setColor (Color.yellow);
     
      for(int i=0;i<lines.size();i++)
      {
          if(lines.get(i) != null)
          {
          page.drawLine((int)lines.get(i).getX1(),(int)lines.get(i).getY1(),
        		  ( int)lines.get(i).getX2(),(int)lines.get(i).getY2());
          }
      }
          

   }

   //*****************************************************************
   //  Represents the listener for all mouse events.
   //*****************************************************************
   private class LineListener implements MouseListener,
                                         MouseMotionListener
   {
      //--------------------------------------------------------------
      //  Captures the initial position at which the mouse button is
      //  pressed.
      //--------------------------------------------------------------
      public void mousePressed (MouseEvent event)
      {
         point1 = event.getPoint();
       
      }

      //--------------------------------------------------------------
      //  Gets the current position of the mouse as it is dragged and
      //  redraws the line to create the rubberband effect.
      //--------------------------------------------------------------
      public void mouseDragged (MouseEvent event)
      {
    	  point2 = event.getPoint();
          
          
        Line2D.Double newLine = new Line2D.Double(point1,point2);
        lines.add(newLine);
       
        repaint();

      }

      //--------------------------------------------------------------
      //  Provide empty definitions for unused event methods.
      //--------------------------------------------------------------
      public void mouseClicked (MouseEvent event) {}
      public void mouseReleased (MouseEvent event){}
      public void mouseEntered (MouseEvent event) {}
      public void mouseExited (MouseEvent event) {}
      public void mouseMoved (MouseEvent event) {}
   }
}
import javax.swing.JFrame;

public class RubberLines
{
   //-----------------------------------------------------------------
   //  Creates and displays the application frame.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      JFrame frame = new JFrame ("Rubber Lines");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

      frame.getContentPane().add (new RubberLinesPanel());

      frame.pack();
      frame.setVisible(true);
   }
}

You're adding a new line for every mouseDragged() event. You only want to add a line when the mouse is released.

Well, what is the size of your array when it draws two lines next to each other? Is it one, or two? Do some debugging. If the array's size is two, then no wonder it looks like that. If its one, my theory is kaput, but have you even made this simple check?

Oops, posted at the same time as you Ezzaral.

You're adding a new line for every mouseDragged() event. You only want to add a line when the mouse is released.

I moved the code to mouseReleased from mousedragged,. it seems to run but lose its "rubber banding "effect, which is this program supposed to do. is there any way to keep the "rubber banding"effect while drawing the line? thanks

public void mouseDragged (MouseEvent event)
      {
    	 
      }

      //--------------------------------------------------------------
      //  Provide empty definitions for unused event methods.
      //--------------------------------------------------------------
      public void mouseClicked (MouseEvent event) {}
      public void mouseReleased (MouseEvent event)
{
 point2 = event.getPoint();
          
          
        Line2D.Double newLine = new Line2D.Double(point1,point2);
        lines.add(newLine);
       
        repaint();
}
      public void mouseEntered (MouseEvent event) {}
      public void mouseExited (MouseEvent event) {}
      public void mouseMoved (MouseEvent event) {}
   }
}

Keep the point2 assignment and a repaint() in mouseDragged() . Leave mouseReleased() as you have it, but also set point2 to null. Then alter your paintComponent() method to draw a line from point1 to point2 if point2 is not null.

commented: very helpful in this thread (acc. to OP) +4

thanks for all the help. I revised my code again. it works fine now, with the help from the nice people here and a friend of mine. what I did is I added "point1 = point2" in mouseDragged class. thanks again to everyone who left message here. thank you Ezzaral : )

import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Line2D;
import java.util.ArrayList;
public class RubberLinesPanel extends JPanel
{
   /**
     * 
     */
    private static final long serialVersionUID = 1L;
   private Point point1 = null, point2 = null;
   private ArrayList<Line2D.Double> lines;

   //-----------------------------------------------------------------
   //  Constructor: Sets up this panel to listen for mouse events.
   //-----------------------------------------------------------------
   public RubberLinesPanel()
   {
    lines = new ArrayList<Line2D.Double>();
       
      LineListener listener = new LineListener();
      addMouseListener (listener);
      addMouseMotionListener (listener);

      setBackground (Color.black);
      setPreferredSize (new Dimension(400, 200));
   }

   //-----------------------------------------------------------------
   //  Draws the current line from the intial mouse-pressed point to
   //  the current position of the mouse.
   //-----------------------------------------------------------------
   public void paintComponent (Graphics page)
   {
      super.paintComponent (page);

      page.setColor (Color.yellow);
     
      for(int i=0;i<lines.size();i++)
      {
          if(lines.get(i) != null)
          {
          page.drawLine((int)lines.get(i).getX1(),(int)lines.get(i).getY1(),
        		  (int)lines.get(i).getX2(),(int)lines.get(i).getY2());
          }
      }
          

   }

   //*****************************************************************
   //  Represents the listener for all mouse events.
   //*****************************************************************
   private class LineListener implements MouseListener,
                                         MouseMotionListener
   {
      //--------------------------------------------------------------
      //  Captures the initial position at which the mouse button is
      //  pressed.
      //--------------------------------------------------------------
      public void mousePressed (MouseEvent event)
      {
         point1 = event.getPoint();
       
      }

      //--------------------------------------------------------------
      //  Gets the current position of the mouse as it is dragged and
      //  redraws the line to create the rubberband effect.
      //--------------------------------------------------------------
      public void mouseDragged (MouseEvent event)
      {
           point2 = event.getPoint();
          
           
         Line2D.Double newLine = new Line2D.Double(point1,point2);
         lines.add(newLine);
           point1 = point2;
         repaint();
      }

      //--------------------------------------------------------------
      //  Provide empty definitions for unused event methods.
      //--------------------------------------------------------------
      public void mouseClicked (MouseEvent event) {}
      public void mouseReleased (MouseEvent event) {}
      public void mouseEntered (MouseEvent event) {}
      public void mouseExited (MouseEvent event) {}
      public void mouseMoved (MouseEvent event) {}
   }
}

Ok, that isn't exactly what I thought you wanted to do, but if it works for you, great :)
I thought you wanted to be able to draw multiple lines by clicking and dragging to stretch the line, like this:

import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import javax.swing.JFrame;

public class RubberLinesPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private Point point1 = null,  point2 = null;
    private ArrayList<Line2D.Double> lines;

    //-----------------------------------------------------------------
    //  Constructor: Sets up this panel to listen for mouse events.
    //-----------------------------------------------------------------
    public RubberLinesPanel() {
        lines = new ArrayList<Line2D.Double>();

        LineListener listener = new LineListener();
        addMouseListener(listener);
        addMouseMotionListener(listener);

        setBackground(Color.black);
        setPreferredSize(new Dimension(400, 200));
    }

    //-----------------------------------------------------------------
    //  Draws the current line from the intial mouse-pressed point to
    //  the current position of the mouse.
    //-----------------------------------------------------------------
    public void paintComponent(Graphics page) {
        super.paintComponent(page);

        if (point2 != null) {
            // draw the temporary "rubberband" line
            page.setColor(Color.GREEN);
            page.drawLine(point1.x, point1.y, point2.x, point2.y);
        }

        // draw all of the completed lines
        page.setColor(Color.yellow);
        for (int i = 0; i < lines.size(); i++) {
            if (lines.get(i) != null) {
                page.drawLine((int)lines.get(i).getX1(), (int)lines.get(i).getY1(),
                        (int)lines.get(i).getX2(), (int)lines.get(i).getY2());
            }
        }
    }

    //*****************************************************************
    //  Represents the listener for all mouse events.
    //*****************************************************************
    private class LineListener implements MouseListener,
            MouseMotionListener {
        //--------------------------------------------------------------
        //  Captures the initial position at which the mouse button is
        //  pressed.
        //--------------------------------------------------------------
        public void mousePressed(MouseEvent event) {
            point1 = event.getPoint();
        }

        //--------------------------------------------------------------
        //  Gets the current position of the mouse as it is dragged and
        //  redraws the line to create the rubberband effect.
        //--------------------------------------------------------------
        public void mouseDragged(MouseEvent event) {
            point2 = event.getPoint();
            repaint();
        }

        //--------------------------------------------------------------
        //  Provide empty definitions for unused event methods.
        //--------------------------------------------------------------
        public void mouseClicked(MouseEvent event) {
        }

        public void mouseReleased(MouseEvent event) {
            // add the line to the collection to be drawn
            Line2D.Double newLine = new Line2D.Double(point1, point2);
            lines.add(newLine);
            point2 = null;
            repaint();
        }

        public void mouseEntered(MouseEvent event) {
        }

        public void mouseExited(MouseEvent event) {
        }

        public void mouseMoved(MouseEvent event) {
        }
    }


   //-----------------------------------------------------------------
   //  Creates and displays the application frame.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      JFrame frame = new JFrame ("Rubber Lines");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

      frame.getContentPane().add (new RubberLinesPanel());

      frame.pack();
      frame.setVisible(true);
   }
}

Ok, that isn't exactly what I thought you wanted to do, but if it works for you, great :)
I thought you wanted to be able to draw multiple lines by clicking and dragging to stretch the line, like this:

import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import javax.swing.JFrame;

public class RubberLinesPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private Point point1 = null,  point2 = null;
    private ArrayList<Line2D.Double> lines;

    //-----------------------------------------------------------------
    //  Constructor: Sets up this panel to listen for mouse events.
    //-----------------------------------------------------------------
    public RubberLinesPanel() {
        lines = new ArrayList<Line2D.Double>();

        LineListener listener = new LineListener();
        addMouseListener(listener);
        addMouseMotionListener(listener);

        setBackground(Color.black);
        setPreferredSize(new Dimension(400, 200));
    }

    //-----------------------------------------------------------------
    //  Draws the current line from the intial mouse-pressed point to
    //  the current position of the mouse.
    //-----------------------------------------------------------------
    public void paintComponent(Graphics page) {
        super.paintComponent(page);

        if (point2 != null) {
            // draw the temporary "rubberband" line
            page.setColor(Color.GREEN);
            page.drawLine(point1.x, point1.y, point2.x, point2.y);
        }

        // draw all of the completed lines
        page.setColor(Color.yellow);
        for (int i = 0; i < lines.size(); i++) {
            if (lines.get(i) != null) {
                page.drawLine((int)lines.get(i).getX1(), (int)lines.get(i).getY1(),
                        (int)lines.get(i).getX2(), (int)lines.get(i).getY2());
            }
        }
    }

    //*****************************************************************
    //  Represents the listener for all mouse events.
    //*****************************************************************
    private class LineListener implements MouseListener,
            MouseMotionListener {
        //--------------------------------------------------------------
        //  Captures the initial position at which the mouse button is
        //  pressed.
        //--------------------------------------------------------------
        public void mousePressed(MouseEvent event) {
            point1 = event.getPoint();
        }

        //--------------------------------------------------------------
        //  Gets the current position of the mouse as it is dragged and
        //  redraws the line to create the rubberband effect.
        //--------------------------------------------------------------
        public void mouseDragged(MouseEvent event) {
            point2 = event.getPoint();
            repaint();
        }

        //--------------------------------------------------------------
        //  Provide empty definitions for unused event methods.
        //--------------------------------------------------------------
        public void mouseClicked(MouseEvent event) {
        }

        public void mouseReleased(MouseEvent event) {
            // add the line to the collection to be drawn
            Line2D.Double newLine = new Line2D.Double(point1, point2);
            lines.add(newLine);
            point2 = null;
            repaint();
        }

        public void mouseEntered(MouseEvent event) {
        }

        public void mouseExited(MouseEvent event) {
        }

        public void mouseMoved(MouseEvent event) {
        }
    }


   //-----------------------------------------------------------------
   //  Creates and displays the application frame.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      JFrame frame = new JFrame ("Rubber Lines");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

      frame.getContentPane().add (new RubberLinesPanel());

      frame.pack();
      frame.setVisible(true);
   }
}

Thank you Ezzaral. This temporary line creates a perfect rubber banding effect while mouse is dragged, and all completed lines are drawn beautifuly on the screen when the mouse is released. I have changed my code to this version, wich is the best one. Thanks again Ezzaral for your help. You are wonderful!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.