Java Reference
In-Depth Information
LISTING 8.20
//********************************************************************
// 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.
//*****************************************************************
Search WWH ::




Custom Search