[SOLVED] JME only renders within two-thirds of the Swing Canvas

Fixed code for Windows below… only a workaround but not half bad.

Custom JPanel does the trick, scaling the canvas up by the Windows scaling factor.

public class CanvasIssue {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            AppSettings settings = new AppSettings(true);
            settings.setWidth(500);
            settings.setHeight(500);

            SimpleGame app = new SimpleGame();
            app.setShowSettings(false);
            app.setSettings(settings);
            app.createCanvas();

            JmeCanvasContext ctx = (JmeCanvasContext) app.getContext();
            ctx.setSystemListener(app);
            Canvas canvas = ctx.getCanvas();
            ctx.getCanvas().setPreferredSize(new Dimension(settings.getWidth(), settings.getHeight()));
            canvas.setBackground(Color.MAGENTA);

            JFrame window = new JFrame("Swing Application");


            // FIX: Custom panel to correct for Windows OS display scaling.
                // If you have scaling set to something different from 100% in Windows display settings, the canvas will render at the incorrect size.
                // This fixes this issue, by scaling the canvas bounds by the Windows scaling amount.
            JPanel panel = new JPanel(new BorderLayout()) {
                @Override
                public void doLayout() {
                    if (JmeSystem.getPlatform().getOs() == Platform.Os.Windows) {
                        GraphicsConfiguration graphicsConfiguration = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
                        AffineTransform displayTransform = graphicsConfiguration.getDefaultTransform();
                        double scaleX = displayTransform.getScaleX(); // Windows OS Display X scaling.
                        double scaleY = displayTransform.getScaleY(); // Windows OS Display Y scaling.
                        canvas.setBounds(0, 0, (int)(getWidth() * scaleX), (int)(getHeight() * scaleY));
                    }

                }
            };
            panel.add(canvas, BorderLayout.CENTER);


            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.getContentPane().setLayout(new BorderLayout());
            window.getContentPane().add(panel, BorderLayout.CENTER);
            window.pack();
            window.setVisible(true);

            app.startCanvas();
        });
    }

    private static class SimpleGame extends SimpleApplication {

        private Geometry boxGeom;
        @Override
        public void simpleInitApp() {
            flyCam.setDragToRotate(true);
            Box b = new Box(1, 1, 1);
            boxGeom = new Geometry("Box", b);
            Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
            mat.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
            boxGeom.setMaterial(mat);
            rootNode.attachChild(boxGeom);
            viewPort.setBackgroundColor(ColorRGBA.fromRGBA255(135, 206, 235, 1));
        }
        @Override
        public void simpleUpdate(float tpf) {
            boxGeom.rotate(tpf, tpf, tpf);
        }
    }

}
4 Likes