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).

!7

import java.awt.font.*;

p {
  BufferedImage img = newBufferedImage(400, 400, Color.blue);
  Graphics2D g = imageGraphics(img);
  g.setFont(sansSerifBold(20));
  paintTextWithOutline(g);
  g.dispose();
  showImage(img);
}

static void paintTextWithOutline(Graphics g) {
  String text = "BOMBS [BAD]";

  Color outlineColor = Color.black;
  Color fillColor = Color.yellow;
  BasicStroke outlineStroke = new BasicStroke(2.0f);

  if (g instanceof Graphics2D) {
      Graphics2D g2 = (Graphics2D) g;
      
      g.translate(100, 100);

      // remember original settings
      Color originalColor = g2.getColor();
      Stroke originalStroke = g2.getStroke();
      RenderingHints originalHints = g2.getRenderingHints();

      // create a glyph vector from your text
      GlyphVector glyphVector = g.getFont().createGlyphVector(g2.getFontRenderContext(), text);
      // get the shape object
      Shape textShape = glyphVector.getOutline();

      // activate anti aliasing for text rendering (if you want it to look nice)
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
              RenderingHints.VALUE_ANTIALIAS_ON);
      g2.setRenderingHint(RenderingHints.KEY_RENDERING,
              RenderingHints.VALUE_RENDER_QUALITY);

      g2.setColor(outlineColor);
      g2.setStroke(outlineStroke);
      g2.draw(textShape); // draw outline

      g2.setColor(fillColor);
      g2.fill(textShape); // fill the shape

      // reset to original settings after painting
      g2.setColor(originalColor);
      g2.setStroke(originalStroke);
      g2.setRenderingHints(originalHints);
  }
}

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: 515 / 584
Version history: 7 change(s)
Referenced in: