aspose file tools*
The moose likes Beginning Java and the fly likes writing mouse position to screen Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "writing mouse position to screen" Watch "writing mouse position to screen" New topic
Author

writing mouse position to screen

MaryEllen Volb
Ranch Hand

Joined: Jan 31, 2001
Posts: 35
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 );
}
}
Helmut Lerch
Ranch Hand

Joined: Feb 11, 2001
Posts: 48
Hi,
Originally posted by MaryEllen Volb:
Hi,
[pre]
public void drawString(Graphics g) {
String s ="The mouse is at x=" + x + " and y=" + y;
g.drawString(s, x, y );
}
[/pre]

change this method to:

MaryEllen Volb
Ranch Hand

Joined: Jan 31, 2001
Posts: 35
THANK YOU THANK YOU THANK YOU!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: writing mouse position to screen