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 960179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:08:30+00:00 2026-05-16T01:08:30+00:00

I am started to deal with OpenGL. My application is written in Java using

  • 0

I am started to deal with OpenGL. My application is written in Java using SWT as windowing system.

Using http://lwjgl.org/ and SWT, I am able to use OpenGL in my SWT canvas. I wrote the following simple OpenGL code in my canvas paint listener:

// clear to background color
GL11.glClearColor(.3f, .5f, .8f, 1.0f);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

// draw rectangle
GL11.glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glBegin(GL11.GL_POLYGON);
GL11.glVertex3f(0.1f, 0.1f, 0.0f);
GL11.glVertex3f(0.1f, 0.9f, 0.0f);
GL11.glVertex3f(0.9f, 0.9f, 0.0f);
GL11.glVertex3f(0.9f, 0.1f, 0.0f);
GL11.glEnd();
GL11.glFlush();

I want know to add a resize listener on my canvas in order to always have my rectangle in the center of the window. How should I do that?

  • 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-16T01:08:31+00:00Added an answer on May 16, 2026 at 1:08 am

    You need to manually set your viewport size by calling glViewport() every time canvas size changes. After that your screen will have dimensions specified by glOrtho().

    Also, your matrices are a mess. Projection matrix is used for projection only and modelview for other transformations (rotation, scaling, moving, etc.).

    // Viewport (needs to be done on canvas resize only)
    GL11.glViewport(0.0, 0.0,                   // Set viewport size
                    canvas.getBounds().width,
                    canvas.getBounds().height);
    
    // Projection (only needs to be set once in most cases)
    GL11.glMatrixMode(GL11.GL_PROJECTION);        // Select projection matrix
    GL11.glLoadIdentity();                        // Clear it
    GL11.glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);  // Set your projection
    
    // model/view transforms
    GL11.glMatrixMode(GL11.GL_MODELVIEW);  // Select modelview matrix
    GL11.glLoadIdentity();                 // Clear it
    
    // Draw (shortcut)
    GL11.glRectf(0.1f, 0.1f, 0.9f, 0.9f);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our customer is using our software (Java Swing application started using Webstart) besides other
I've started a simple facebook iframe application. I created a handler to deal with
So here's the deal: I read this article and started trying to use Rhino.
Started using Visual Studio 2012 RC since yesterday, We have one WCF solution. Whenever
Started working on a new application this week that is running the latest rails
I started getting the following error whenever i use SVN in my server: svn:
I started to work in translating a Java program which has tens of dialog
I started to think about tracking changes in complex object graph in disconnected application.
I've just started using Google's search API to find addresses and the distances between
Here's the deal: I just started a new job, and source control here is

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.