Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

modify the program below so that listener classes are derived from the appropria

ID: 3763069 • Letter: M

Question

 modify the program below so that listener classes are derived from the appropriate adapter classes for handling mouse events. also remove all unessecary code  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 (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);       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();          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) {}    } } 

Explanation / Answer

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)
{
page.setColor(Color.GREEN);
page.drawLine(point1.x, point1.y, point2.x, point2.y);
}
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)
{
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);
}
}