Click to See Complete Forum and Search --> : write an application to display the mouse position whe the mouse is pressed. I need this before mond


Maria4
February 20th, 2000, 02:08 PM
write an application to display the mouse position whe the mouse is pressed.

Please check this code

private int x, y = 0; //x, y coordinates

public TMouseEvent()
{
setTitle("TestMouseEvent");
addMouseListener(this);
}

public static void main(String[] args)
{
Frame f = new TMouseEvent();
f.setSize(200,200);
f.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)
{
}

//draw a small solid square around the point (x,y)
public void paint(Graphics g)
{
if(x!=-1)
g.drawString(x,y);
}
}

Maria4
February 21st, 2000, 06:40 AM
if you press mousebutton on the frame it should display the mouse position
Ex(120,30)

thank you

davedrake1
February 21st, 2000, 09:04 AM
I think your paint routine should be:

public void paint(Graphics g)
{
if(x!=-1)
g.drawString(x + ", " + y, 10,10);
}



Where 10,10 is the location of where on the frame to print the location.
You could substitute g.drawString(x + ", " + y, x, y);

to print the string right next to the mouse cursor.

Maria4
February 21st, 2000, 11:43 AM
Thanks a lot it works.