Advertisement
sedran

Java Swing - TextBorder

Jan 29th, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. /**
  2.  * Yazar: Serdar KUZUCU
  3.  * http://blog.asosyalbebe.com/
  4.  */
  5. package sedran.borders;
  6.  
  7. import java.awt.Color;
  8. import java.awt.Component;
  9. import java.awt.Font;
  10. import java.awt.FontMetrics;
  11. import java.awt.Graphics;
  12. import java.awt.Insets;
  13. import java.awt.geom.Rectangle2D;
  14.  
  15. import javax.swing.border.Border;
  16.  
  17. public class TextBorder implements Border {
  18.     private String title = "";
  19.     private Font f = new Font("Tahoma", Font.PLAIN, 11);
  20.     private Insets insets = new Insets(23, 9, 9, 9);
  21.     private Color line_color = new Color(148,145,140);
  22.     private Color text_color = new Color(20,20,20);
  23.     private boolean rounded = false;
  24.  
  25.     public TextBorder(String title) {
  26.         this.title = title;
  27.     }
  28.  
  29.     public void setText(String text) {
  30.         this.title = text;
  31.     }
  32.  
  33.     public void setBorderColor(Color c) {
  34.         line_color = c;
  35.     }
  36.  
  37.     public void setTextColor(Color c) {
  38.         text_color = c;
  39.     }
  40.  
  41.     public void setRounded(boolean round) {
  42.         rounded = round;
  43.     }
  44.  
  45.     public void setFont(Font f) {
  46.         this.f = f;
  47.     }
  48.  
  49.     public Font getFont() {
  50.         return this.f;
  51.     }
  52.    
  53.     public String getText() {
  54.         return title;
  55.     }
  56.  
  57.     public Insets getBorderInsets(Component c) {
  58.         return new Insets(insets.top, insets.left, insets.bottom, insets.right);
  59.     }
  60.  
  61.     public boolean isBorderOpaque() {
  62.         return true;
  63.     }
  64.    
  65.     public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
  66.         int roundWidth = rounded ? 15 : 0;
  67.         g.setColor(line_color);
  68.         g.drawRoundRect(x+3, y+10, width-6, height-13, roundWidth, roundWidth);
  69.         g.setColor(Color.WHITE);
  70.         g.drawRoundRect(x+4, y+11, width-8, height-15, roundWidth, roundWidth);
  71.  
  72.         g.setFont(f);
  73.         FontMetrics fm = g.getFontMetrics(f);
  74.         Rectangle2D textsize = fm.getStringBounds(title, g);
  75.         g.setColor(c.getBackground());
  76.         g.fillRect(x+12, y+9, (int)textsize.getWidth()+6, 4);
  77.         g.setColor(text_color);
  78.         g.drawString(title, x+15, y+fm.getAscent()+3);
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement