create Rounded Empty Border - Java Swing

Java examples for Swing:Border

Description

create Rounded Empty Border

Demo Code

/*//from www .ja  va2  s  .  c o  m
 * Copyright (C) 2011 Trilarion
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;
import java.awt.Color;
import java.awt.Component;

import java.awt.Graphics;
import java.awt.Graphics2D;

import java.awt.Insets;

import java.awt.Shape;

import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;

import javax.swing.border.Border;

public class Main {
    /**
     *
     * @param top
     * @param left
     * @param bottom
     * @param right
     * @param arc
     * @param color
     * @return
     */
    public static Border createRoundedEmptyBorder(final int top,
            final int left, final int bottom, final int right,
            final int arc, final Color color) {
        if (top < 0 | left < 0 | bottom < 0 | right < 0 | arc < 0) {
            throw new IllegalArgumentException();
        }
        final int dr = (int) Math.ceil((1 - 1 / Math.sqrt(2)) * arc);
        return new Border() {
            @Override
            public void paintBorder(Component c, Graphics g, int x, int y,
                    int width, int height) {
                if (c.isOpaque()) {
                    Graphics2D g2d = (Graphics2D) g;

                    Color oldColor = g2d.getColor();

                    g2d.setColor(color);

                    Shape outer, inner;

                    outer = new RoundRectangle2D.Float(left, top, width
                            - left - right, height - top - bottom, arc, arc);
                    inner = new Rectangle2D.Float(left + dr, top + dr,
                            width - left - right - 2 * dr, height - top
                                    - bottom - 2 * dr);

                    Path2D path = new Path2D.Float(Path2D.WIND_EVEN_ODD);

                    path.append(outer, false);
                    path.append(inner, false);

                    g2d.fill(path);

                    g2d.setColor(oldColor);
                }
            }

            @Override
            public Insets getBorderInsets(Component c) {
                return new Insets(top + dr, left + dr, right + dr, bottom
                        + dr);
            }

            @Override
            public boolean isBorderOpaque() {
                return true;
            }
        };
    }
}

Related Tutorials