Reputation: 21329
CODE
import javax.swing.*;
import java.awt.*;
class tester {
public static void main(String args[]) {
JFrame fr = new JFrame();
JPanel p = new JPanel();
p.setBackground(Color.RED);
p.paintImmediately(20,20,500,500);
fr.add(p);
fr.setVisible(true);
fr.setSize(2000,2000);
}
}
I get a panel painted completely red. Why don't I get the line? How can I get it?
Upvotes: 3
Views: 302
Reputation: 420990
I get a panel painted completely of red color.
That's because you set the background and didn't do any further painting...
Why dont i get the line ? How can i get it?
This is not the way to do it. Why do you call paintImmediately
? Here is what the documentation says:
Paints the specified region in this component and all of its descendants that overlap the region, immediately.
It's rarely necessary to call this method. In most cases it's more efficient to call repaint, which defers the actual painting and can collapse redundant requests into a single paint call. This method is useful if one needs to update the display while the current event is being dispatched.
I suggest you read up on painting in AWT/Swing.
To get something like this
you could change your code like this:
JFrame fr = new JFrame();
JPanel p = new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(20, 20, 500, 500);
}
};
p.setBackground(Color.RED);
fr.add(p);
fr.setVisible(true);
fr.setSize(200, 200);
Upvotes: 8