• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

writing mouse position to screen

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 );
}
}
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THANK YOU THANK YOU THANK YOU!!!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic