Showing posts with label cursor. Show all posts
Showing posts with label cursor. Show all posts

Java invisible cursor

Complete source code below will show you, how to create invisible cursor in java.

************************************************************************
COMPLETE SOURCE CODE FOR : MakeCursorInvisible.java
************************************************************************


import java.awt.Toolkit;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.Image;

import javax.swing.JFrame;

public class MakeCursorInvisible
{
//Create an empty byte array
byte[]imageByte=new byte[0];

Cursor myCursor;
Point myPoint=new Point(0,0);

//Create image for cursor using empty array
Image cursorImage=Toolkit.getDefaultToolkit().createImage(imageByte);

public MakeCursorInvisible()
{
//Create cursor
myCursor=Toolkit.getDefaultToolkit().createCustomCursor(cursorImage,myPoint,"cursor");

JFrame frame=new JFrame("Put your cursor into me");

//Set cursor for JFrame using created cursor(myCursor)
frame.setCursor(myCursor);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[]args)
{
MakeCursorInvisible mci=new MakeCursorInvisible();
}
}


************************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************************

Get current cursor location in JTextArea

Complete source code below will show you, how to get current cursor location in JTextArea.

***********************************************************************
COMPLETE SOURCE CODE FOR : GetCurrentCursorLocationInJTextArea.java
***********************************************************************


import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class GetCurrentCursorLocationInJTextArea extends JTextArea implements MouseMotionListener
{
//Create window using JFrame with title ( Get current cursor location in JTextArea )
JFrame frame=new JFrame("Get current cursor location in JTextArea");

//Create text field that will show you, current cursor location
JTextField textFieldX=new JTextField(5);
JTextField textFieldY=new JTextField(5);

//Create label that will use to describe function for each text field
JLabel labelX=new JLabel("Coordinate X : ");
JLabel labelY=new JLabel("Coordinate Y : ");

//Create panel that will use to store text field and label
JPanel panelX=new JPanel();
JPanel panelY=new JPanel();

//Panel that will use to store panelX and panelY
JPanel container=new JPanel();

public GetCurrentCursorLocationInJTextArea()
{
//Add mouse motion listener to JTextArea
addMouseMotionListener(this);

//Set container layout using BorderLayout
container.setLayout(new BorderLayout());

//Set panelX and panelY layout using GridLayout
panelX.setLayout(new GridLayout(1,2));
panelY.setLayout(new GridLayout(1,2));

//Add labelX and textFieldX into panelX
panelX.add(labelX);
panelX.add(textFieldX);

//Add labelY and textFieldY into panelY
panelY.add(labelY);
panelY.add(textFieldY);

//Add panelX and panelY into container
container.add(panelX,BorderLayout.WEST);
container.add(panelY,BorderLayout.EAST);

//Set frame layout using BorderLayout
frame.setLayout(new BorderLayout());

//Add JTextArea into JFrame
frame.add(this,BorderLayout.CENTER);

//Add container into JFrame
frame.add(container,BorderLayout.SOUTH);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
}

public void mouseMoved(MouseEvent event)
{
//Get current X-coordinate for cursor
textFieldX.setText("");
textFieldX.setText(Integer.toString(event.getX()));

//Get current Y-coordinate for cursor
textFieldY.setText("");
textFieldY.setText(Integer.toString(event.getY()));
}

public void mouseDragged(MouseEvent event)
{
//Do nothing because our case not handle dragging
}

public static void main(String[]args)
{
GetCurrentCursorLocationInJTextArea gcclijta=new GetCurrentCursorLocationInJTextArea();
}
}


***********************************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************************

Close JFrame when cursor exit from JFrame

************************************************************************
COMPLETE SOURCE CODE FOR : CloseJFrameWhenMouseExit.java
************************************************************************


import javax.swing.JFrame;

import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class CloseJFrameWhenMouseExit
{
public static void main(String[]args)
{
//Create mouse listener that will listen when cursor exit JFrame
MouseListener ml=new MouseAdapter()
{
public void mouseExited(MouseEvent event)
{
//Put JFrame close code here
System.exit(0);
}
};

//Create JFrame with title ( GET YOUR CURSOR INTO JFRAME, AFTER THAT MOVE IT AWAY )
JFrame frame=new JFrame("GET YOUR CURSOR INTO JFRAME, AFTER THAT MOVE IT AWAY");

//Add mouse listener to JFrame
frame.addMouseListener(ml);

//Set default close operation to JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size to :
//Width : 700 pixels
//Height : 400 pixels
frame.setSize(700,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


************************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************************

How to move cursor on screen using java ???


******************************************************************
COMPLETE SOURCE CODE FOR : MovingCursorOnScreen.java
******************************************************************


import java.awt.Robot;

public class MovingCursorOnScreen
{
public static void main(String[]args)
{
try
{
int Xcoordinate=10;
int Ycoordinate=50;

Robot a=new Robot();

//this method will move your cursor to the location base on Xcoordinate and Ycoordinate
a.mouseMove(Xcoordinate,Ycoordinate);
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


******************************************************************
JUST COMPILE AND EXECUTE IT
******************************************************************

Change cursor for a component


*********************************************************************
COMPLETE SOURCE CODE FOR : HowToChangeComponentCursor.java
*********************************************************************


import java.awt.Frame;
import java.awt.Component;
import java.awt.Cursor;

import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class HowToChangeComponentCursor
{
public static void main(String[]args)
{
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

Frame a=new Frame("HOVER YOUR CURSOR IN THIS FRAME");
a.addWindowListener(wl);

//METHOD setCursor WILL SET NEW CURSOR FOR COMPONENT
a.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));

a.setSize(800,600);
a.setVisible(true);
}
}


*********************************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************************

RELAXING NATURE VIDEO