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);
}
}
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);
}
}