Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The key is to deal with the <a href="http://docs.oracle.com/javase/7/docs/api/java/awt/FontMetrics.html" rel="nofollow">FontMetrics</a> API. The best you can do is looking at this <a href="http://docs.oracle.com/javase/tutorial/2d/text/measuringtext.html" rel="nofollow">reference doc</a>.</p> <p>Here is a sample code demonstrating this usage. It draws red and blue lines around an "Hello world" text according a range of chars.</p> <p>The text is inside a JLabel, but you can adapt the paint method on any component (but you will have to call <code>graphics.drawChars</code> to paint the text.)</p> <p>(the code is not very nice, but it demonstrates the usage of FontMetrics)</p> <pre><code>package com.example.swing; import javax.swing.*; import java.awt.*; public class DemoFontMetrics { public static void main(String[] args){ JFrame frame = new JFrame(); DecoratedLabel label = new DecoratedLabel("hello world !",new int[]{2,4}, new int[]{6,9}); JPanel textContainer = new JPanel(new FlowLayout()); textContainer.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); textContainer.add(label); frame.getContentPane().add(textContainer); frame.pack(); frame.setVisible(true); } private static class DecoratedLabel extends JLabel{ int startBlue; int endBlue; int startRed; int endRed; private DecoratedLabel(String text, int[] blueRange, int[] redRange) { super(text); startBlue = blueRange[0]; endBlue = blueRange[1]; startRed = redRange[0]; endRed = redRange[1]; } @Override public void paint(Graphics g) { super.paint(g); //draw text //set line with : 3 Stroke stroke = new BasicStroke(3f); ((Graphics2D)g).setStroke(stroke); FontMetrics fm = g.getFontMetrics(); int h = fm.getHeight(); //compute blue line coordonate fm.stringWidth(getText().substring(0,startBlue)); int x1 = fm.stringWidth(getText().substring(0, startBlue)); int x2 = fm.stringWidth(getText().substring(0, endBlue)); g.setColor(Color.BLUE); g.drawLine(x1,0,x2,0);// draw blue line //compute red line coordonates int x3 = fm.stringWidth(getText().substring(0,startRed)); int x4 = fm.stringWidth(getText().substring(0, endRed)); g.setColor(Color.RED); g.drawLine(x3,h-1,x4,h-1); // draw redline } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload