Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6851069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:12:09+00:00 2026-05-27T01:12:09+00:00

For some reason the size if the window always stays the same size even

  • 0

For some reason the size if the window always stays the same size even when i change the width and the height. I think the problem is something to the viewport as the viewable area in the window changes correctly but there’s always a black area in the part of the window that is not used. I’ve probably expalained this really badly but any help would be much appreciated.

I’ve put a screenshot of the window below..

public class DisplayExample
{

    private int     width           = 800;
    private int     height          = 600;
    private int     colourDepth     = 32;
    private String  windowTitle     = "My First Window";
    public boolean  closeRequested  = false;
    private boolean fullscreen      = false;

    private float rtri = 0.0f;
    private float rquad = 0.0f;

    public void start()
    {

        createWindow(windowTitle, width, height, colourDepth, fullscreen);
        initGL();

        while (!closeRequested)
        {
            pollInput();
            updateLogic();
            renderGL();

            Display.update(); // flushes OpenGL pipeline and swaps back and
                                // front buffers. perhaps waits for v-sync.
        }

        cleanUp();
    }

    /**
     * Initialise OpenGL
     */
    private void initGL()
    {
        GL11.glViewport(0, 0, width, height);
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GLU.gluPerspective(45.0f, ((float) width / (float) height), 0.1f,
                100.0f);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glLoadIdentity();

        GL11.glShadeModel(GL11.GL_SMOOTH); // Smooth shading
        GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black background
        GL11.glClearDepth(1.0f);
        GL11.glEnable(GL11.GL_DEPTH_TEST);
        GL11.glDepthFunc(GL11.GL_LEQUAL);
        GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
    }

    /**
     * Update Logic
     */
    private void updateLogic()
    {

    }

    /**
     * Render OpenGL
     */
    private void renderGL()
    {
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Clear
                                                                            // The
                                                                            // Screen
                                                                            // And
                                                                            // The
                                                                            // Depth
                                                                            // Buffer
        GL11.glLoadIdentity(); // Reset The View

        GL11.glTranslatef(-1.5f, 0.0f, -6.0f); // Move Left 1.5 Units And Into
                                                // The Screen 6.0
        GL11.glRotatef(rtri, 0.0f, 1.0f, 0.0f);
        GL11.glBegin(GL11.GL_TRIANGLES); // Drawing Using Triangles

        GL11.glColor3f(1.0f, 0.0f, 0.0f);
        GL11.glVertex3f(0.0f, 1.0f, 0.0f); // Top
        GL11.glColor3f(0.0f, 1.0f, 0.0f);
        GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left
        GL11.glColor3f(0.0f, 0.0f, 1.0f);
        GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Right

        GL11.glColor3f(1.0f, 0.0f, 0.0f);
        GL11.glVertex3f(0.0f, 1.0f, 0.0f); // Top
        GL11.glColor3f(0.0f, 0.0f, 1.0f);
        GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Left
        GL11.glColor3f(0.0f, 1.0f, 0.0f);
        GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right

        GL11.glColor3f(1.0f, 0.0f, 0.0f);
        GL11.glVertex3f(0.0f, 1.0f, 0.0f); // Top
        GL11.glColor3f(0.0f, 0.0f, 1.0f);
        GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left
        GL11.glColor3f(0.0f, 1.0f, 0.0f);
        GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right

        GL11.glColor3f(1.0f, 0.0f, 0.0f);
        GL11.glVertex3f(0.0f, 1.0f, 0.0f); // Top
        GL11.glColor3f(0.0f, 1.0f, 0.0f);
        GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Left
        GL11.glColor3f(0.0f, 0.0f, 1.0f);
        GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right




        GL11.glEnd(); // Finished Drawing The Triangle


        GL11.glLoadIdentity(); // Reset The View

        GL11.glTranslatef(1.5f, 0.0f, -2.5f); // Move Right 3 Units

        GL11.glRotatef(rquad, 1.0f, 1.0f, 1.0f);

        GL11.glBegin(GL11.GL_QUADS); // Draw A Quad

        //TOP
        GL11.glColor3f(0.0f, 1.0f, 0.0f);
        GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left
        GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right
        GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Bottom Right
        GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left

        //FRONT
        GL11.glColor3f(1.0f, 0.5f, 0.0f);
        GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left
        GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Top Right
        GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Right
        GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left

        //LEFT
        GL11.glColor3f(1.0f, 0.0f, 0.0f);
        GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left
        GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right
        GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right
        GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left

        //RIGHT
        GL11.glColor3f(1.0f, 1.0f, 0.0f);
        GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Top Left
        GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right
        GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right
        GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Left

        //BACK
        GL11.glColor3f(0.0f, 0.0f, 1.0f);
        GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Top Left
        GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right
        GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right
        GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Left

        //BOTTOM
        GL11.glColor3f(1.0f, 0.0f, 1.0f);
        GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Top Left
        GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Top Right
        GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Right
        GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left




        GL11.glEnd(); // Done Drawing The Quad

        rtri += 0.5f;
        rquad += -0.5f;
    }

