Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create BevelBorder of certain width in Java Swing

Tags:

java

swing

I am trying to create a Button like below using combination of Borders.

While using BorderFactory or Bevel classes, there is no option to give the width.

Is it possible to give width to BevelBorder in Java Swing?

enter image description here

like image 912
Barun Avatar asked Aug 16 '16 05:08

Barun


People also ask

How to add borders in Java Swing?

To put a border around a JComponent , you use its setBorder method. You can use the BorderFactory class to create most of the borders that Swing provides. If you need a reference to a border — say, because you want to use it in multiple components — you can save it in a variable of type Border .

What is border factory in Java?

The BorderFactory is a Factory class which provides different types of borders in Java.


1 Answers

No, you cannot set the width of a BevelBorder. A BevelBorder just draws two 1px lines per edge - one for the outer shadow and one for the inner shadow. Unfortunately, just calling setStroke wouldn't work, because on each corner one color would just overlap the other and it would come to other unpleasant visuals.

Also, what you seem to request isn't really a BevelBorder. It's a border with 5 color specifications: The top, right, bottom, left and line color. A BevelBorder doesn't have a line color and also doesn't have such color specifications.

I've made a class that extends AbstractBorder which should fit your requirements:

enter image description here


import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.RenderingHints;

import javax.swing.border.AbstractBorder;

public class AdvancedBevelBorder extends AbstractBorder {

    private Color topColor, rightColor, bottomColor, leftColor, lineColor;
    private int borderWidth;

    public AdvancedBevelBorder(Color topColor, Color rightColor, Color bottomColor, Color leftColor, Color lineColor,
            int borderWidth) {
        setTopColor(topColor);
        setRightColor(rightColor);
        setBottomColor(bottomColor);
        setLeftColor(leftColor);
        setLineColor(lineColor);
        setBorderWidth(borderWidth);
    }

    @Override
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
        super.paintBorder(c, g, x, y, width, height);

        int h = height;
        int w = width;
        int bw = getBorderWidth();
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.translate(x, y);

        Polygon topPolygon = createPolygon(new Point(0, 0), new Point(w, 0), new Point(w - bw, bw), new Point(bw, bw),
                new Point(0, 0));
        g2.setColor(getTopColor());
        g2.fill(topPolygon);
        g2.setColor(getLineColor());
        g2.draw(topPolygon);

        Polygon rightPolygon = createPolygon(new Point(w - 1, 0), new Point(w - 1, h), new Point(w - bw - 1, h - bw),
                new Point(w - bw - 1, bw), new Point(w - 1, 0));
        g2.setColor(getRightColor());
        g2.fill(rightPolygon);
        g2.setColor(getLineColor());
        g2.draw(rightPolygon);

        Polygon bottomPolygon = createPolygon(new Point(0, h - 1), new Point(w, h - 1), new Point(w - bw, h - bw - 1),
                new Point(bw, h - bw - 1), new Point(0, h - 1));
        g2.setColor(getBottomColor());
        g2.fill(bottomPolygon);
        g2.setColor(getLineColor());
        g2.draw(bottomPolygon);

        Polygon leftPolygon = createPolygon(new Point(0, 0), new Point(0, h), new Point(bw, h - bw), new Point(bw, bw),
                new Point(0, 0));
        g2.setColor(getLeftColor());
        g2.fill(leftPolygon);
        g2.setColor(getLineColor());
        g2.draw(leftPolygon);

        g2.dispose();
    }

    @Override
    public Insets getBorderInsets(Component c) {
        return new Insets(getBorderWidth(), getBorderWidth(), getBorderWidth() + 1, getBorderWidth() + 1);
    }

    private Polygon createPolygon(Point... points) {
        Polygon polygon = new Polygon();
        for (Point point : points) {
            polygon.addPoint(point.x, point.y);
        }
        return polygon;
    }

    public Color getTopColor() {
        return topColor;
    }

    public void setTopColor(Color topColor) {
        this.topColor = topColor;
    }

    public Color getRightColor() {
        return rightColor;
    }

    public void setRightColor(Color rightColor) {
        this.rightColor = rightColor;
    }

    public Color getBottomColor() {
        return bottomColor;
    }

    public void setBottomColor(Color bottomColor) {
        this.bottomColor = bottomColor;
    }

    public Color getLeftColor() {
        return leftColor;
    }

    public void setLeftColor(Color leftColor) {
        this.leftColor = leftColor;
    }

    public Color getLineColor() {
        return lineColor;
    }

    public void setLineColor(Color lineColor) {
        this.lineColor = lineColor;
    }

    public int getBorderWidth() {
        return borderWidth;
    }

    public void setBorderWidth(int borderWidth) {
        this.borderWidth = borderWidth;
    }

}

By the way, I made the component you can see in image with the following code:

AdvancedBevelBorder border = new AdvancedBevelBorder(new Color(120, 172, 220), new Color(55, 93, 128),
        new Color(73, 124, 169), new Color(150, 191, 229), new Color(36, 83, 126), 10);
JPanel panel = new JPanel() {
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 60);
    }
};
panel.setBackground(new Color(91, 154, 212));
panel.setBorder(border);
like image 108
Lukas Rotter Avatar answered Oct 20 '22 01:10

Lukas Rotter