How to draw a diagram around text in AWT?

How can I draw a diagram around any text in AWT that looks something like this image?

Outline

+8
java awt
source share
5 answers

two examples

the output of this paint will be BufferedImage , for the AWT components the paint() method is used, for Swing JComponents there is paintComponet()

Also, from the code associated in the comment:

yvnRi.png

+6
source share

Not sure how you are drawing text now, but one way you could do this is to use BufferedImage as an overlay to what you are drawing.

  • Create a BufferedImage using the dimensions of the line and the font you want to draw (look at the FontMetrics class for this).
  • Fill the BufferedImage with transparency.
  • Draw your line on the BufferedImage with whatever color you want.
  • Loop over each pixel in BufferedImage and see how far it is from the pixel of your text. If it is a certain distance, draw that pixel black and possibly more transparent if it is farther from the color of your text. Of course, if the pixel already has the same color as the text color, then ignore it.
  • Draw a BufferedImage on everything you draw.

EDIT

There may be libraries that already do this, but if I had to code it from scratch, I would try to do it.

+2
source share

Some of the most stupid workarounds: -The same words twice, but one of them is black and the other is white, white on black, you can get something like that. -find the font looks above the example and use it.

0
source share

Here is a hacker example. It is not as difficult as others, but easier to understand, and it behaves like JLabel.

 public class OutlineLabel extends JLabel { private Color outlineColor = Color.WHITE; private boolean isPaintingOutline = false; private boolean forceTransparent = false; public OutlineLabel() { super(); } public OutlineLabel(String text) { super(text); } public OutlineLabel(String text, int horizontalAlignment) { super(text, horizontalAlignment); } public Color getOutlineColor() { return outlineColor; } public void setOutlineColor(Color outlineColor) { this.outlineColor = outlineColor; this.invalidate(); } @Override public Color getForeground() { if ( isPaintingOutline ) { return outlineColor; } else { return super.getForeground(); } } @Override public boolean isOpaque() { if ( forceTransparent ) { return false; } else { return super.isOpaque(); } } @Override public void paint(Graphics g) { String text = getText(); if ( text == null || text.length() == 0 ) { super.paint(g); return; } // 1 2 3 // 8 9 4 // 7 6 5 if ( isOpaque() ) super.paint(g); forceTransparent = true; isPaintingOutline = true; g.translate(-1, -1); super.paint(g); // 1 g.translate( 1, 0); super.paint(g); // 2 g.translate( 1, 0); super.paint(g); // 3 g.translate( 0, 1); super.paint(g); // 4 g.translate( 0, 1); super.paint(g); // 5 g.translate(-1, 0); super.paint(g); // 6 g.translate(-1, 0); super.paint(g); // 7 g.translate( 0, -1); super.paint(g); // 8 g.translate( 1, 0); // 9 isPaintingOutline = false; super.paint(g); forceTransparent = false; } public static void main(String[] args) { JFrame w = new JFrame(); w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); OutlineLabel label = new OutlineLabel("Test", OutlineLabel.CENTER); label.setOpaque(true); w.setContentPane(new JPanel(new BorderLayout())); w.add(label, BorderLayout.CENTER); w.pack(); w.setVisible(true); } } 
0
source share

Try the following:

 public void paintTextWithOutline(Graphics g) { String text = "some text"; Color outlineColor = Color.white; Color fillColor = Color.black; BasicStroke outlineStroke = new BasicStroke(2.0f); if (g instanceof Graphics2D) { Graphics2D g2 = (Graphics2D) g; // remember original settings Color originalColor = g2.getColor(); Stroke originalStroke = g2.getStroke(); RenderingHints originalHints = g2.getRenderingHints(); // create a glyph vector from your text GlyphVector glyphVector = 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); } } 
0
source share

All Articles