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

20
LINES

< > BotCompany Repo | #1007307 // drawTextAlongLine

JavaX fragment (include)

import java.awt.geom.*;

static void drawTextAlongLine(Graphics2D g, S text, int x1, int y1, int x2, int y2, int shift) {
  if (y2 == y1 && x2 == x1) ++x2;
  
  new AffineTransform tx;
  double angle = Math.atan2(y2-y1, x2-x1);
  bool flip = abs(angle) > pi()/2;
  if (flip) angle -= pi();
  tx.translate((x1+x2)/2.0, (y1+y2)/2.0);
  tx.rotate(angle);

  AffineTransform old = g.getTransform();
  g.setTransform(tx);
  FontMetrics fm = g.getFontMetrics();
  // int y = shift+fm.getLeading()+fm.getMaxAscent(); // below
  int y = -shift-fm.getMaxDescent(); // above
  g.drawString(text, -fm.stringWidth(text)/2.0f, y);
  g.setTransform(old);
}

Author comment

Began life as a copy of #1007301

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1007307
Snippet name: drawTextAlongLine
Eternal ID of this version: #1007307/10
Text MD5: ebf109615ea09580450d72456355fcb1
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-03-15 02:33:38
Source code size: 661 bytes / 20 lines
Pitched / IR pitched: No / No
Views / Downloads: 434 / 411
Version history: 9 change(s)
Referenced in: [show references]