让jLabel变得有立体

public class RichJLabel extends JLabel {

 private int tracking;//调节字符间的距离
 public RichJLabel(String text, int tracking) {
  super(text);
  this.tracking = tracking;
 }
 
 private int left_x, left_y, right_x, right_y;
 private Color left_color, right_color;
 public void setLeftShadow(int x, int y, Color color) {
  left_x = x;
  left_y = y;
  left_color = color;
 }
 
 public void setRightShadow(int x, int y, Color color) {
  right_x = x;
  right_y = y;
  right_color = color;
 }
 
 public Dimension getPreferredSize() {
  String text = getText();
  FontMetrics fm = this.getFontMetrics(getFont());
  
  int w = fm.stringWidth(text);
  if(tracking<=0) {
   w -= (text.length()-1) * tracking;
  } else {
   w += (text.length()-1) * tracking;
  }
//  w += (text.length()-1) * tracking;
  w += left_x + right_x;

  int h = fm.getHeight();
  h += left_y + right_y;
  return new Dimension(w, h);
 }

 @Override
 protected void paintComponent(Graphics g) {
 // super.paintComponent(g);//此处是为了对比效果才有的,可不用。
  ((Graphics2D)g).setRenderingHint(
    RenderingHints.KEY_TEXT_ANTIALIASING,
    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  
  char[] chars = getText().toCharArray();
  
  FontMetrics fm = this.getFontMetrics(getFont());
  
  int h = fm.getAscent();
  int x = 0;
  for(int i=0; i<chars.length; i++) {
   char ch = chars[i];
   int w = fm.charWidth(ch) + tracking;
   
   g.setColor(left_color);
   g.drawString(" "+chars[i], x-left_x, h-left_y);
   
   g.setColor(right_color);
   g.drawString(" " + chars[i], x+right_x, h+right_y);
   
   g.setColor(getForeground());
   g.drawString(" "+chars[i], x, h);
   x += w;
  }
  ((Graphics2D)g).setRenderingHint(
    RenderingHints.KEY_TEXT_ANTIALIASING,
    RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT);
 }
 
 public static void main(String[] args) {
 /* 
     //drop shadow w / hightlight
  RichJLabel label = new RichJLabel("76", -10);
  label.setLeftShadow(1, 1, Color.white);
  label.setRightShadow(2, 3, Color.black);
  label.setForeground(Color.gray);
  label.setFont(label.getFont().deriveFont(140f));
 */
  
 /* 
     // subtle outline
  RichJLabel label = new RichJLabel("76", -30);
  label.setLeftShadow(1, 1, Color.white);
  label.setRightShadow(1, 1, Color.white);
  label.setForeground(Color.blue);
  label.setFont(label.getFont().deriveFont(140f));
 */
  
  // 3d letters
  RichJLabel label = new RichJLabel("76", -30);
  label.setLeftShadow(5, 5, Color.white);
  label.setRightShadow(-3, -3, new Color(0xccccff));
  label.setForeground(new Color(0x8888ff));
  label.setFont(label.getFont().deriveFont(140f));
  
  JFrame frame = new JFrame("RichJLabel hack");
  frame.getContentPane().add(label);
 // frame.setExtendedState(JFrame.MAXIMIZED_BOTH);//屏幕最大化
  Dimension d = label.getPreferredSize();
  frame.setSize(d);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//让JFrame的关闭按钮起作用
  frame.setVisible(true);//显示JFrame
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值