All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alee.painter.common.BorderPainter Maven / Gradle / Ivy

/*
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library 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.
 *
 * WebLookAndFeel library 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 WebLookAndFeel library.  If not, see .
 */

package com.alee.painter.common;

import com.alee.managers.style.Bounds;
import com.alee.painter.AbstractPainter;
import com.alee.utils.GraphicsUtils;

import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RectangularShape;
import java.awt.geom.RoundRectangle2D;

/**
 * Simple border painter.
 * This painter might be used instead of {@link javax.swing.border.LineBorder} in any component that supports painters.
 *
 * @param  component type
 * @param  component UI type
 * @author Mikle Garin
 * @see AbstractPainter
 * @see com.alee.painter.Painter
 */

public class BorderPainter extends AbstractPainter
{
    /**
     * Border round.
     */
    protected Integer round;

    /**
     * Border stroke.
     */
    protected Stroke stroke;

    /**
     * Border color.
     */
    protected Color color;

    /**
     * Constructs default border painter.
     */
    public BorderPainter ()
    {
        super ();
    }

    /**
     * Constructs border painter with a specified color.
     *
     * @param color border color
     */
    public BorderPainter ( final Color color )
    {
        super ();
        this.color = color;
    }

    /**
     * Returns border round.
     *
     * @return border round
     */
    public int getRound ()
    {
        return round != null ? round : 0;
    }

    /**
     * Sets border round.
     * This will also force stroke to update and overwrite old stroke value.
     *
     * @param round new border round
     */
    public void setRound ( final int round )
    {
        this.round = round;
    }

    /**
     * Returns border stroke.
     *
     * @return border stroke
     */
    public Stroke getStroke ()
    {
        return stroke;
    }

    /**
     * Returns stroke width.
     *
     * @return stroke width
     */
    protected int getStrokeWidth ()
    {
        final Stroke stroke = getStroke ();
        return stroke != null && stroke instanceof BasicStroke ? Math.round ( ( ( BasicStroke ) stroke ).getLineWidth () ) : 1;
    }

    /**
     * Sets border stroke.
     *
     * @param stroke new border stroke
     */
    public void setStroke ( final Stroke stroke )
    {
        this.stroke = stroke;
    }

    /**
     * Returns border color.
     *
     * @return border color
     */
    public Color getColor ()
    {
        return color;
    }

    /**
     * Sets border color.
     *
     * @param color new border color
     */
    public void setColor ( final Color color )
    {
        this.color = color;
    }

    @Override
    protected Insets getBorder ()
    {
        final int width = getStrokeWidth ();
        return new Insets ( width, width, width, width );
    }

    @Override
    public void paint ( final Graphics2D g2d, final C c, final U ui, final Bounds bounds )
    {
        final Object aa = GraphicsUtils.setupAntialias ( g2d );
        final Stroke os = GraphicsUtils.setupStroke ( g2d, stroke, stroke != null );

        g2d.setPaint ( color );
        g2d.draw ( getBorderShape ( bounds.get () ) );

        GraphicsUtils.restoreStroke ( g2d, os, stroke != null );
        GraphicsUtils.restoreAntialias ( g2d, aa );
    }

    /**
     * Returns border shape for the specified limiting bounds.
     *
     * @param bounds limiting bounds
     * @return border shape for the specified limiting bounds
     */
    protected RectangularShape getBorderShape ( final Rectangle bounds )
    {
        final int round = getRound ();
        final int width = getStrokeWidth ();
        final double shear = width == 1 ? 0 : ( double ) width / 2;
        if ( round > 0 )
        {
            return new RoundRectangle2D.Double ( bounds.x + shear, bounds.y + shear, bounds.width - shear * 2 - 1,
                    bounds.height - shear * 2 - 1, round * 2, round * 2 );
        }
        else
        {
            return new Rectangle2D.Double ( bounds.x + shear, bounds.y + shear, bounds.width - shear * 2 - 1,
                    bounds.height - shear * 2 - 1 );
        }
    }

    @Override
    public Dimension getPreferredSize ()
    {
        final int width = getStrokeWidth ();
        return new Dimension ( Math.max ( width * 2, round * 2 ), Math.max ( width * 2, round * 2 ) );
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy