Reputation: 1623
I have the following code:
public class Canvas extends JPanel{
JLabel label = new JLabel();
public void init()
{
label.setSize(100, 100);
label.setLocation(10, 10);
label.setText("lalallaalal");
this.add(label);
}
@Override
public void paint(Graphics g) {
super.paint(g);
paintRoad(g);
paintBorders(g);
paintEnemies(g, enemies);
paintPlayer(g);
}
I want the label to be redrawn every time the JPanel
is repainted, but when I put this.add(label)
at the end of paint
method it doesn't show the label.
Any idea why?
Upvotes: 1
Views: 805
Reputation: 567
Instead of using the JLabel, try using the drawString(String str, int X, int y) method in the paint method.
public void paint(Graphics g){
g.drawString(label.getText(), 110, 110);
}
Upvotes: 3
Reputation: 46841
paint()
invokes paintComponent()
. It's better to override paintComponent
instead of paint
.
protected void paintComponent(Graphics g)
A Closer Look at the Paint Mechanism
Upvotes: 3