Home > Java > javaTutorial > body text

How to Create a Rounded Border with Transparency in Java?

Barbara Streisand
Release: 2024-11-17 16:23:02
Original
149 people have browsed it

How to Create a Rounded Border with Transparency in Java?

Border with Rounded Corners and Transparency

This question tackles the issue of creating a rounded border with transparency, allowing the underlying component to show through. The solution involves modifying the TextBubbleBorder class to paint the background color of the parent outside the border's clip region.

Solution:

The modification made to the TextBubbleBorder class is as follows:

// 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 retrieves the parent component and its background color. It then creates an area representing the border region and subtracts the bubble and pointer areas from it. This defines the region outside the border.

With the clip region set, the code fills the region with the parent component's background color, making the border transparent outside the rounded corners.

Additional Considerations:

  • A bug exists in the original code, causing a clipping issue. This bug has been fixed in the "clipping bug fix" for the paintComponent() method.
  • By setting the pointer size to 0, the border transforms into a standard rounded rectangle.
  • A parameter left is introduced in the TextBubbleBorder constructor to create right-aligned bubbles if set to false.

The above is the detailed content of How to Create a Rounded Border with Transparency in Java?. 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