import java.awt.*;
import sun.awt.SunToolkit;

public class TestBG {

    private volatile Dialog parent, dialog;
    private final Robot robot;

    private synchronized void waitForIdle(int ms) {
        SunToolkit.flushPendingEvents();
        ((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
        robot.delay(ms);
    }

    public TestBG(Dialog.ModalityType t) throws Exception {
        robot = new Robot();
        EventQueue.invokeLater(this::createGUI);
    }

    private void createGUI() {

        Dimension sz = new Dimension(150, 100);

        parent = new Dialog((Frame) null, "parent");
        parent.setSize(sz);
        parent.setLocation(50, 50);
        parent.setBackground(Color.RED);
        parent.setVisible(true);

        dialog = new Dialog(parent, "dialog");
        dialog.setSize(sz);
        dialog.setLocation(250, 50);
        dialog.setVisible(true);
    }

    public void run() throws Exception {

        try {
            waitForIdle(1000);
            System.out.println("BG color = " + dialog.getBackground());
            waitForIdle(1000);
        } finally {
            EventQueue.invokeAndWait(this::closeAll);
        }
    }


    private void closeAll() {
        if (dialog != null) {  dialog.dispose(); }
        if (parent != null) {  parent.dispose(); }
    }

    public static void main(String[] args) throws Exception {
        (new TestBG(null)).run();
    }
}
