Creating Custom Cursors
The ARGB color model stores each pixel with alpha, red, green, and blue components, and the alpha channel controls transparency.
A Cursor With a See-Through Center
Earlier in this chapter you learned several ways to draw with Graphics2D. This section puts those ideas together in one practical example: a custom mouse cursor. The source shows a method that sets the cursor of a component to a red square whose interior is transparent. Whatever sits underneath the cursor shows through the middle of the square.
The example is presented as a fun demonstration, but it touches the core ideas of this module. To understand why the interior can be see-through, you first need to know how Java represents transparency in an image. Keep that question in mind as you read the next section, because the whole cursor depends on it.
Four Channels Packed Into Each Pixel
The ARGB color model stores each pixel with alpha, red, green, and blue components. The alpha channel controls transparency. The name lists the four components in that order: A for alpha, then R, G, and B.
Think of a single pixel as one color value made of four parts. Three of the parts describe the visible color, which is red, green, and blue mixed together. The fourth part, alpha, does not describe a color at all. It describes how much of that pixel is allowed to show. Every pixel in an ARGB image carries all four parts at once, so transparency is a property of each individual pixel rather than of the whole image.
The diagram shows the four components side by side in the order given by the name. Alpha is the first component, and it is the one that decides whether the other three are visible at that position.
How Alpha Decides What Shows Through
The source states the key rule directly: a color with zero alpha is fully transparent. A pixel whose alpha is zero contributes nothing visible, so the background behind it shows through. The source also names the image type that supports this. BufferedImage.TYPE_INT_ARGB is the image type used when transparency is needed.
What do you think happens?
The method uses a background color built with four arguments, where the last argument is alpha. If that background color has zero alpha, what should the interior of the square look like?
Reveal answer
The interior is see-through, so whatever is underneath the cursor shows through the middle of the square.
A color with zero alpha is fully transparent. Filling the background with it leaves the interior pixels transparent, which is why the red border is the only visible part of the cursor.
Tracing the Red Square Method
The source shows the method that creates the cursor. It builds a 24 by 24 image, draws three concentric red rectangles to form a thick border, and then hands the image to the Java toolkit as a custom cursor. The method has two inputs for the toolkit: the image and a hot spot. Everything else in the method exists to produce the image.
Read the stages as a chain of data flow. First the image type makes transparency possible. Next the zero-alpha background decides which pixels start out transparent. Then the red drawing calls decide which pixels become visible, forming the square outline. Finally the hot spot tells the system which point of the image is the actual click point of the cursor.
Predicting the visible shape of the cursor
Starting from the red square method, what does the finished cursor look like, and which pixels are transparent?
- 1Image type
The image is an ARGB image, so each pixel carries an alpha value and can be transparent.
- 2Background
The background is built with zero alpha, so every pixel begins fully transparent.
- 3Drawing
Three concentric red rectangles are drawn over the image. Those red pixels are visible, forming a thick border.
- 4Cursor creation
The image is handed to the toolkit with a hot spot that marks the click point.
The cursor is a hollow red square. The border pixels are visible, and the interior pixels, which the drawing never touched, remain fully transparent and show whatever is underneath.
Which Pixels End Up Transparent
A useful way to see the result is to look at the pixel grid. The diagram below is an illustrative excerpt of one row across the cursor, not the exact 24 by 24 geometry from the source. It shows the pattern that the method produces: the drawn border cells are opaque, and the cells the drawing never reached keep their zero alpha from the background.
The boundary sits where the red drawing stops. Pixels inside the square are the ones the drawing calls never reached, so they keep the zero alpha that the background gave them. Pixels on the ring are the ones the drawing calls covered, so they are visible. Predicting the transparent region therefore means asking which pixels the drawing calls did not touch.
Where a Transparent Cursor Can Diverge
Suppose you copy the method and the cursor appears as a solid square instead of a hollow one with a see-through center. The result has diverged from what you expected. The source gives a diagnostic order for finding the step where it went wrong: check the image type, then the background alpha, then the drawing calls. Work through the stages in the order the data flows.
Common mistake Assuming the interior is red because the square looks red
What it looks likeReading the cursor as a red square and expecting every pixel to be red.
Why it failsThe red drawing calls only cover the border. The interior was filled with a zero-alpha background, so it stays transparent.
Do this insteadIdentify which pixels the drawing calls touched. Only those are opaque red.
Common mistake Looking at the background alpha only and skipping the image type
What it looks likeA zero-alpha background is set, but the image is not created with the transparency-capable type.
Why it failsTransparency depends on the image type as well as the pixel values. The source names BufferedImage.TYPE_INT_ARGB as the type used when transparency is needed.
Do this insteadCheck the image type first, then the background alpha, then the drawing calls.
Common mistake Treating the hot spot as the center of the square
Why it failsThe source says the hot spot tells the system which point of the image is the actual click point of the cursor. It is a separate input from the picture.
Do this insteadRead the hot spot value as the click point, and do not assume it sits at the middle of the image.
Common mistake Expecting a solid result after drawing the border with an opaque background that was never set to zero alpha
Practice Tracing the Cursor
Suppose the red square method is copied exactly, except that the background is built with an alpha value of 255 instead of 0. Predict whether the interior of the cursor is transparent, and name the stage where the result diverges from the original method.
Hints
- Recall that a color with zero alpha is fully transparent.
- Ask which stage of the data flow the change happens in: image type, background, drawing, or cursor creation.
- Compare the interior of the square with and without the change.
Key Takeaways
- The ARGB color model stores each pixel with alpha, red, green, and blue components, and the alpha channel controls transparency.
- A color with zero alpha is fully transparent, and BufferedImage.TYPE_INT_ARGB is the image type used when transparency is needed.
- The red square cursor method builds a 24 by 24 image, fills it with a zero-alpha background, draws three concentric red rectangles for a thick border, and sets the cursor with a hot spot.
- The interior of the cursor stays transparent because the drawing calls never touch it, so the zero-alpha background shows through.
- When a transparent result looks wrong, check the image type, the background alpha, and the drawing calls, in that order.