This is a Java Program to Create a Transparent Cursor
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.
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.
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.
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.
/* Java Program to Create a Transparent Cursor using an Applet */
import java.applet.*;
import java.awt.*;
public class My_Cursor extends Applet
{
//Function to create a transparent cursor
public void init()
{
setBackground(Color.white);
setLayout(null);
//Create a transparent cursor
Toolkit t = Toolkit.getDefaultToolkit();
Image img = t.getImage("");
Point p = new Point(0,0);
Cursor c = t.createCustomCursor(img,p,"my_cursor");
//Create a area when the cursor is transparent
TextArea area = new TextArea();
area.setBackground(Color.green);
area.setBounds(100,100,300,300);
this.add(area);
//Set transparent cursor to the area
area.setCursor(c);
}
}
/*
<applet code = My_Cursor.class width = 500 height = 500>
</applet>
*/
To compile an execute the program use the following commands :
>>>javac My_Cursor.java >>>appletviewer My_Cursor.java
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.
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.
- Practice Information Technology MCQs
- Practice BCA MCQs
- Check Programming Books
- Practice Programming MCQs
- Apply for Java Internship