Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CS293 Graphics, Lab 1, Setting up Netbeans and Simple 
Experiments
Use a web browser (preferably Firefox or Opera) to open the web page for the course:
http://www.computing.surrey.ac.uk/courses/cs293
Follow the instructions in the section on 'Lab Exercises' to download these files:
● jogl-1_0_0-windows-i586.zip
● jogl-1_0_0-docs.zip
● TeaPots.zip
● Simple.java
Unzip JOGL files and Chang Path environment variable
Unzip the contents of the jogl-1_0_0-windows-i586.zip file to a folder of your choosing. For 
example you may end up with a folder H:\JOGL\jogl-1_0_0-windows-i586\lib. Check that it 
contains the files:
● jogl.dll  
● jogl.jar  
● jogl_awt.dll  
● jogl_cg.dll
Change the Path evnvironment variable so that it includes this new folder. Right click on the My 
Computer icon on the desktop of your machine and select properties.
Next choose advanced and environment variables. If there is no user defined Path variable then 
choose new. Otherwise choose edit for Path. Add a semi-colon ';' to the end of the string and then 
type in the name of the folder you have placed jogl.jar etc. In the above case I would add:
'; H:\JOGL\jogl-1_0_0-windows-i586\lib'
to the end of the already defined Path.
Testing your JOGL installation
Unzip the TeaPots.zip file to a folder of your choice. This should contain the file 
TeaPotMapping.jar, and a folder lib containing another copy of the jogl.jar file. Do not move this 
second copy of jogl.jar or the example will not work.
If your machine has suitable Java file associations you should now be able to double click the 
TeaPotMapping.jar file to execute it. Otherwise start a Cygwin command shell (or windows 
command shell if you prefer) and cd to the folder containing  TeaPotMapping.jar. This command 
should now execute the file:
java -jar  TeaPotMapping.jar
At this point you should see a JFrame containing falling teapots rather like this one:
Creating a NetBeans Project With a JOGL library
These instructions are correct for NetBeans 5.0. I assume they will be more or less the same for 
whichever version of NetBeans comes after this.
First create a new project SimpleLab001, which is a Java application within NetBeans. Choose 
Simple as the main class. Suppose that you have chosen to make you're  SimpleLab001 project 
live in the H:\JOGL\SimpleLab001 folder. In that case replace the existing Simple.java file in the 
H:\JOGL\SimpleLab001\src\simplelab001 folder with the one you downloaded from the course 
web site earlier. 
If you open the Simple.java file in NetBeans you 
should see various error messages throughout the file. 
This is because NetBeans does not know where to 
find the JOGL library routines. 
Right click on the project name in the navigator view 
and select 
properties:
Click on libraries and choose Add Library:
A list of existing libraries will now appear. Choose the manage libraries option in order to create 
a new library containing the JOGL jar file. Click on New Library and give the new library the 
JOGL.
Click OK. Next select JOGL from the possible libraries and click on Add Jar/Folder:
Navigate to the lib folder where you have unpacked the  jogl-1_0_0-windows-i586.zip. In the 
example here that is the H:\JOGL\jogl-1_0_0-windows-i586\lib folder. Select jogl.jar and click 
Add Jar/Folder.
Now select add library to incorporate the JOGL library classes into the  SimpleLab001 project. 
Once you click on OK and return to the Simple.java file it should, after a short pause, be error 
free. Clean, build and run the project and you should see a JFrame like this:
First Exercise for Simple.java
First unzip the jogl-1_0_0-docs.zip file somewhere convenient. Such as the JOGL folder you 
created earlier. Open the index.html file. Find the glGetString method for the GL class. Find in 
the Simple.java code where this is used.
Download the RedBook PDF file from the web site given in the course notes. Search for the 
section 'Which Version Am I Using?'. 
Define String objects that store the values of GL_VENDOR, GL_RENDERER, GL_VERSION, 
and  GL_EXTENSIONS. 
Using the HelloWorldSwing.java as a guide, create a new JFrame within the init() method of the 
Simple class, which dispays the values you have just defined. Thus when you execute your 
application you should now have two JFrames, rather like this:
It is almost certain that the values you obtain for your machine will be different from those 
shown here.
Second Exercise
Add a new line 
        gl.glBegin(GL.GL_LINE_LOOP);  
immediately before the gl.glBegin(GL.GL_POLYGON) statement. What difference does this 
make?
Search for  GL_LINE_LOOP in the redbook. What happens if you try some of the alternatives 
given in the redbook in Table 2-2?
In Java we can define the angle 2п as 2*Math.PI. Also if we want to define the (x,y) coordinates 
of a point on a circle that is 'angle' degrees counterclockwise from the x axis this is given by the 
equation: ((float)Math.cos(angle), (float)Math.sin(angle)). Where we have defined the values as 
floating point numbers.
Instead of drawing a square as in the first part of the lab. Change the code so that it now draws 
an octahedron. That is an eight sided closed polygon, where each side is the same length. Note 
this could be drawn by working out eight points on a circle that are equally far apart. If they are 
equally far apart then the angle between the radial lines of two consecutive such points will be 
2п/8.
The result should look something like this:
Third Exercise
Change the reshape method to this:
 public void reshape(GLAutoDrawable drawable, int x, int y, int width,
          int height)
      {
          GL gl = drawable.getGL( );
            GLU glu = new GLU();
          gl.glViewport(0, 0, width, height);
          gl.glMatrixMode(GL.GL_PROJECTION);
          gl.glLoadIdentity ();
          glu.gluPerspective(30.0, 1.0, -1.0, 2.0);
          gl.glMatrixMode (GL.GL_MODELVIEW);
      }
Then change the display to this
public void display(GLAutoDrawable drawable)
      {
        GL gl = drawable.getGL( );
        GLU glu = new GLU();
        
        /* throw this in just to illustrate using FloatBuffer type for
           defining a vertex */
        FloatBuffer vec0 = FloatBuffer.wrap( new float[] {0.5f, -0.5f});
        
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
        gl.glColor3f(1.0f, 1.0f, 1.0f);
        
        glu.gluLookAt (0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
        gl.glBegin(GL.GL_POLYGON); 
            gl.glVertex2f(-0.5f, -0.5f);
            gl.glVertex2f(-0.5f, 0.5f);
            gl.glVertex2f(0.5f, 0.5f);
            gl.glVertex2fv(vec0); 
        gl.glEnd();
      }
Look up the  gluLookAt method in the redbook. Now try some different values for this method 
and see what happens.
Fourth Exercise
Next change the display method to this:
      public void display(GLAutoDrawable drawable)
      {
        GL gl = drawable.getGL( );
        GLU glu = new GLU();
        GLUT glut = new GLUT();
        FloatBuffer vec0 = FloatBuffer.wrap( new float[] {0.5f, -0.5f});
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
        gl.glColor3f(1.0f, 1.0f, 1.0f);
        
        glu.gluLookAt (0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
        glut.glutWireTeapot(0.3);
      }
What happens now, and what happens when you change the parameters for gluLookAt again?