Java Program to Create a Transparent Cursor


This is a Java Program to Create a Transparent Cursor

Problem Description

We have to write a program in Java such that it creates an applet in which the cursor behaves as a transparent cursor inside an area.

Expected Input and Output

For creating a transparent cursor, we can have the following set of input and output.


To View the Transparent Cursor :

When the cursor is moved inside the text area,
it is expected that the cursor becomes transparent.
Problem Solution

1. Create a object of class Toolkit and get the default system toolkit.
2. Create an object of class Image for the cursor image. For a transparent cursor the image path is empty.
3. Create an object of class Point.
4. Create a cursor using the createCustomCursor method of the Cursor class.
5. Create a text area and add the cursor to the area.

Program/Source Code

Here is source code of the Java Program to create a transparent cursor. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

  1. /* Java Program to Create a Transparent Cursor using an Applet */
  2. import java.applet.*;
  3. import java.awt.*;
  4. public class My_Cursor extends Applet
  5. {
  6.     //Function to create a transparent cursor
  7.     public void init()
  8.     {
  9. 	setBackground(Color.white);
  10. 	setLayout(null);
  11. 	//Create a transparent cursor
  12. 	Toolkit t = Toolkit.getDefaultToolkit();
  13. 	Image img = t.getImage("");
  14. 	Point p = new Point(0,0);
  15. 	Cursor c = t.createCustomCursor(img,p,"my_cursor");
  16. 	//Create a area when the cursor is transparent
  17. 	TextArea area = new TextArea();
  18. 	area.setBackground(;
  19. 	area.setBounds(100,100,300,300);
  20. 	this.add(area);
  21. 	//Set transparent cursor to the area
  22. 	area.setCursor(c);
  23.     }
  24. }
  25. /*
  26. <applet code = My_Cursor.class width = 500 height = 500>
  27. </applet>
  28. */

To compile an execute the program use the following commands :

Program Explanation

1. The method createCustomCursor(Cursor Image, Point Hotspot, Cursor Name) is used to create a cursor.
2. To set the cursor to a component use setCursor(Cursor) method.

Runtime Test Cases

Here’s the run time test cases for creating a custom cursor for different input cases.

Test case 1 – When the cursor is outside the text area :

Test case 2 – When the cursor is inside the text area :

Test case 3 – When the cursor is outside the text area :


Sanfoundry Global Education & Learning Series – Java Programs.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn