How to Add a Custom Cursor in JFrame
In this tutorial, we will focus on creating and adding a custom cursor in Java JFrame.
Creation of a JFrame
We firstly create a Java JFrame in our code and import necessary libraries such as Java Swing and Java AWT. To learn more about these libraries, there is a detailed explanation about JFrame, JLabel, their components, etc in the below articles. Do check them out.
Let’s see the code:
JFrame fr = new JFrame(); // Creating a JFrame fr.setTitle("Custom Cursor"); //Setting title of the frame fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Closing frame on close button fr.setBounds(100, 200, 350, 300); //Setting location of frame fr.setVisible(true);
Output:
Creation of a JLabel
Here, the idea is to create two labels each one with a custom pointer. For this exact purpose, we will first create a Java Panel object. Then, we create two Java labels namely “CodeSpeedy Pointer” which will have the official CodeSpeedy Logo as a cursor pointer.
We now create another label namely “Another Custom Pointer” which would have a separate cursor pointer image. Thus, we will add these two labels to our panel and then display them on the frame.
The code is as shown below:
JPanel panel = new JPanel(); //Creating a JPanel Point point = new Point(0,0); //Creating a Point object //Creating two Jlabels Label label1 = new Label("CodeSpeedy Pointer"); Label label2 = new Label("Another Custom Pointer"); //Adding labels to the panel panel.add(label1); panel.add(label2);
Output:
Create a Custom Cursor in JFrame
We use the createCustomCursor() method of the Toolkit class to create custom cursors. This method contains three parameters i.e., the image of the cursor, the location point where it can be accessed, the name of the cursor.
We must add these necessary parameters to this method. For the first parameter, we use the Image class and specify the location of the image. In the case of the second parameter, we create a Point object with the location. Lastly, we specify the name of the cursor.
Let us see the code:
//Initializing the Toolkit Toolkit tkit=Toolkit.getDefaultToolkit(); //Extractig the Images for the custom cursor Image img1 = tkit.getImage("CodeSpeedy-Logo.png"); Image img2 = tkit.getImage("Custom-2.png"); //Creating two custom cursors Cursor cursor1 = tkit.createCustomCursor(img1, point, "CodeSpeedy"); Cursor cursor2 = tkit.createCustomCursor(img2, point, "YellowCursor");
We can use various images for our cursor pointers which can be found here. In this tutorial, I have used two images for the creation of custom cursors which can be found in the links below :
- https://www.codespeedy.com/wp-content/uploads/2022/01/CodeSpeedy-Logo.png
- https://www.codespeedy.com/wp-content/uploads/2022/01/cursor-pointer.png
Now, after creating the custom cursors, we must make sure to display them on the JFrame. For achieving this, we add our cursors to the labels. After doing this, we add these labels to the panel and then to the JFrame.
By doing so, we have achieved the creation of custom cursors and added them to the JFrame. We will now look at the entire code for a detailed understanding of what we have done so far.
//Java Program to Add a Custom Cursor in JFrame import javax.swing.*; import java.awt.*; public class CustomCursor extends JFrame { public static void main(String[] args) { JFrame fr = new JFrame(); // Creating a JFrame fr.setTitle("Custom Cursor"); //Setting title of the frame fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Closing frame on close button fr.setBounds(100, 200, 350, 300); //Setting location of frame JPanel panel = new JPanel(); //Creating a JPanel Point point = new Point(0,0); //Creating a Point object //Creating two Jlabels Label label1 = new Label("CodeSpeedy Pointer"); Label label2 = new Label("Another Custom Pointer"); //Initializing the Toolkit Toolkit tkit=Toolkit.getDefaultToolkit(); //Extractig the Images for the custom cursor Image img1 = tkit.getImage("CodeSpeedy-Logo.png"); Image img2 = tkit.getImage("Custom-2.png"); //Creating two custom cursors Cursor cursor1 = tkit.createCustomCursor(img1, point, "CodeSpeedy"); Cursor cursor2 = tkit.createCustomCursor(img2, point, "YellowCursor"); //Setting cursors to the label label1.setCursor(cursor1); label2.setCursor(cursor2); //Adding labels to the panel panel.add(label1); panel.add(label2); //Adding panel to JFrame fr.add(panel); fr.setVisible(true); // Displaying the frame } }
Output:
Analysis:
We observe that when we hover to the CodeSpeedy Pointer label, our cursor gets changed to the logo of the CodeSpeedy. Also, when we hover to the Another Pointer label, our cursor pointer again changes. Therefore, we have achieved adding the custom cursor to our JFrame.
I hope you have found this post useful!
Leave a Reply