I'm using Scala swing to display a window which needs to contain an LWJGL Display object. The only method I've found is it use an intermediary AWT Canvas instance to bind the Display object to and wrap inside a Frame:
object Run extends SimpleSwingApplication {
System.setProperty("org.lwjgl.opengl.Window.undecorated", "true")
val initialWindowSize = new Dimension(256, 240)
lazy val top = new MainFrame {
title = s"Nescala ${nescala.BuildInfo.version}"
size = initialWindowSize
visible = true
peer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
peer.setFocusable(true)
peer.setIgnoreRepaint(true)
peer.add(canvas)
peer.pack()
}
def canvas:Canvas = new Canvas {
setPreferredSize(initialWindowSize)
setSize(initialWindowSize)
setFocusable(true)
setIgnoreRepaint(true)
requestFocus()
setVisible(true)
override def addNotify() = {
super.addNotify()
attachDisplay()
}
override def removeNotify() = {
detachDisplay()
super.removeNotify()
}
def attachDisplay(): Unit = {
try {
Display.setParent(this)
Display.create
} catch {
case e:LWJGLException => e.printStackTrace()
}
}
def detachDisplay():Unit = Display.destroy()
}
}
However when the window is initialized and the display is attached it appears positioned beneath the top of the Canvas/Frame, possibly including the height of a titlebar:
How can the Display be attached to the Canvas with the same dimensions? I was under the impression that the window dimensions were set from the parent, although I have tried to explicitly set a DisplayMode using the same values. Additionally, is it possible to use an LWJGL Display object without having a heavyweight AWT Canvas object set as the parent (but still within a Swing Ui element)?
With regard to lightweight/heavyweight mixing, I suggest adding the canvas directly to the default content pane of the frame, which is a JPanel
with BorderLayout
. Also, the pack()
call undoes the previous size_=
.