Login  Register

NewtCanvasAWT in JSplitPane on macOS

Posted by bogdanni on Apr 22, 2019; 9:04am
URL: https://forum.jogamp.org/NewtCanvasAWT-in-JSplitPane-on-macOS-tp4039717.html

Hello,

The following program doesn't work quite right on macOS. Resizing with the divider doesn't resize the top canvas component. It works when the canvas is the bottom component.

I'm aware there were issues in the past, but not all are fixed. It works as expected in Linux.

package abc;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;

import com.jogamp.newt.awt.NewtCanvasAWT;
import com.jogamp.newt.opengl.GLWindow;
import com.jogamp.opengl.GL;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;

public class Main {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            MainWindow w = new MainWindow();
            w.setPreferredSize(new Dimension(640, 480));
            w.pack();
            w.setVisible(true);
        });
    }

    private static class MainWindow extends JFrame {
        private static final long serialVersionUID = -1;

        MainWindow() {
            GLProfile profile = GLProfile.getDefault();
            GLCapabilities caps = new GLCapabilities(profile);

            GLWindow glWindow = GLWindow.create(caps);
            glWindow.addGLEventListener(new MainGLEventListener());

            NewtCanvasAWT canvas = new NewtCanvasAWT(glWindow);

            JPanel canvasPanel = new JPanel(new BorderLayout());
            canvasPanel.setMinimumSize(new Dimension(1, 1));
            canvasPanel.add(canvas);

            JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
            pane.setBottomComponent(new JButton("Button"));
            pane.setTopComponent(canvasPanel);

            getContentPane().add(pane);
        }

        private class MainGLEventListener implements GLEventListener {
            private double time;

            public void init(GLAutoDrawable drawable) {
            }

            public void dispose(GLAutoDrawable drawable) {
            }

            public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
                // System.out.println(">>> " + w + " " + h);
            }

            public void display(GLAutoDrawable drawable) {
                GL gl = drawable.getGL();
                float r = (float) Math.abs(Math.sin(time));
                float g = (float) Math.abs(Math.sin(time + 0.3));
                float b = (float) Math.abs(Math.sin(time + 0.6));
                gl.glClearColor(r, g, b, 1);
                gl.glClear(GL.GL_COLOR_BUFFER_BIT);
                time += 0.01;
            }
        }
    }

}

Best regards,
Bogdan