0

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.

user1097772
  • 3,499
  • 15
  • 59
  • 95
  • 1
    I'm no expert at this, but I worry about your calling `paintChildren` directly within the paintComponent method. You do know of course that `paintChildren` is called within the JPanel's `paint` method, right? Wouldn't you instead want to override `paint`, rotate the graphics, and then call `super.paint(...)` after the rotation? Of course as always in such situations, it would be best if you created and posted your [mcve], code that we could **directly test and modify**. – Hovercraft Full Of Eels Dec 16 '15 at 14:23
  • Remove `paintChildren()` from `paintComponent()`. I can guarantee any weird attempt at rewriting the intended logic for the paint stack will result in inconsistencies. Might not fix your problem, but shouldn't be there at the very least. Also, what @HovercraftFullOfEels said. `paintComponent()` overrides get messy, fast; more code is always better than less. – Gorbles Dec 16 '15 at 14:41
  • @HovercraftFullOfEels 2 things: first the paintChildren in paintComponent was just another atempt to make it work, but I forgot it in my code, it shouldn't be here so I deleted it, second: I also deleted whole overriding of paintChildren and the behaviour is still same I described. I'm struggling on this problem for some time so I'm sorry for this mistake here but it didn't changed anything about the behaviour. So i removed all changes and calls of paintChildren and the behaviour is still stame like in the question. – user1097772 Dec 16 '15 at 15:00
  • 1
    Poster -- thanks for the update. Still, please consider creating and posting your [mcve] (please read the link) so we can help you better. This is not a full code dump, and is certainly not a link to an outside code repository. Rather it's a new program that you create that simplifies your problem to its bare essence, so that the code is very small, is compilable and runnable by us, and demonstrates your problem for us. – Hovercraft Full Of Eels Dec 16 '15 at 15:02
  • @Gorbles thanks for the point and sorry it was juts another attempt to make it work, because i found it somewhere. I test it without any overriding and calling it and the behaviour is still like I described. – user1097772 Dec 16 '15 at 15:03
  • @HovercraftFullOfEels well it's part of big project which has hundreads of classes thousands lines of code, its pretty impossible to make that example :( – user1097772 Dec 16 '15 at 15:05
  • 1
    *"its pretty impossible to make that example"* If so, it's outside the scope of what people on SO can fix. Good luck with it. Voting to close. – Andrew Thompson Dec 16 '15 at 15:12
  • @AndrewThompson The question was actually not about solving problem but about how the repaint mechanism works. So I can possibly find out Why can adding component cause the repaint but calling repaint not. I didn't find this in documentation. And I thing someone who understands swing more than me could tell me how it really works. – user1097772 Dec 16 '15 at 15:18
  • 1
    It's up to you. As for me and likely as for @AndrewThompson, we'll wait for your [mcve]. As for "how the painting mechanism works" -- read the docs: [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html). We're not going to want to re-write this for you since it has already been well written, better than we're going to do. – Hovercraft Full Of Eels Dec 16 '15 at 15:23
  • 1
    Add me to the list of people who have been waiting 5 days for a runnble example. How is this different that your last question: http://stackoverflow.com/questions/34182239/post-overriding-the-paint-method-of-the-components-in-java, where you have also been asked to post a runnable example that demonstrates the concept. It does not take `hundreds of classes` to demonstrate a concept. If you can't properly explain your concept how do you expect us to help??? – camickr Dec 16 '15 at 15:28

0 Answers0