I’m trying to render a JME within a Swing Canvas. All seems well, except JME only renders within two-thirds of the canvas on both axis.
I initially thought that the Canvas was not taking up the full size of the window, but upon changing the canvas background color to magenta, it would seem the canvas is occupying the whole window.
The ‘padding’ around JME always seems to be one-third of the size (on both axis), despite resizing the window etc.
If anyone could please let me know what I’m missing, or steer me in the right direction I’d greatly appreciate it.
Here’s the code:
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.system.AppSettings;
import com.jme3.system.JmeCanvasContext;
import java.awt.*;
import javax.swing.*;
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);
JPanel panel = new JPanel(new BorderLayout());
panel.add(canvas, BorderLayout.CENTER);
JFrame window = new JFrame("Swing Application");
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);
}
}
}