1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Test {
public static void makeBorderedFont(JButton button, Color border, float size) {
Font font = button.getFont();
Color color = button.getForeground();
String text = button.getText();
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
int width = img.getGraphics().getFontMetrics(font).stringWidth(text);
int height = img.getGraphics().getFontMetrics(font).getHeight();
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D)img.getGraphics();
g2d.setFont(font);
g2d.setColor(border);
g2d.drawString(text,size,height/2F+size);
g2d.drawString(text,-size,height/2F-size);
g2d.setColor(color);
g2d.drawString(text,0,height/2);
g2d.dispose();
button.setText(null);
button.setIcon(new ImageIcon(img));
}
public static void makeBorderedFont(JLabel label, Color border, float size) {
Font font = label.getFont();
Color color = label.getForeground();
String text = label.getText();
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
int width = img.getGraphics().getFontMetrics(font).stringWidth(text);
int height = img.getGraphics().getFontMetrics(font).getHeight();
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D)img.getGraphics();
g2d.setFont(font);
g2d.setColor(border);
g2d.drawString(text,size,height/2F+size);
g2d.drawString(text,-size,height/2F-size);
g2d.drawString(text,-size,height/2F+size);
g2d.drawString(text,size,height/2F-size);
g2d.setColor(color);
g2d.drawString(text,0,height/2);
g2d.dispose();
label.setText(null);
label.setIcon(new ImageIcon(img));
}
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Label", JLabel.CENTER);
label.setFont(new Font("Arial", Font.PLAIN, 22));
label.setForeground(Color.WHITE);
makeBorderedFont(label, Color.GREEN, 1.2F);
frame.getContentPane().add(label, BorderLayout.WEST);
JButton button = new JButton("Bouton");
button.setFocusPainted(false);
button.setFont(new Font("Papyrus", Font.BOLD|Font.ITALIC, 20));
button.setForeground(Color.BLACK);
makeBorderedFont(button, Color.RED, 1F);
frame.getContentPane().add(button, BorderLayout.EAST);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
} |
Partager