Java OpenGL and jogl getting started example -- collision and moving cube

Source: Internet
Author: User

Step 1: Go to https://jogl.dev.java.net/download jogl-1.1.2-pre-20080523-windows-i586.zip
 
Decompress to a random folder, such as: C:/program files/Java/jogl-1.1.2-pre-20080523-windows-i586
 
Open eclipse, and click File --> New ---> Project ---> project name, for example, my_opengl ---- finish.
 
After creating a project, find the new project in the workspace on the left.

For example, my_opengl, right click --> properties --> JAVA build path select libraries ---> Add external jars
 
Find the original decompressed folder such as: C:/program files/Java/jogl-1.1.2-pre-20080523-windows-i586

Find Lib in it, open ---> double-click jogl. jar and gluegen-rt.jar to add these two jar packages to our libraries
 
In this case, you can see the jogl. jar and gluegen-rt.jar packages in libraries, select jogl. jar,
 
---> Native library location: ----> edit ---> external Folder
 
--> C:/program files/Java/jogl-1.1.2-pre-20080523-windows-i586/lib
 
This completes the configuration.
 
 
 

In the my_opengl project, create a Class, Class Name: opengl_bounce
 
CTRL + C first, copy the following code to the class,

Import Java. AWT. borderlayout; import Java. AWT. frame; import Java. AWT. headlessexception; import Java. AWT. event. windowadapter; import Java. AWT. event. using wevent; 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; public class opengl_bounce extends frame impleme CNT gleventlistener, runnable {float X1 = 100366f; // X coordinate float Y1 = 150366f; // y coordinate long rsize = 50; // The reserved position for the rectangle width or the distance from float xstep = 1.0f; // The distance along the X step displacement. Of course, you can increase the float ystep = 1.0f; // float horizontal wwidth along the y step; // This is not the width of the form border, but the float horizontal wheight on the right side of the visual projection; // It is not the height of the form border, it is the top GL of visual projection; // the main interface glcanvas of OpenGL; // similar to Java. AWT. canvas and glcanvas are mainly used to display the effects of various OpenGL glcapabilities capabilities; // Set a set of OpenGL functions: the rendering content must be supported, such as the color depth and whether the stereo is enabled. Thread mythread = new thread (this); Public opengl_bounce () throws headlessexception {This. capabilities = new glcapabilities (); // instantiate capabilities this. glcanvas = new glcanvas (capabilities); // instantiate glcanvas this. glcanvas. addgleventlistener (this); // Add a GL event to glcanvas to process this. add (glcanvas, borderlayout. center); // Add a component: glcanvas this. addwindowlistener (New windowadapter () {// Add the Close event public void windowclosing (LOGIN wevent e) to the form {system. exit (0) ;}}); mythread. start (); // main thread start} public static void main (string [] ARGs) {opengl_bounce F = new opengl_bounce (); F. settitle ("moving moment"); F. setsize (800,600); F. setvisible (true);} public void display (glautodrawable drawable) {If (x1> export wwidth-rsize | X1 <0) {xstep =-xstep ;} if (Y1> running wheight-rsize | Y1 <0) {ystep =-ystep;} If (x1> running wwidth-rsize) {x1 = running wwidth-rsize ;} if (Y1> Returns wheight-rsize) {y1 = returns wheight-rsize;} X1 + = xstep; Y1 + = ystep; GL. glclear (GL. gl_color_buffer_bit); GL. glcolor3f (1.0f, 0.0f, 0.0f); GL. glrectf (x1, Y1, X1 + rsize, Y1 + rsize); GL. glflush (); glcanvas. swapbuffers ();} public void displaychanged (glautodrawable drawable, Boolean arg1, Boolean arg2) {} public void Init (glautodrawable drawable) {GL = drawable. getgl (); GL. glclearcolor (0.0f, 0.0f, 1.0f, 1.0f);} public void reshape (glautodrawable drawable, int X, int y, int W, int h) {If (H = 0) {H = 1;} GL. glviewport (0, 0, W, H); GL. glmatrixmode (GL. gl_projection); GL. glloadidentity (); If (W <= h) {This. required wwidth = 2500000f; this. required wheight = 250366f * H/W;} else {This. required wwidth = 2500000f * W/H; this. required wheight = 250366f;} GL. glortho (0.0f, 1_wwidth, 0.0f, windowheight, 1.0f,-1.0f); GL. glmatrixmode (GL. gl_modelview); GL. glloadidentity ();} public void run () {While (true) {glcanvas. display (); // The main thread does not stop calling display () Try {mythread. sleep (20); // a rest of 20 milliseconds after each execution} catch (interruptedexception ex) {ex. printstacktrace ();}}}}

Let's not talk about the runnable interface. This is a multi-threaded interface,
 
We can directly see the implements gleventlistener in the above Code, which is used to declare a client code to manage events rendered by OpenGL.
 
Four methods in the gleventlistener interface must be overwritten (of course, instead of re-writing, eclipse will automatically prompt you to load these methods ):

Public void Init (glautodrawable drawable) is called at the initial test. Public void display (glautodrawable drawable) // is called as a drawing action, that is, the Main drawing function {If (x1> drawing wwidth-rsize | X1 <0) {when the X coordinate of the moment type is greater than the rightmost or less than 0 of the view, move backward x} public void displaychanged (glautodrawable drawable, Boolean arg1, Boolean arg2) // The Public void reshape (glautodrawable drawable, int X, int y, int W, int h) {}// when the screen size changes, it will be called at least once at the beginning. final Result: xstep =-xstep;} If (Y1> windowheight-rsize | Y1 <0) {when the Y coordinate of the moment is greater than the rightmost or less than 0 of the vision, take ystep =-ystep in the direction of Y;} If (x1> wxwwidth-rsize) {when the X coordinate of the rectangle arrives at the rightmost of the view, first stop X1 = running wwidth-rsize;} If (Y1> running wheight-rsize) {when the Y coordinate of the moment type reaches the rightmost of the view, first stop Y1 = running wheight-rsize ;} x1 + = xstep; Y1 + = ystep; GL. glclear (GL. gl_color_buffer_bit); In Init (), the background color is set to blue, and GL needs to be cleared here. glcolor3f (1.0f, 0.0f, 0.0f); set the paint brush color to Red GL. glrectf (x1, Y1, X1 + rsize, Y1 + rsize); rectangle type, coordinate (x1, Y1) GL. glflush (); refresh glcanvas. swapbuffers (); Update foreground buffer and background buffer {GL = drawable. getgl (); Use the drawable device to obtain the GL interface. This statement is required. Otherwise, an error is returned. glclearcolor (0.0f, 0.0f, 1.0f, 1.0f); sets the current background clearing color to Blue RGB (, 1), and the system defaults to black}

From: http://blog.csdn.net/wjyjimy2/article/details/4055350

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.