Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

writing mouse position to screen

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
THANK YOU THANK YOU THANK YOU!!!
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic