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

80
LINES

< > BotCompany Repo | #1011147 // ShadowJLabel

JavaX fragment (include)

sclass ShadowJLabel extends BetterLabel {
  int tracking;

  *() {}
  
  *(S 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() {
       FontMetrics fm = this.getFontMetrics(getFont());

       int w = totalWidth();
       int h = fm.getHeight();
       h += left_y + right_y;

       return new Dimension(w,h);
  }
  
  int totalWidth() {
    String text = getText();
    FontMetrics fm = this.getFontMetrics(getFont());
    int w = fm.stringWidth(text);
    w += (text.length()-1)*tracking;
    w += left_x + right_x;
    ret w;
  }

  public void paintComponent(Graphics g) {
      ((Graphics2D)g).setRenderingHint(
          RenderingHints.KEY_TEXT_ANTIALIASING,
          RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
      char[] chars = getText().toCharArray();

      FontMetrics fm = this.getFontMetrics(getFont());

      int ha = getHorizontalAlignment();
      int va = getVerticalAlignment();
      int h = fm.getAscent();
      if (va == SwingConstants.CENTER) h += (getHeight()-fm.getHeight())/2;
      int x = ha == SwingConstants.CENTER ? (getWidth()-totalWidth())/2 : 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);

  } // end paintComponent()
}

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: 595 / 1222
Version history: 8 change(s)
Referenced in: [show references]