Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7570333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:23:50+00:00 2026-05-30T15:23:50+00:00

From the original question (below), I am now offering a bounty for the following:

  • 0

From the original question (below), I am now offering a bounty for the following:

An AlphaComposite based solution for rounded corners.

  • Please demonstrate with a JPanel.
  • Corners must be completely transparent.
  • Must be able to support JPG painting, but still have rounded corners
  • Must not use setClip (or any clipping)
  • Must have decent performance

Hopefully someone picks this up quick, it seems easy.

I will also award the bounty if there is a well-explained reason why this can never be done, that others agree with.

Here is a sample image of what I have in mind (but usingAlphaComposite)enter image description here


Original question

I’ve been trying to figure out a way to do rounded corners using compositing, very similar to How to make a rounded corner image in Java or http://weblogs.java.net/blog/campbell/archive/2006/07/java_2d_tricker.html.

However, my attempts without an intermediate BufferedImage don’t work – the rounded destination composite apparently doesn’t affect the source. I’ve tried different things but nothing works. Should be getting a rounded red rectangle, instead I’m getting a square one.

So, I have two questions, really:

1) Is there a way to make this work?

2) Will an intermediate image actually generate better performance?

SSCCE:

the test panel TPanel

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JLabel;

public class TPanel extends JLabel {
int w = 300;
int h = 200;

public TPanel() {
    setOpaque(false);
    setPreferredSize(new Dimension(w, h));
        setMaximumSize(new Dimension(w, h));
        setMinimumSize(new Dimension(w, h));
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();

    // Yellow is the clipped area.
    g2d.setColor(Color.yellow);
    g2d.fillRoundRect(0, 0, w, h, 20, 20);
    g2d.setComposite(AlphaComposite.Src);

    // Red simulates the image.
    g2d.setColor(Color.red);
    g2d.setComposite(AlphaComposite.SrcAtop);

    g2d.fillRect(0, 0, w, h);
    }
}

and its Sandbox

import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JFrame;

public class Sandbox {
public static void main(String[] args) {
    JFrame f = new JFrame();
        f.setMinimumSize(new Dimension(800, 600));
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new FlowLayout());

        TPanel pnl = new TPanel();
        f.getContentPane().add(pnl);

        f.setVisible(true);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:23:52+00:00Added an answer on May 30, 2026 at 3:23 pm

    With respect to your performance concerns the Java 2D Trickery article contains a link to a very good explanation by Chet Haase on the usage of Intermediate Images.

    I think the following excerpt from O’Reilly’s Java Foundation Classes in a Nutshell could be helpful to you in order to make sense of the AlphaComposite behaviour and why intermediate images may be the necessary technique to use.

    The AlphaComposite Compositing Rules

    The SRC_OVER compositing rule draws a possibly translucent source
    color over the destination color. This is what we typically want to
    happen when we perform a graphics operation. But the AlphaComposite
    object actually allows colors to be combined according to seven other
    rules as well.

    Before we consider the compositing rules in detail, there is an
    important point you need to understand. Colors displayed on the screen
    never have an alpha channel. If you can see a color, it is an opaque
    color. The precise color value may have been chosen based on a
    transparency calculation, but, once that color is chosen, the color
    resides in the memory of a video card somewhere and does not have an
    alpha value associated with it. In other words, with on-screen
    drawing, destination pixels always have alpha values of 1.0.

    The situation is different when you are drawing into an off-screen
    image, however. As you’ll see when we consider the Java 2D
    BufferedImage class later in this chapter, you can specify the desired
    color representation when you create an off-screen image. By default,
    a BufferedImage object represents an image as an array of RGB colors,
    but you can also create an image that is an array of ARGB colors. Such
    an image has alpha values associated with it, and when you draw into
    the images, the alpha values remain associated with the pixels you
    draw.

    This distinction between on-screen and off-screen drawing is important
    because some of the compositing rules perform compositing based on the
    alpha values of the destination pixels, rather than the alpha values
    of the source pixels. With on-screen drawing, the destination pixels
    are always opaque (with alpha values of 1.0), but with off-screen
    drawing, this need not be the case. Thus, some of the compositing
    rules only are useful when you are drawing into off-screen images that
    have an alpha channel.

    To overgeneralize a bit, we can say that when you are drawing
    on-screen, you typically stick with the default SRC_OVER compositing
    rule, use opaque colors, and vary the alpha value used by the
    AlphaComposite object. When working with off-screen images that have
    alpha channels, however, you can make use of other compositing rules.
    In this case, you typically use translucent colors and translucent
    images and an AlphaComposite object with an alpha value of 1.0.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SEE BELOW FOR SOLUTION BASED ON ANSWER/COMMENT from @Bertrand Le Roy ORIGINAL QUESTION: Not
(please read the update section below, I leave the original question too for clarity)
Note: Full working example now below. Original question follows: I'm having problems using ld's
So the problem changed from what it was, i'll leave the original question below
I want to generate Javascript code from an existing Java project ( original question
Edit: I'm looking for solution for this question now also with other programming languages.
SEE EDIT UPDATES BELOW. Original question has been modified! I have a working window
[EDITED: I left the original question below, with some more context and code to
update: the answer from virtualeyes (below) looks pretty nice: but now a bit code-sanititzing
My original question I've managed to answer by cobbling other answers together from this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.