Ruang penulis
← Kembali ke jurnal
AWT

How do I get the location of mouse pointer?

MouseInfo provides methods for getting information about the mouse, such as mouse pointer location and the number of mouse buttons. package org.kodejava.awt; import java.awt.*; public class MouseInfoDemo { public static void main(String[] args) { int i = 0; while (i < 10) { // Get the location of our mouse x and y coordinate using […]

DWayan · 09 Mei 2009 · 1 menit baca

Artikel oleh Wayan · Sumber: Kode Java ↗

Terjemahan artikel ini belum tersedia. Isi asli ditampilkan.

MouseInfo provides methods for getting information about the mouse, such as mouse pointer location and the number of mouse buttons.

package org.kodejava.awt;

import java.awt.*;

public class MouseInfoDemo {
    public static void main(String[] args) {
        int i = 0;
        while (i < 10) {
            // Get the location of our mouse x and y coordinate using
            // the PointerInfo.getLocation() method which return an 
            // instance of Point.
            Point location = MouseInfo.getPointerInfo().getLocation();
            double x = location.getX();
            double y = location.getY();

            System.out.println("x = " + x);
            System.out.println("y = " + y);

            try {
                Thread.sleep(1000);
                i++;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
← Jelajahi catatan lainnya