Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

80
LINES

< > BotCompany Repo | #1011147 // ShadowJLabel

JavaX fragment (include)

1  
sclass ShadowJLabel extends BetterLabel {
2  
  int tracking;
3  
4  
  *() {}
5  
  
6  
  *(S text, int tracking) {
7  
      super(text);
8  
      this.tracking = tracking;
9  
  }
10  
11  
  private int left_x, left_y, right_x, right_y;
12  
  private Color left_color, right_color;
13  
14  
  public void setLeftShadow(int x, int y, Color color) {
15  
      left_x = x;
16  
      left_y = y;
17  
      left_color = color;
18  
  }
19  
20  
  public void setRightShadow(int x, int y, Color color) {
21  
      right_x = x;
22  
      right_y = y;
23  
      right_color = color;
24  
  }
25  
26  
  public Dimension getPreferredSize() {
27  
       FontMetrics fm = this.getFontMetrics(getFont());
28  
29  
       int w = totalWidth();
30  
       int h = fm.getHeight();
31  
       h += left_y + right_y;
32  
33  
       return new Dimension(w,h);
34  
  }
35  
  
36  
  int totalWidth() {
37  
    String text = getText();
38  
    FontMetrics fm = this.getFontMetrics(getFont());
39  
    int w = fm.stringWidth(text);
40  
    w += (text.length()-1)*tracking;
41  
    w += left_x + right_x;
42  
    ret w;
43  
  }
44  
45  
  public void paintComponent(Graphics g) {
46  
      ((Graphics2D)g).setRenderingHint(
47  
          RenderingHints.KEY_TEXT_ANTIALIASING,
48  
          RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
49  
      char[] chars = getText().toCharArray();
50  
51  
      FontMetrics fm = this.getFontMetrics(getFont());
52  
53  
      int ha = getHorizontalAlignment();
54  
      int va = getVerticalAlignment();
55  
      int h = fm.getAscent();
56  
      if (va == SwingConstants.CENTER) h += (getHeight()-fm.getHeight())/2;
57  
      int x = ha == SwingConstants.CENTER ? (getWidth()-totalWidth())/2 : 0;
58  
59  
      for(int i=0; i<chars.length; i++) {
60  
          char ch = chars[i];
61  
          int w = fm.charWidth(ch) + tracking;
62  
63  
          g.setColor(left_color);
64  
          g.drawString(""+chars[i],x-left_x,h-left_y);
65  
66  
          g.setColor(right_color);
67  
          g.drawString(""+chars[i],x+right_x,h+right_y);
68  
69  
          g.setColor(getForeground());
70  
          g.drawString(""+chars[i],x,h);
71  
72  
          x+=w;
73  
  }
74  
75  
      ((Graphics2D)g).setRenderingHint(
76  
          RenderingHints.KEY_TEXT_ANTIALIASING,
77  
          RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT);
78  
79  
  } // end paintComponent()
80  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1011147
Snippet name: ShadowJLabel
Eternal ID of this version: #1011147/9
Text MD5: 2a115e5a56c5626d1bfeb44e0c01270b
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-10-15 23:42:13
Source code size: 2150 bytes / 80 lines
Pitched / IR pitched: No / No
Views / Downloads: 514 / 1132
Version history: 8 change(s)
Referenced in: [show references]