Question
//******************************************************************** // RubberLinesPanel.java Author: Lewis/Loftus // // Represents the primary drawing panel for the RubberLines program. //******************************************************************** import javax.swing.JPanel; import java.awt.*; import java.awt.event.*; public class
//******************************************************************** // RubberLinesPanel.java Author: Lewis/Loftus // // Represents the primary drawing panel for the RubberLines program. //******************************************************************** 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 initial 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) {} } }
Modify the RubberLines program from this chapter so that it shows all of the lines drawn. Show only the final lines (from initial mouse press to mouse release), not the intermediate lines drawn to show the rubberbanding effect. Hint: Keep track of a list of objects that represent the lines similar to how the Dots program kept track of multiple dots.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started