0

First of all i don't know good english.

I want to make a window and have 2 labels and 2 fields. One label for x-coordinate and 1 for y-coordinate.Fields will show the x-y coordinates.

Coordinates are from mouse from full screen (meaning outside from window).

I prefer to be on clicking but i have read from other answers and questions that this will not act as we want (because it loses focus).So i tried not to be on clicking

I want help with my code because it has 2 problems-mistakes:

1) window can't close

2)when mouse is not moving fields take the same coordinates forever and i want take 1 time the coordinates and don't take the same until it moves.

here is the full code:

package mouseClick;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class MouseEventDemo extends Frame implements MouseListener {

   // Private variables
   private TextField tfMouseX; //  mouse-click-x
   private TextField tfMouseY; // mouse-click-y

   // Constructor
   public MouseEventDemo() {

       //handle the close-window button.
       WindowDestroyer listener =new WindowDestroyer();
        addWindowListener(listener);

      setLayout(new FlowLayout()); // sets layout

      // Label
      add(new Label("X-Click: ")); // adds component

      // TextField
      tfMouseX = new TextField(10); // 10 columns
      tfMouseX.setEditable(false);  // read-only
      add(tfMouseX);                // adds component

      // Label
      add(new Label("Y-Click: ")); // adds component

      // TextField
      tfMouseY = new TextField(10);
      tfMouseY.setEditable(false);  // read-only
      add(tfMouseY);                // adds component

          // fires the MouseEvent
      addMouseListener(this);

      setTitle("MouseEvent Demo"); // sets title
      setSize(350, 100);           // sets initial size
      setVisible(true);            // shows
   }

   public static void main(String[] args) {
      new MouseEventDemo(); 
   }

   // MouseEvent handlers
   public void mouseClicked(MouseEvent e) {
       while (true){
      tfMouseX.setText(Integer.toString(xmouse()));
      tfMouseY.setText(Integer.toString(ymouse()));
       }
   }
   public void mousePressed(MouseEvent e) { }
   public void mouseReleased(MouseEvent e) { }
   public void mouseEntered(MouseEvent e) { }
   public void mouseExited(MouseEvent e) { }

   public class WindowDestroyer extends WindowAdapter {
        public void windowClosing (WindowEvent e) {
            System.exit(0);
        }
    }
   public int xmouse() {    
        Point simiox = MouseInfo.getPointerInfo().getLocation();
        int x= (int) simiox.getX();
        return x;
    }

    public int ymouse() {
        Point simioy = MouseInfo.getPointerInfo().getLocation();
        int y=(int) simioy.getY();
        return y;
    }
}

1 Answers1

0

in order to close the window, use

windowVariable.seDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

where windowVariable is the variable for you JFrame

EDIT:

For returning the value only once, try using a for loop and a hasMoved boolean. For instance,

    for (int i = 0; i < 1; i++){
         tfMouseX.setText(Integer.toString(xmouse()));
         if (hasMoved == true)
                i = -1;
   }

then do the same for the y. this basically checks to see if the mouse has moved, and if it has not, it will set the label only once. If it has, it will update the label.

ArcWalrus
  • 33
  • 1
  • 7
  • And "how" does the op check if the mouse has moved – MadProgrammer May 23 '15 at 06:12
  • i can't figure out where and how i put windowVariable.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)...Its supposed if the code go in the for loop and never get out then the window cant close..But if you get out of loop then you will stop receiving coordinates meaning program stop work.So the problem is that i want to close the window even if i am in a loop. –  May 23 '15 at 13:53
  • the setDefualtCloseOperation() should go at the end of the main method – ArcWalrus May 23 '15 at 17:03
  • no. it doesn't work IDE say method is undefined..also i need a windowVariable and in the main method cant have a windowVariable because window (and the window variable) created through constructor... –  May 23 '15 at 21:09