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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:32:06+00:00 2026-05-23T12:32:06+00:00

i try to write a code that show a rect in a blue frame:

  • 0

i try to write a code that show a rect in a blue frame:

 import java.awt.Color;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.sun.opengl.util.Animator;
public class Jojl extends GLCanvas implements GLEventListener {

static final long serialVersionUID = 4262202874615600756L;

public static void main(String[] args) {
    Jojl convas=new Jojl();
    Animator animator=new Animator(convas);
    JFrame jframe=new JFrame("hello JOGL");
    jframe.setVisible(true);
    jframe.setSize(500,500);
    jframe.setVisible(true);
    jframe.setLayout(null);
    JPanel jp=new JPanel();
    jp.setSize(jframe.getSize());
    jframe.add(jp);
    jp.setBackground(Color.BLUE);
    jp.add(convas);
    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    animator.start();

    }

    public Jojl()
    {
    super( new GLCapabilities());

    }
    @Override
    public void display(GLAutoDrawable arg0) {
        System.out.println("hello");
        GL gl=arg0.getGL();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        gl.glRectf(-0.5f,-0.5f,0.5f,0.5f);


    }

    @Override
    public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {

    }

    @Override
    public void init(GLAutoDrawable arg0) {

    }

    @Override
    public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
            int arg4) {
        // TODO Auto-generated method stub

    }
}

but this show only a blue frame and not show rectangle.

  • 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-23T12:32:06+00:00Added an answer on May 23, 2026 at 12:32 pm

    Try this way:

    import javax.media.opengl.GL;
    import javax.media.opengl.GLAutoDrawable;
    import javax.media.opengl.GLCanvas;
    import javax.media.opengl.GLCapabilities;
    import javax.media.opengl.GLEventListener;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import com.sun.opengl.util.Animator;
    import java.awt.BorderLayout;
    
    public class Jojl extends GLCanvas implements GLEventListener {
    
        static final long serialVersionUID = 4262202874615600756L;
    
        public static void main(String[] args) {
            Jojl convas = new Jojl();
            Animator animator = new Animator(convas);
            JFrame jframe = new JFrame("hello JOGL");
            jframe.setVisible(true);
            jframe.setSize(500, 500);
            jframe.setVisible(true);
            jframe.setLayout(null);
            JPanel jp = new JPanel(new BorderLayout()); // 1.              <- HERE
            jp.setSize(jframe.getSize());
            jframe.add(jp);
            // jp.setBackground(Color.BLUE); // 2. rather do that in JOGL
            jp.add(convas);
            jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            animator.start();
    
        }
    
        public Jojl() {
            super(new GLCapabilities());
            addGLEventListener(this);  // 2.                              <-- HERE
        }
    
        @Override
        public void display(GLAutoDrawable arg0) {
            System.out.println("hello");
            GL gl = arg0.getGL();
            gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
            gl.glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
        }
    
        @Override
        public void displayChanged(GLAutoDrawable arg0,
                boolean arg1, boolean arg2) {}
    
        @Override
        public void init(GLAutoDrawable arg0) {}
    
        @Override
        public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
                int arg4) {}
    }
    

    Note the comments. You missed only two elements:

    • Adding GLEventListener to GLCanvas. Normally You don’t extend GLCanvas, implementing GLEventlistener is all that is required to get GL.

    • JPanel’s LayoutManager was unspecified, so no care was taken to layout the canvas at full extent

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

Sidebar

Related Questions

I need to write a delegate function that can 'wrap' some while/try/catch code around
When I try to open a file in write mode with the following code:
I try to write a macro in clojure that sets up a namespace and
When I try to write code like the following source code, Visual Studio doesn't
I try to write KSH script for processing a file consisting of name-value pairs,
I try to write to a large file, but it seems like it does
I try to write a simple client/server application (all application is a bluetooth service
I try to write a simple Markdown parser in JavaScript. Therefore I want to
I try to write a simple user script to enlarge the picture when you
I try to write the following in latex: \begin{itemize} \item \textbf{insert(element|text)} inserts the element

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.