How to draw a String with a background on a chart?

I draw texts with Graphics.drawString, but I want to draw lines with a rectangular background.

+7
source share
2 answers

Use Graphics.fillRect or Graphics2D.fill before drawing text.

Here is an example:

 import java.awt.*; import java.awt.geom.Rectangle2D; import javax.swing.*; public class FrameTestBase extends JFrame { public static void main(String args[]) { FrameTestBase t = new FrameTestBase(); t.add(new JComponent() { public void paintComponent(Graphics g) { String str = "hello world!"; Color textColor = Color.WHITE; Color bgColor = Color.BLACK; int x = 80; int y = 50; FontMetrics fm = g.getFontMetrics(); Rectangle2D rect = fm.getStringBounds(str, g); g.setColor(bgColor); g.fillRect(x, y - fm.getAscent(), (int) rect.getWidth(), (int) rect.getHeight()); g.setColor(textColor); g.drawString(str, x, y); } }); t.setDefaultCloseOperation(EXIT_ON_CLOSE); t.setSize(400, 200); t.setVisible(true); } } 

enter image description here

+21
source

Sentence:

  • Use JLabel
  • Set the opaque property to true through setOpaque(true);
  • Set the foreground color using setForeground(myForegroundColor);
  • Then set the background color using setBackground(myBackgroundColor);
+4
source

All Articles