Posted on 2004-11-06
Last Modified: 2008-02-20
I have this program "RubberLines" that I need to make remember all the lines that users have put in. It has been suggested that I use an ArrayList to store where the points where located and have them draw out each time a new line is put in. I'm not sure how to  store the points of a line in an Array list.

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

public class RubberLinesPanel extends JPanel
   private Point point1 = null, point2 = null;

   //  Constructor: Sets up this panel to listen for mouse events.
   public RubberLinesPanel()
      LineListener listener = new LineListener();
      addMouseListener (listener);
      addMouseMotionListener (listener);

      setBackground (;
      setPreferredSize (new Dimension(400, 400));

   //  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);
      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();

      //  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();

      //  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) {}
Question by:romeotheriault
    1 Comment
    LVL 92

    Accepted Solution

    1. Store the two points individually, so for every line you would add two points to list.


    2. Add each line has 2 element array of points

    lines.add(new Point[] { point1, point2 });

    3. Use the java.awt.geom.Line2D.float class

    lines.add(new Line2D.Float(point1, point2));

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    EvenOdd challenge 10 69
    advance java 4 36
    countHi2 challenge 7 30
    Running Jira on Raspberry PI 2? 3 45
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now