Java repaint updates only part of my Canvas

190 views Asked by At

I have this program, where I have a canvas in a window with 2 circles in it (you can move one of the circles by clicking on the canvas).

It works with no problems on windows and iOS, but it has this problem on ubuntu:

When I change the selected circle with the combobox in the top left corner of the window, the circles disappears and are only visible in the top left corner of the canvas in the area which was covered by the combobox items right before I swiched the selected circle.

Also when I drag the application window so it leaves the screen or put another window over my window, the circles start to be visible in the part of the canvas which was hidden.

Can you please explain, why is this happening and what am I doing wrong?

The code of my program is here: http://pastie.org/private/rm0gw2awx6d7iztyabzs7q

2

There are 2 answers

0
MadProgrammer On

You're breaking the paint chain, you should be calling super.paint, but in the case of the JPanel, you should be overriding paintComponent (and calling super.paintComponent before you do any custom painting)

Take a look at Painting in AWT and Swing and Performing Custom Painting for more details

0
flaz14 On

It is hard to give any thoughts (the piece of code is not short). On my system (Cinnamon 2.4.8) everything is painted pretty well. Anyway, adding super.paint() doesn't require much effort and will not harm anything.

The following is interesting: you create your Frame directly in main() method. But nowadays Swing/AWT main frame should be created in event queue:

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            new BlochovyKruhy2();
        }
    });
}

instead of

public static void main(String[] args) {
    new BlochovyKruhy2();
}