create a new border
807605Sep 11 2007 — edited Sep 11 2007this border is used to underline a Label
import java.awt.*;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.*;
import java.awt.*;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class createUnderLineBorder extends AbstractBorder
{
protected Color lineColor;
protected String s="";
protected int size;
public createUnderLineBorder(String b) {
this.s=b;
this.lineColor=Color.BLACK;
this.size=19;
}
public createUnderLineBorder(String a,Color lineColor) {
this.lineColor = lineColor;
this.s=a;
this.size=19;
}
public createUnderLineBorder(String a,int size1,Color lineColor) {
this.lineColor = lineColor;
this.s=a;
this.size=size1;
}
public void paintBorder(Component component,
Graphics g, int x, int y, int w, int h) {
Graphics copy = g.create();
if(copy != null) {
try {
copy.translate(x,y);
paintLine(component,copy,w,h);
}
finally {
copy.dispose();
}
}
}
public Insets getBorderInsets() {
return new Insets(size,size,size,size);
}
protected void paintLine(Component c, Graphics g,
int w, int h) {
g.setColor(lineColor);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Font plainFont = new Font("Times New Roman", Font.PLAIN, size);
AttributedString as = new AttributedString(s);
as.addAttribute(TextAttribute.FONT, plainFont);
as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, 0,s.length());
// as.addAttribute(TextAttribute.STRIKETHROUGH,
// TextAttribute.STRIKETHROUGH_ON, 18, 25);
g2.drawString(as.getIterator(), 24, 180);
}
the main program is :
import javax.swing.*;
import javax.swing.BorderFactory;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.UIManager;
public class Test extends JFrame {
public static void main(String[] args)throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JLabel jlabel1=new JLabel();
jlabel1.setBorder(new createUnderLineBorder("elie",100,Color.RED));
JLabel jlabel2=new JLabel();
jlabel2.setBorder(new createUnderLineBorder("yazbeck",Color.blue));
JLabel jlabel3=new JLabel();
jlabel3.setBorder(new createUnderLineBorder("hanna"));
JFrame frame = new JFrame("ya mama");
frame .getContentPane().setLayout(new GridLayout(1,10));
frame.getContentPane().add(jlabel1);
frame.getContentPane().add(jlabel2);
frame.getContentPane().add(jlabel3);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(640,480);
frame.setLocation(100,100);
frame.setVisible(true);
}
}