如何使用OpenGL在Java中调整我的圆圈大小

时间:2016-09-11 06:32:01

标签: java opengl

如何使用OpenGL在Java中调整圆形的大小?我是Java和OpenGL的新手,我不明白为什么我的圈子没有调整大小?我用不同的参数再次调用该函数但由于某种原因,我的圆圈保持完全相同并且不会改变,为什么呢?

我最初在我的drawCircle(..,..,..,..)函数

中绘制了调用display(..)的圈子

当按下某个键时,您会注意到; 'a',我想增加圆圈的大小

@Override
    public void keyTyped(KeyEvent e) 
    {
        // TODO Auto-generated method stub
        char key= e.getKeyChar();
        System.out.printf("Key typed: %c\n", key); 

        // Make shape bigger
        // increase size of circle
        if(key == 'a')
        {

            drawCircle(test, 10.0f, 10.0f, 10.0f);
        }

        // move right
        if(key == 'f')
        {

        }
    }

出于某种原因,虽然它甚至没有改变我所绘制的初始圆圈。

我有:

JoglEventListener.java

package helloOpenGL;

/*************************************************************************************
*   IMPORTS     
**************************************************************************************/
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import com.jogamp.opengl.*;
import com.jogamp.opengl.glu.GLU;

/*************************************************************************************
*   JoglEventListener   
**************************************************************************************/
public class JoglEventListener implements GLEventListener, KeyListener 
{


    float rot; 
    GL2 gl = null;
    GLAutoDrawable test = null;
    // Instantiate GLU thing?
    private GLU glu = new GLU();

    /*************************************************************************************
    *   displayChanged 
    *   What does this do?  
    **************************************************************************************/
    public void displayChanged(GLAutoDrawable gLDrawable, 
                boolean modeChanged, boolean deviceChanged) 
    {
        // Function that does nothing?
    }

    /** Called by the drawable immediately after the OpenGL context is
    * initialized for the first time. Can be used to perform one-time OpenGL
    * initialization such as setup of lights and display lists.
    * @param gLDrawable The GLAutoDrawable object.
    */

    /*************************************************************************************
    *   init    
    **************************************************************************************/
    public void init(GLAutoDrawable gLDrawable) 
    {
        GL2 gl = gLDrawable.getGL().getGL2();
        //gl.glShadeModel(GL.GL_LINE_SMOOTH);              // Enable Smooth Shading
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);    // Black Background
        gl.glClearDepth(1.0f);                      // Depth Buffer Setup
        gl.glEnable(GL.GL_DEPTH_TEST);              // Enables Depth Testing
        gl.glDepthFunc(GL.GL_LEQUAL);               // The Type Of Depth Testing To Do
        // Really Nice Perspective Calculations
        //gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);  
    }

    /*************************************************************************************
    *   reshape ?
    *   ?   
    **************************************************************************************/     
    public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, 
                int height) 
    {
        gl = gLDrawable.getGL().getGL2();

        if (height <= 0) // avoid a divide by zero error!
            height = 1;
        final float h = (float) width / (float) height;
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(45.0f, h, 1.0, 200.0);
        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glTranslatef(0.0f, 0.0f, -40.0f);
    }

    public void drawCircle(GLAutoDrawable gLDrawable, float x, float y, float radius)
    {
        GL2 gl = gLDrawable.getGL().getGL2();
        int i;
        test = gLDrawable;
        //GLfloat radius = 0.8f; //radius
        float twicePi = (float) (2.0f * Math.PI);

        gl.glBegin(GL.GL_LINE_LOOP);
            for(i = 0; i <= 360;i++) 
            { 
                gl.glVertex2f(
                    x + ((float)(radius * Math.cos(i *  twicePi / 360))), 
                    y + ((float)(radius* Math.sin(i * twicePi / 360)))
                );
            }
        gl.glEnd();
    }



    @Override
    public void display(GLAutoDrawable gLDrawable) 
    {
        // TODO Auto-generated method stub
        final GL2 gl = gLDrawable.getGL().getGL2();

        gl.glClearColor(backrgb[0], 0, 1, 1);
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

        backrgb[0]+=0.0005;
        if (backrgb[0]> 1) backrgb[0] = 0; 

        //  DRAW STUFF IN THIS FUNCTION
        drawCircle(gLDrawable, 5.0f, 5.0f, 3.0f);

    }

    @Override
    public void dispose(GLAutoDrawable arg0) 
    {
        // TODO Auto-generated method stub

    }   

    @Override
    public void keyTyped(KeyEvent e) 
    {
        // TODO Auto-generated method stub
        char key= e.getKeyChar();
        System.out.printf("Key typed: %c\n", key); 

        // Move Horizontally
        // move left
        if(key == 'a')
        {

            drawCircle(test, 10.0f, 10.0f, 10.0f);
        }

        // move right
        if(key == 'f')
        {

        }
    }

    @Override
    public void keyPressed(KeyEvent e) 
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyReleased(KeyEvent e) 
    {
        // TODO Auto-generated method stub

    }



}

1 个答案:

答案 0 :(得分:0)

您需要在display函数中提供更新后的半径才能绘制它。否则图像会被覆盖。因此,将半径存储到变量中,例如在float rot;下,并且在按下&#39; a&#39;时不要绘制,只更新变量。然后在显示功能中使用变量:

drawCircle(gLDrawable, 5.0f, 5.0f, radius);