write an application to display the mouse position whe the mouse is pressed. I need this before mond
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);
}
}
Re: write an application to display the mouse position whe the mouse is pressed. I need this before
if you press mousebutton on the frame it should display the mouse position
Ex(120,30)
thank you
Re: write an application to display the mouse position whe the mouse is pressed. I need this before
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.
Re: write an application to display the mouse position whe the mouse is pressed. I need this before