Java.GUI : Melacak Jejak dan Aktivitas Mouse di Layar


Belakangan ini kuliah libur, sehingga banyak waktu senggang. Waktu luang tersebut saya gunakan untuk memodifikasi dan menyalin ulang kode untuk melacak/tracking jejak dan aktivitas(event) mouse Anda pada Layar. Sekalian belajar GUI. Saya ingin bisa GUI, kenapa? Karena saya tertarik dengan desain.
Oke, inilah kodenya, terbagi dalam dua kelas :

  • class MouseTrackerFrame.java
    import java.awt.Color;
    import java.awt.BorderLayout;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.MouseEvent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class MouseTrackerFrame extends JFrame
    {
        private JPanel mousePanel;
        private JLabel statusBar;
    
        public MouseTrackerFrame()
        {
        super("Demonstrasi Mouse Event");
        mousePanel = new JPanel();
        mousePanel.setBackground(Color.WHITE);
        add(mousePanel, BorderLayout.CENTER);
    
        statusBar = new JLabel("Mouse Di luar JPanel");
        add(statusBar, BorderLayout.SOUTH);
    
        //create and register listener for mouse and mouse motion events
        MouseHandler handler = new MouseHandler();
        mousePanel.addMouseListener(handler);
        mousePanel.addMouseMotionListener(handler);
        }
        private class MouseHandler implements MouseListener, MouseMotionListener
        {
        public void mouseClicked(MouseEvent event)
        {
            statusBar.setText(String.format("Diklik di [%d, %d]",
                            event.getX(), event.getY()));
        }
    
        public void mousePressed(MouseEvent event)
        {
            statusBar.setText(String.format("Ditekan di [%d, %d]",
                            event.getX(), event.getY()));
        }
    
        public void mouseReleased(MouseEvent event)
        {
            statusBar.setText(String.format("Dilepas di [%d,%d]",
                            event.getX(), event.getY()));
        }
        public void mouseEntered(MouseEvent event)
        {
            statusBar.setText(String.format("Mouse masuk di[%d,%d]",
                            event.getX(),event.getY()));
            mousePanel.setBackground(Color.GREEN);
        }
        public void mouseExited(MouseEvent event)
        {
            statusBar.setText("Mouse di luar JPanel");
            mousePanel.setBackground(Color.WHITE);
        }
    
        public void mouseDragged(MouseEvent event)
        {
            statusBar.setText(String.format("Diseret Like Hell di [%d, %d]",
                            event.getX(), event.getY()));
        }
        public void mouseMoved(MouseEvent event)
        {
            statusBar.setText(String.format("Digerakkan di [%d, %d]",
                            event.getX(), event.getY()));
        }
        }
    }
    
  • class MouseTracker.java
    import javax.swing.JFrame;
    
    public class MouseTracker
    {
        public static void main(String args[])
        {
        MouseTrackerFrame mouseTrackerFrame = new MouseTrackerFrame();
        mouseTrackerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mouseTrackerFrame.setSize(300,100);
        mouseTrackerFrame.setVisible(true);
        }
    }
    

SCREENSHOTS









Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s