this code should do rotation of JFrame 180°. It somehow does if some conditions are met. But ... Lets look at it:
//anchorx,anchory are center points of screen (JFrame)
JPanel panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//super.paintChildren(g); // this shouldn't be here
//but the behaviour is same if i deleted this line
Graphics2D g2d = (Graphics2D) g;
g2d.rotate(Math.PI, anchorx, anchory);
}
/*
/* //I also commented this whole overiding paintChildren and
//the behaviour is still same like i described.
@Override
public void paintChildren(Graphics g) {
super.paintChildren(g);
Graphics2D g2d2 = (Graphics2D) g;
g2d2.rotate(Math.PI, anchorx, anchory);
}*/
};
JFrame frame = new JFrame();
frame.setContentPane(panel);
DEFINITION (to prevent missunderstanding): rotation, rotated JFrame = imagine that JFrame is picture or photo and you cant touch it and literally rotate it around its center. So components of it is also rotated. So there is man on the picture and you rotate picture 180°. His head will be down, his right arm will be on your left side, his left arm will be on your right side. So in this metaphore picture is JFrame and components on JFrame is the man. So rotation of JFrame mean rotation of whole as one unit, with all of its components.
Code above just draw unrotated JFrame.
So the code above should rotate JFrame but nothing happens.
There comes some options which I tried but didn't change anything. (Ofcourse after setting the content pane).
1) repaint:
frame.repaint();
2) validate and repaint:
frame.repaint();
3) lets try it on contentPane too:
frame.getContentPane().repaint();
4) -||- :
frame.getContentPane().validate();
frame.getContentPane().repaint();
Attempts 1 - 4 didn't force Java to really "repaint" the JFrame and rotate it.
5) So I just tried this:
JLabel hwLabel = new JLabel("Hello world.");
hwLabel.setVisible(true);
frame.getContentPane().add(hwLabel);
If the point 5 is added after the row frame.setContentPane(panel) it somehow finally FORCE java to do its job and repaint the JFrame, so its rotated as I want.
Problem is when content of some component on contentPane is changed it again drawed unrotated over the rotated version. So if I use my metaphore from definition. "The changed left hand of 180° rotated man should be redrawn on right side but its drawn on left side like non rotated". Simply. The updated component are drawn on the place where they would be if I would do no rotation, ofcourse not rotated.
QUESTION:
Can anyone explain me how does (re)painting lifecycle works? Or maybe just what I am missing that repaint (or basically my attempts 1-4) doesn't force java to repaint it but the adding component on JFrame yes (attempt 5)?
The fact with wrong updating the component is big pain in my a** but its not main point of this question. It is here just for finish the description of actual behaviour.
I have read documentation about painting and many topics and questions about it but I haven't find anything helpful.