Home > Java > javaTutorial > How to Achieve Transparent Corners in a Rounded Rectangle with a Triangular Pointer?

How to Achieve Transparent Corners in a Rounded Rectangle with a Triangular Pointer?

Barbara Streisand
Release: 2024-11-07 22:11:02
Original
798 people have browsed it

How to Achieve Transparent Corners in a Rounded Rectangle with a Triangular Pointer?

Transparent Corners in Border with Rounded Corners

In the given code, the TextBubbleBorder class paints a rounded rectangle with a triangular pointer at the bottom. However, the corners outside the rectangle extend a bit, showing the background color of the parent panel. To achieve transparent corners, we modify the paintBorder method to include an additional step:

// Paint the BG color of the parent, everywhere outside the clip
// of the text bubble.
Component parent  = c.getParent();
if (parent!=null) {
    Color bg = parent.getBackground();
    Rectangle rect = new Rectangle(0,0,width, height);
    Area borderRegion = new Area(rect);
    borderRegion.subtract(area);
    g2.setClip(borderRegion);
    g2.setColor(bg);
    g2.fillRect(0, 0, width, height);
    g2.setClip(null);
}
Copy after login

This code checks if the component has a parent, retrieves its background color, and creates a rectangle representing the entire border region. It then creates an Area object borderRegion that represents this rectangle. Next, it subtracts the area representing the text bubble from the borderRegion, creating an Area called clip that represents the area outside the text bubble.

With clip, the code sets the clipping region for the Graphics2D object, fills it with the parent's background color, and then resets the clipping region to draw the border itself. This ensures that the corners outside the rounded rectangle become transparent, showing the parent's background color.

The above is the detailed content of How to Achieve Transparent Corners in a Rounded Rectangle with a Triangular Pointer?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template