    /**
     * Poll Input
     */
    public void pollInput()
    {

        // scroll through key events
        while (Keyboard.next())
        {
            if (Keyboard.getEventKeyState())
            {
                if (Keyboard.getEventKey() == Keyboard.KEY_ESCAPE)
                    closeRequested = true;
                else if (Keyboard.getEventKey() == Keyboard.KEY_F1)
                {
                        fullscreen = !fullscreen;
                    System.out.println(fullscreen);
                    setDisplayMode(width, height, fullscreen);
                }
            }

        }

        if (Display.isCloseRequested())
        {
            closeRequested = true;
        }
    }

    private void createWindow(String title, int width, int height,
            int colourDepth, boolean fullscreeen)
    {
        try
        {
            DisplayMode[] modes = Display.getAvailableDisplayModes();

            for (int i = 0; i < modes.length; i++)
            {

                DisplayMode current = modes[i];

                if (current.isFullscreenCapable())
                {
                    Display.setDisplayMode(current);
                }
                else
                    Display.setDisplayMode(new DisplayMode(width, height));
            }
            Display.setFullscreen(fullscreeen);
            Display.setTitle(title);
            Display.setVSyncEnabled(true);
            Display.create();
        }
        catch (LWJGLException e)
        {
            Sys.alert("Error", "Initialisation failed!\n\n" + e.getMessage());
        }
    }

    private void cleanUp()
    {
        Display.destroy();
    }

    public void setDisplayMode(int width, int height, boolean fullscreen)
    {

        // return if requested DisplayMode is already set
        if ((Display.getDisplayMode().getWidth() == width)
                && (Display.getDisplayMode().getHeight() == height)
                && (Display.isFullscreen() == fullscreen))
        {
            return;
        }

        try
        {
            DisplayMode targetDisplayMode = null;

            if (fullscreen)
            {
                DisplayMode[] modes = Display.getAvailableDisplayModes();
                int freq = 0;

                for (int i = 0; i < modes.length; i++)
                {
                    DisplayMode current = modes[i];

                    if ((current.getWidth() == width)
                            && (current.getHeight() == height))
                    {
                        if ((targetDisplayMode == null)
                                || (current.getFrequency() >= freq))
                        {
                            if ((targetDisplayMode == null)
                                    || (current.getBitsPerPixel() > targetDisplayMode
                                            .getBitsPerPixel()))
                            {
                                targetDisplayMode = current;
                                freq = targetDisplayMode.getFrequency();
                            }
                        }

                        // if we've found a match for bpp and frequence against
                        // the
                        // original display mode then it's probably best to go
                        // for this one
                        // since it's most likely compatible with the monitor
                        if ((current.getBitsPerPixel() == Display
                                .getDesktopDisplayMode().getBitsPerPixel())
                                && (current.getFrequency() == Display
                                        .getDesktopDisplayMode().getFrequency()))
                        {
                            targetDisplayMode = current;
                            break;
                        }
                    }
                }
            }
            else
            {
                targetDisplayMode = new DisplayMode(width, height);
            }

            if (targetDisplayMode == null)
            {
                System.out.println("Failed to find value mode: " + width + "x"
                        + height + " fs=" + fullscreen);
                return;
            }

            Display.setDisplayMode(targetDisplayMode);
            Display.setFullscreen(fullscreen);
        }
        catch (LWJGLException e)
        {
            System.out.println("Unable to setup mode " + width + "x" + height
                    + " fullscreen=" + fullscreen + e);
        }
    }

    public static void main(String[] argv)
    {
        DisplayExample displayExample = new DisplayExample();
        displayExample.start();
    }
}

window

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:12:10+00:00Added an answer on May 27, 2026 at 1:12 am

    The setDisplayMode only creates the window at an initial size. But when it gets resized the change must be applied to OpenGL.

    I always recommend to forget about the idea of a “one time OpenGL initialization”. IMHO the best practice is to set all state that directly affects drawing operations, and viewport and projection do belong to that state, in the drawing code! The whole code you have in InitGL should be at the beginning of renderGL. width and height are variables to be set by the window resizing handler, or determined by querying window state.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, my topnav is not the full width of my browser window
For some reason if I try to get the actual size of mystruct I
For some reason I never see this done. Is there a reason why not?
For some reason when I attempt to make a request to an Ajax.net web
For some reason when I create a new namespace in Visual Studio 2008 its
For some reason, when I try to install SQL Server 2008 Express , I
For some reason, lately the *.UDL files on many of my client systems are
For some reason, Section 1 works but Section 2 does not. When run in
For some reason beyond me I can't access the mysql server on a machine.
For some reason the Windows command prompt is special in that you have to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.