CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2000
    Posts
    20

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



  2. #2
    Join Date
    Feb 2000
    Posts
    20

    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


  3. #3
    Join Date
    Sep 1999
    Location
    Johnson City, NY
    Posts
    43

    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.



  4. #4
    Join Date
    Feb 2000
    Posts
    20

    Re: write an application to display the mouse position whe the mouse is pressed. I need this before

    Thanks a lot it works.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured