0

The code below shows 50 circles drawn inside SWT with Swing with SWT_AWT bridge.

Each calling of paint causes debug print.

It is evident, that paint is called three times on run. If runned inside normal Swing, it is called only once.

Why is it called three times if inside SWT?

package tests;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Try_SWT_Swing {

    public static void main(String[] args) {

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());

        Composite composite = new Composite(shell, SWT.EMBEDDED | SWT.NO_BACKGROUND);
        composite.setLayout(new FillLayout());

        JPanel panel = new JPanel() {

            @Override
            protected void paintComponent(Graphics g) {

                System.out.println("paint");

                Graphics2D g2 = (Graphics2D) g;

                for(int i=1; i<50; ++i) {
                    g2.drawOval(i*10, i*10, 100, 100);
                }

            }
        };

        Frame frame = SWT_AWT.new_Frame(composite);
        frame.setLayout(new BorderLayout());
        frame.add(panel);

        shell.setSize(640, 480);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();

    }

}
Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
  • You have no control over how many times SWT calls the JPanel paintComponent method. Just make sure that you are doing nothing but drawing in the paintComponent method. You need to call super.paintComponent() in the first line of your paintComponent method. – Gilbert Le Blanc Mar 23 '14 at 08:25
  • So, I can't control the number of redraws. But WHY there are THREE of them instead of ONE? I would post bug report into SWT project if this was not correct. – Suzan Cioc Mar 23 '14 at 08:35
  • You can throw an exception each time the paintComponent method is called, and see what's calling it. I suspect once for the initial positioning, once again for a resize, and once again just for luck. – Gilbert Le Blanc Mar 23 '14 at 09:14

0 Answers0