0

I want to put text ( must be formatted with HTML, so I can't use drawString in PaintComponent) inside Circle. Problem is that "paintComponent" is called after drawing label, so it covers my text.

How to draw oval at the beginning and then draw my String?

class Circle extends JLabel 
{

         public Circle(String string) {         super(string);  }
     @Override
    public void paintComponent( Graphics g )
    {
        super.paintComponent(g);
        g.setColor(Color.yellow);
        g.fillOval(0,0, 70, 70);
        g.setColor(Color.blue);
       g.drawOval(0,0, 70, 70); 
    }
 }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2262230
  • 199
  • 1
  • 3
  • 17
  • You might consider putting the label inside a custom border. See [`TextBubbleBorder`](http://stackoverflow.com/a/16909994/418556) for ideas. – Andrew Thompson Jun 07 '13 at 18:45

3 Answers3

1

Probably the quickest solution is to change your paintComponent to

public void paintComponent( Graphics g )
{
  g.setColor(Color.yellow);
  g.fillOval(0,0, 70, 70);
  g.setColor(Color.blue);
  g.drawOval(0,0, 70, 70); 
  super.paintComponent(g);
}

I'd however also consider composition rather than inheritance in this case. Perhaps define another component class composed of the label and a panel with the circle.

david a.
  • 5,283
  • 22
  • 24
1

Consider putting the component inside a custom border. See TextBubbleBorder for ideas.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

I would try using setComponentZOrder() to set the labels order to be higher up than the circle.

gavlaaaaaaaa
  • 612
  • 2
  • 7
  • 11