codice:
package it.test;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class OvalBorder implements Border {
protected int w = 6;
protected int h = 6;
protected Color topColor = Color.white;
protected Color bottomColor = Color.gray;
public OvalBorder() {
w = 6;
h = 6;
}
public OvalBorder(int w, int h) {
this.w = w;
this.h = h;
}
public OvalBorder(int w, int h, Color topColor, Color bottomColor) {
this.w = w;
this.h = h;
this.topColor = topColor;
this.bottomColor = bottomColor;
}
public Insets getBorderInsets(Component c) {
return new Insets(h, w, h, w);
}
public boolean isBorderOpaque() {
return true;
}
public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
w--;
h--;
g.setColor(topColor);
g.drawLine(x, y + h - h, x, y + h);
g.drawArc(x, y, 2 * w, 2 * h, 180, -90);
g.drawLine(x + w, y, x + w - w, y);
g.drawArc(x + w - 2 * w, y, 2 * w, 2 * h, 90, -90);
g.setColor(bottomColor);
g.drawLine(x + w, y + h, x + w, y + h - h);
g.drawArc(x + w - 2 * w, y + h - 2 * h, 2 * w, 2 * h, 0, -90);
g.drawLine(x + w, y + h, x + w - w, y + h);
g.drawArc(x, y + h - 2 * h, 2 * w, 2 * h, -90, -90);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Custom Border: OvalBorder");
JLabel label = new JLabel("Etichetta con OvalBorder");
JButton button = new JButton("Bottone con OvalBorder");
Container c = frame.getContentPane();
c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
((JPanel) c).setBorder(new EmptyBorder(10,10,10,10));
button.setBorder(new OvalBorder(10, 10));
label.setBorder(new OvalBorder(10, 10));
c.add(button);
c.add(Box.createVerticalGlue());
c.add(label);
frame.setBounds(0, 0, 300, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}