Java Swing Border createRoundedEmptyBorder(final int top, final int left, final int bottom, final int right, final int arc, final Color color)

Here you can find the source of createRoundedEmptyBorder(final int top, final int left, final int bottom, final int right, final int arc, final Color color)

Description

create Rounded Empty Border

License

Open Source License

Parameter

Parameter Description
top a parameter
left a parameter
bottom a parameter
right a parameter
arc a parameter
color a parameter

Declaration

public static Border createRoundedEmptyBorder(final int top, final int left, final int bottom, final int right,
        final int arc, final Color color) 

Method Source Code


//package com.java2s;
/*//from w ww .ja  v a  2 s  . co 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/>.
 */

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

  1. createCompoundBorder(Border... borders)
  2. createDebugBorder(JComponent c, Color color)
  3. createFormSectionBorder(Color color, String title)
  4. createJScrollPane(Component component, Rectangle bounds, Color backgroundColor, boolean noBorder, boolean visible)
  5. createRightBorder(int size, Color color)
  6. createTitledBorder(Color color, String name)
  7. createVerticalBox(Border border, Component... comps)
  8. drawBorderTrans(Graphics2D g, JComponent focusOwner, JComponent area)
  9. drawBorderWhite(Graphics2D g, JComponent focusOwner, JComponent area)