Hi,
This program was modified not to draw a rectangle when the mouse was pressed, but to show the pixel location instead. The program compiles, runs, but doesn't show the pixel location when mouse is clicked. If someone can find my error, I'd be VERY grateful!!!
THANKS!!! Mary Ellen
MousePositionInPixels.java--This program modifies the Chapter 8 program
TestMouseEvent.java. Instead of filling a square when a mouse click is
detected, the location of the mouse click in pixels will appear on the
screen.*/
package Chapter8;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.*;
public class MousePositionInPixels extends MyFrameWithExitHandling
implements MouseListener
{
private int x, y = 0; // x, y coordinates
// Default constructor
public MousePositionInPixels()
{
setTitle("MousePositionInPixels");
addMouseListener(this); // Register listener
}
// Main method
public static void main(
String[] args)
{
MousePositionInPixels frame = new MousePositionInPixels();
frame.setSize(200, 200);
frame.setVisible(true);
}
// When the mouse is pressed, the mouse pointer location
// will be stored in (x, y)
public void mousePressed(MouseEvent e)
{
// Get (x, y) coordinates using getX() and getY() methods
x = e.getX();
y = e.getY();
repaint();
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void drawString(Graphics g)
{
String s ="The mouse is at x=" + x + " and y=" + y;
g.drawString(s, x, y );
}
}