简体   繁体   中英

How can I resize my circle in Java using OpenGL

How can I resize my circle shape in Java using OpenGL? I am very new to Java and OpenGL and I don't understand why my circle isn't being resized? I'm calling the function again with different parameters but for some reason my circle stays exactly the same and doesn't change, why is that?

I initially draw the circle calling drawCircle(..,..,..,..) in my display(..) function

You'll notice that when a key is pressed; 'a' , I want to increase the size of the circle

@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')
        {

        }
    }

For some reason though it just doesn't even change my initial circle drawn whatsoever.

I have:

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

    }



}

You'll need to give the updated radius in the display function to get it drawn. Otherwise the image gets overwritten by it. So store the radius into a variable, for example under float rot; and don't draw when pressing 'a', only update the variable. Then in the display function use the variable:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM