0

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: Black bar shows gap between Canvas & LWJGL Display
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)?

Sparko
  • 735
  • 6
  • 15
  • No expert here, just some guesses: a) You set some LWJGL property about undecorated windows, but your `MainFrame` is not undecorated itself. b) You don't replace the frame's content pane, but add to it. By default this is a `JPanel` with `BoxLayout` if I remember correctly. __So you add the heavyweight component to a lightweight one.__—possibly related: http://stackoverflow.com/questions/13459670/lwjgl-display-and-java-swing . c) If you call `pack()`, this undoes any previous call to `setSize`. I would also make the frame visible in the very end. – 0__ Oct 04 '15 at 18:45
  • 1
    Correction: default `JPanel` has `BorderLayout`. Anyways, this seems the way to do it: http://env3d.org/beta/node/44 -- perhaps the offset you are seeing is from non-native menu bar? – 0__ Oct 04 '15 at 18:52
  • Thankyou - removing `peer.pack()` and adding `peer.add(canvas, BorderLayout.CENTER)` in the `MainFrame` fixed the issue. – Sparko Oct 04 '15 at 19:00

1 Answers1

0

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_=.

0__
  • 66,707
  • 21
  • 171
  • 266