SimpleBorder.java Source code

Java tutorial

Introduction

Here is the source code for SimpleBorder.java

Source

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;

class SimpleBorder implements Border {
    int top, left, bottom, right;
    Color color = null;

    public SimpleBorder() {
        this.top = 2;
        this.left = 4;
        this.bottom = 8;
        this.right = 10;
        this.color = Color.RED;
    }

    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
        Insets insets = getBorderInsets(c);
        if (color != null)
            g.setColor(color);

        g.fill3DRect(0, 0, width - insets.right, insets.top, true);

        g.fill3DRect(0, insets.top, insets.left, height - insets.top, true);
        g.fill3DRect(insets.left, height - insets.bottom, width - insets.left, insets.bottom, true);
        g.fill3DRect(width - insets.right, 0, insets.right, height - insets.bottom, true);
    }

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

    public boolean isBorderOpaque() {
        return true;
    }
}

public class Main {
    public static void main(String[] a) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton button = new JButton("Aaaaaaaaaaa");
        button.setBorder(new SimpleBorder());
        frame.add(button);
        frame.pack();
        frame.setVisible(true);
    }
}