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

54
LINES

< > BotCompany Repo | #1007348 // Text With Outline Test

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (4204L/28K/106K).

1  
!7
2  
3  
import java.awt.font.*;
4  
5  
p {
6  
  BufferedImage img = newBufferedImage(400, 400, Color.blue);
7  
  Graphics2D g = imageGraphics(img);
8  
  g.setFont(sansSerifBold(20));
9  
  paintTextWithOutline(g);
10  
  g.dispose();
11  
  showImage(img);
12  
}
13  
14  
static void paintTextWithOutline(Graphics g) {
15  
  String text = "BOMBS [BAD]";
16  
17  
  Color outlineColor = Color.black;
18  
  Color fillColor = Color.yellow;
19  
  BasicStroke outlineStroke = new BasicStroke(2.0f);
20  
21  
  if (g instanceof Graphics2D) {
22  
      Graphics2D g2 = (Graphics2D) g;
23  
      
24  
      g.translate(100, 100);
25  
26  
      // remember original settings
27  
      Color originalColor = g2.getColor();
28  
      Stroke originalStroke = g2.getStroke();
29  
      RenderingHints originalHints = g2.getRenderingHints();
30  
31  
      // create a glyph vector from your text
32  
      GlyphVector glyphVector = g.getFont().createGlyphVector(g2.getFontRenderContext(), text);
33  
      // get the shape object
34  
      Shape textShape = glyphVector.getOutline();
35  
36  
      // activate anti aliasing for text rendering (if you want it to look nice)
37  
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
38  
              RenderingHints.VALUE_ANTIALIAS_ON);
39  
      g2.setRenderingHint(RenderingHints.KEY_RENDERING,
40  
              RenderingHints.VALUE_RENDER_QUALITY);
41  
42  
      g2.setColor(outlineColor);
43  
      g2.setStroke(outlineStroke);
44  
      g2.draw(textShape); // draw outline
45  
46  
      g2.setColor(fillColor);
47  
      g2.fill(textShape); // fill the shape
48  
49  
      // reset to original settings after painting
50  
      g2.setColor(originalColor);
51  
      g2.setStroke(originalStroke);
52  
      g2.setRenderingHints(originalHints);
53  
  }
54  
}

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: #1007348
Snippet name: Text With Outline Test
Eternal ID of this version: #1007348/8
Text MD5: f01bab79952799fc3b088fc7878f81b7
Transpilation MD5: f812a979fdf4d59433e63e13fdaa7d23
Author: stefan
Category: javax / gui
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-03-16 01:27:35
Source code size: 1655 bytes / 54 lines
Pitched / IR pitched: No / No
Views / Downloads: 516 / 585
Version history: 7 change(s)
Referenced in: [show references]