TheNewGuy
TheNewGuy

Reputation: 411

LWJGL Display.setWidth/Height?

Similar to what you can do with a Java's Canvas class, I am currently trying to write a method to set an LWJGL Display's maximum size. The only problem I have run into is that LWJGL's Display class doesn't have a setWidth or setHeight method, and I can't think of an way I could write my own. My first thought was to somehow get the instance of the JFrame that the window was using, but there's no method for this either so I feel like I am SOL. I also tried using DisplayMode and just resetting the size through that, but it closes the window and reopens it every time it resizes. I am trying to write a method that'd simply resize the window as if the user was dragging it to enlarge it. Does anybody know how I could/should go about doing this? Is it even possible to get this smooth max-size effect I am looking for, considering that the display doesn't even update until you let go after dragging/resizing it manually?

Upvotes: 0

Views: 418

Answers (1)

James Taylor
James Taylor

Reputation: 6258

If you need more control over your window, you can try using an AWT Frame as a parent to an LWJGL Canvas. This would allow your code to take advantage of default window methods, including setWidth() and setHeight() found in the Java standard libraries, prevent and control window behavior, and add listeners to run code for certain events.

First, import java.awt.* and then set up code that looks something like this:

Create a Frame and Canvas:

Frame frame = new Frame("LWJGL Game Window");
frame.setLayout(new BorderLayout());
Canvas canvas = new Canvas();

Add the Canvas to the Frame:

frame.add(canvas, BorderLayout.CENTER);

Setup LWJGL and apply it to the Canvas:

try {
    Display.setParent(canvas);

    frame.setPreferredSize(new Dimension(1024, 786));
    frame.setMinimumSize(new Dimension(800, 600));
    frame.pack();
    frame.setVisible(true);

    Display.create();

    while(!Display.isCloseRequested() && !closeRequested)
    {
        // main code or call to code goes here

        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
        Display.update();
    }

    Display.destroy();
    frame.dispose();
    System.exit(0);

} catch (LWJGLException e) {
    e.printStackTrace();
}

Not sure how much you know about Java, but from here, you could add an ActionListerner or ComponentListerner that does something if the window is resized.

Also check out the LWJGL wiki for AWT Frames. This should, at least, help to point you in the right direction.

Upvotes: 2

Related Questions