How to draw a String inside a filled rectangle?

alicedimarco picture alicedimarco · Nov 26, 2011 · Viewed 9.8k times · Source

I just want to display my String inside a rectangle filled with black. Thanks for the help!

Answer

GETah picture GETah · Nov 26, 2011

Here you are:

public class MyPanel extends JPanel{
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawRect(10/*x*/, 10/*y*/, 80/*width*/, 30/*hight*/);
    g.drawString("TextToDraw", 25/*x*/, 25/*y*/);
  }
}