Java Reference
In-Depth Information
LISTING 9.14
//********************************************************************
// OffCenterPanel.java Author: Lewis/Loftus
//
// Represents the primary drawing panel for the OffCenter program.
//********************************************************************
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;
public class OffCenterPanel extends JPanel
{
private final int WIDTH=300, HEIGHT=300;
private DecimalFormat fmt;
private Point current;
private int centerX, centerY;
private double length;
//-----------------------------------------------------------------
// Constructor: Sets up the panel and necessary data.
//-----------------------------------------------------------------
public OffCenterPanel()
{
addMouseListener ( new OffCenterListener());
centerX = WIDTH / 2;
centerY = HEIGHT / 2;
fmt = new DecimalFormat ("0.##");
setPreferredSize ( new Dimension(WIDTH, HEIGHT));
setBackground (Color.yellow);
}
//-----------------------------------------------------------------
// Draws a line from the mouse pointer to the center point of
// the panel and displays the distance.
//-----------------------------------------------------------------
public void paintComponent (Graphics page)
{
super .paintComponent (page);
Search WWH ::




Custom Search