📜  JOGL Hello World(1)

📅  最后修改于: 2023-12-03 15:16:40.457000             🧑  作者: Mango

JOGL Hello World

JOGL (Java Binding for the OpenGL) is a Java API for OpenGL rendering. In this tutorial, we will create a simple JOGL program that displays a "Hello World!" message on the screen.

Setup

Before starting, you need to make sure that you have JOGL installed on your machine. You can follow the instructions on the JOGL website to download and install JOGL.

Code

Here's the code for our JOGL Hello World program:

import javax.media.opengl.*;
import javax.swing.JFrame;

public class JOGLHelloWorld extends JFrame implements GLEventListener {

    public JOGLHelloWorld() {
        GLCanvas canvas = new GLCanvas();
        canvas.addGLEventListener(this);
        getContentPane().add(canvas);
    }

    public static void main(String[] args) {
        JOGLHelloWorld window = new JOGLHelloWorld();
        window.setSize(800, 600);
        window.setVisible(true);
    }

    @Override
    public void init(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    }

    @Override
    public void display(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
        gl.glColor3f(1.0f, 1.0f, 1.0f);
        gl.glRasterPos2f(-0.5f, 0.0f);
        String message = "Hello World!";
        for (int i = 0; i < message.length(); i++) {
            glut.glutBitmapCharacter(GLUT.BITMAP_HELVETICA_18, message.charAt(i));
        }
        gl.glFlush();
    }

    @Override
    public void dispose(GLAutoDrawable drawable) {
    }

    @Override
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
    }
}
Explanation

Let's go through the code line by line:

  1. We import the necessary JOGL and Swing packages.
  2. We define a class called JOGLHelloWorld that extends JFrame and implements GLEventListener.
  3. We create a constructor for JOGLHelloWorld that creates a GLCanvas, adds the GLEventListener, and adds the canvas to the content pane of the JFrame.
  4. We define the main method, which creates a new JOGLHelloWorld window, sets the size, and makes it visible.
  5. We implement the init method, which initializes the OpenGL context by setting the clear color to black.
  6. We implement the display method, which is called every time the canvas needs to be redrawn. We clear the color buffer, set the color to white, set the raster position to (-0.5, 0.0), and draw the "Hello World!" message using the glutBitmapCharacter function. Finally, we call glFlush to ensure that all the OpenGL commands have been executed.
  7. We implement the dispose method, which is called when the window is closed. We don't need to do anything here.
  8. We implement the reshape method, which is called when the window is resized. We don't need to do anything here.
Conclusion

That's it! You should now have a basic understanding of how to create a JOGL program that displays a message on the screen. You can modify this program to do much more exciting things with OpenGL. Happy coding!