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.

advertisement
advertisement
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(Color.green);
  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 :

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
>>>javac My_Cursor.java
>>>appletviewer My_Cursor.java
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.

advertisement

Test case 1 – When the cursor is outside the text area :
java-program-cursor-transparent-visible-1

Test case 2 – When the cursor is inside the text area :
java-program-cursor-transparent-invisible

Test case 3 – When the cursor is outside the text area :
java-program-cursor-transparent-visible-2

advertisement

Sanfoundry Global Education & Learning Series – Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). 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!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.