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 9202107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:10:42+00:00 2026-06-17T23:10:42+00:00

For my java application i need a Round rectangle with an outline that looks

  • 0

For my java application i need a Round rectangle with an outline that looks like a normal rectangle, like this

enter image description here

I know you can do that by drawing a normal rectangle and a RoundRect inside it but i don’t want to draw a RoundRect inside it because I want to draw something else in it.

So a round rect with normal corners. How do I draw that in Java?

The problem is that the rectangle looks like this if I use layers:
enter image description hereThe corners are filled up with the wrong color. How do I prevent that?

  • 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-06-17T23:10:43+00:00Added an answer on June 17, 2026 at 11:10 pm

    I can think of two approaches. The first is to generate a Shape that represents the square outter edge and the rounded inner edge.

    The second would be to use a AlphaComposite to generate a masked result.

    enter image description here

    public class TestMask {
    
        public static void main(String[] args) {
            new TestMask();
        }
    
        public TestMask() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new MaskedPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
    
            });
        }
    
        public class MaskedPane extends JPanel {
    
            public MaskedPane() {
                setBackground(Color.RED);
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
    
                BufferedImage outter = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
                Graphics2D g2d = outter.createGraphics();
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.setColor(Color.BLACK);
                g2d.fillRect(0, 0, getWidth(), getHeight());
                g2d.dispose();
                BufferedImage inner = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
                g2d = inner.createGraphics();
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.setColor(Color.BLACK);
                g2d.fillRoundRect(10, 10, getWidth() - 20, getHeight() - 20, 20, 20);
                g2d.dispose();
    
                BufferedImage masked = applyMask(outter, inner, AlphaComposite.DST_OUT);
                g.drawImage(masked, 0, 0, this);
    
            }
    
            public BufferedImage applyMask(BufferedImage sourceImage, BufferedImage maskImage, int method) {
                BufferedImage maskedImage = null;
                if (sourceImage != null) {
    
                    int width = maskImage.getWidth();
                    int height = maskImage.getHeight();
    
                    maskedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
                    Graphics2D mg = maskedImage.createGraphics();
    
                    int x = (width - sourceImage.getWidth()) / 2;
                    int y = (height - sourceImage.getHeight()) / 2;
    
                    mg.drawImage(sourceImage, x, y, null);
                    mg.setComposite(AlphaComposite.getInstance(method));
                    mg.drawImage(maskImage, 0, 0, null);
                    mg.dispose();
                }
    
                return maskedImage;
            }
    
            public BufferedImage applyMask(BufferedImage sourceImage, BufferedImage maskImage) {
                return (BufferedImage) applyMask(sourceImage, maskImage, AlphaComposite.DST_IN);
            }
        }
    }
    

    Updated with Shape example

    enter image description here

    Finally had time to bang one out…

    public class TestMask {
    
        public static void main(String[] args) {
            new TestMask();
        }
    
        public TestMask() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new ShapedPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class ShapedPane extends JPanel {
    
            public ShapedPane() {
                setBackground(Color.GREEN);
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
    
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.setColor(Color.BLACK);
                g2d.fill(new RounedFrame(getWidth(), getHeight(), 10, 20));
                g2d.dispose();
            }
        }
    
        public class RounedFrame extends Path2D.Float {
    
            public RounedFrame(float width, float height, float thickness, float radius) {
    
                moveTo(0, 0);
                lineTo(width, 0);
                lineTo(width, height);
                lineTo(0, height);
                lineTo(0, 0);
    
                float innerWidth = width - thickness;
                float innerHeight = height - thickness;
    
                moveTo(thickness + radius, thickness);
                lineTo(innerWidth - radius, thickness);
    
                curveTo(innerWidth, thickness, innerWidth, thickness, innerWidth, thickness + radius);
    
                lineTo(innerWidth, innerHeight - radius);
                curveTo(innerWidth, innerHeight, innerWidth, innerHeight, innerWidth - radius, innerHeight);
                lineTo(thickness + radius, innerHeight);
                curveTo(thickness, innerHeight, thickness, innerHeight, thickness, innerHeight - radius);
                lineTo(thickness, thickness + radius);
                curveTo(thickness, thickness, thickness, thickness, thickness + radius, thickness);
    
                closePath();
    
                setWindingRule(WIND_EVEN_ODD);
    
            }
        }
    }
    

    Updated

    From a comment by Andrew, you could simplify the use of the shape example by using Area

    You could replace the paintComponent from the above example with this one…

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
    
        Area area = new Area(new Rectangle(0, 0, getWidth(), getHeight()));
        area.subtract(new Area(new RoundRectangle2D.Float(10, 10, getWidth() - 20, getHeight() - 20, 20, 20)));
    
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(Color.BLACK);
        g2d.fill(area);
        g2d.dispose();
    }
    

    Which is much simpler 😀

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

Sidebar

Related Questions

I need to write a java application that detects a USB device, and can
I need to write a MultiThreaded Java Application that will be used to load
I need to write a java application which can merge docx files. Any suggestions?
I need to call some PL/SQL procedures from my Java application. I can do
I need to make a Java application that stores calendar events in employees within
I have a Java application that I need to integrate our existing PHP website
i need to create an application in java that would convert text strings into
I have a Java application that parses files and stores the data I need
In my java web application i need to show the image that exits in
I'm running a Java application from Eclipse that need a lot of memory. Where

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.