Libraryless. Click here for Pure Java version (10119L/56K).
1 | import java.awt.font.GlyphVector; |
2 | |
3 | static void drawTextWithOutline(Graphics2D etc g2, S text, float x, float y, Color fillColor, Color outlineColor) { |
4 | BasicStroke outlineStroke = new BasicStroke(2.0f); |
5 | |
6 | g2.translate(x, y); |
7 | |
8 | // remember original settings |
9 | Stroke originalStroke = g2.getStroke(); |
10 | RenderingHints originalHints = g2.getRenderingHints(); |
11 | |
12 | // create a glyph vector from your text |
13 | GlyphVector glyphVector = g2.getFont().createGlyphVector(g2.getFontRenderContext(), text); |
14 | // get the shape object |
15 | Shape textShape = glyphVector.getOutline(); |
16 | |
17 | g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); |
18 | |
19 | g2.setColor(outlineColor); |
20 | g2.setStroke(outlineStroke); |
21 | g2.draw(textShape); // draw outline |
22 | |
23 | g2.setColor(fillColor); |
24 | g2.fill(textShape); // fill the shape |
25 | |
26 | // reset to original settings after painting |
27 | g2.setStroke(originalStroke); |
28 | g2.setRenderingHints(originalHints); |
29 | |
30 | g2.translate(-x, -y); |
31 | } |
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: | #1007349 |
Snippet name: | drawTextWithOutline |
Eternal ID of this version: | #1007349/8 |
Text MD5: | c194637e0f9ab02247cfeb2f8294d4a6 |
Transpilation MD5: | 50fb93f0e356773b8ca637609a7e0406 |
Author: | stefan |
Category: | javax / gui |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2023-02-12 20:45:40 |
Source code size: | 1001 bytes / 31 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 540 / 610 |
Version history: | 7 change(s) |
Referenced in: | [show references] |