【问题标题】:Using user input radius to create a circle in JOGL使用用户输入半径在 JOGL 中创建一个圆
【发布时间】:2018-07-02 12:04:24
【问题描述】:

我想获取用户输入来获取半径值并使用它在 java opengl (jogl) 中创建一个圆。半径变量名称是 rx。但是,当我尝试在 main() 中获取输入时,该变量在其他任何地方都无法识别。我也不能在这个函数之外输入。但是当我手动为 rx(radius) 分配一个值时,代码可以正常工作。我该怎么办?

package rrassi2;

import java.awt.Frame;
import java.util.Scanner;  

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import com.jogamp.newt.event.WindowListener;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.glu.GLU;

public class ellipse implements GLEventListener{

    /**
     * @param args
     */
    int pntX1 = 70,  pntY1=50,  ry=50;
    private GLU glu;

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner bucky = new Scanner(System.in);

         int rx = bucky.nextInt();
        bucky.close();


        GLProfile glp = GLProfile.get(GLProfile.GL2);
        GLCapabilities cap = new GLCapabilities(glp);
        GLCanvas canvas = new GLCanvas(cap);

        Frame frame = new Frame("Assignment1");
        frame.setSize(1200, 800);
        frame.add(canvas);
        frame.setVisible(true);

        ellipse l = new ellipse();
        canvas.addGLEventListener(l);  


        frame.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent arg0) {
                // TODO Auto-generated method stub
                System.exit(0);
            }});
        }




    public void display(GLAutoDrawable drawable) {
        // TODO Auto-generated method stub

        GL2 gl = drawable.getGL().getGL2();

        gl.glClear (GL2.GL_COLOR_BUFFER_BIT);
        //gl.glColor3f (0.0f, 0.0f, 0.0f);
        gl.glPointSize(1.0f);

        midPointCircleAlgo(gl);

    gl.glFlush ();
    }

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

    }

    public void init(GLAutoDrawable gld) {
        // TODO Auto-generated method stub

         GL2 gl = gld.getGL().getGL2();
         glu = new GLU();

        gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
        gl.glColor3f(0.0f, 0.0f, 0.0f);
        gl.glPointSize(4.0f);
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
    glu.gluOrtho2D(0.0, 640.0, 0.0, 480.0);

    }

    void plot(GL2 gl,int x, int y)
    {
        gl.glBegin(GL2.GL_POINTS);
        gl.glVertex2i(x+pntX1, y+pntY1);
        gl.glEnd();
    }

    void midPointCircleAlgo(GL2 gl)
    {
        int x = 0;
        int y = ry;
        float decision = ry^2-rx^2*ry+((rx^2)/4);
        plot(gl, x, y);

        while(! ((2*(ry^2)*x )> (2*(rx^2)*y)))
        {
            if (decision < 0)
            {
                x++; 
                decision += (2*(ry^2)*x)+ry^2 ;
            }
            else
            {
                y--;
                x++;
                decision +=(2*(ry^2)*x)-(2*(ry^2)*y)+ry^2;
            }
            plot(gl,x, y);
            plot(gl, -x, y);
            plot (gl, x,-y);
            plot (gl, -x, -y);



        }

        double decision2 = (((ry^2)*((x+0.5)*(x+0.5)))-((rx^2)*(ry^2))+((rx^2)*((y-1)^2)));
        plot(gl, x, y);

        while(y> 0)
        {
            if (decision2 > 0)
            {

                y--;
                decision2 += -(2*(rx^2)*y)+rx^2 ;
            }
            else
            {
                y--;
                x++;

                decision2 +=(2*(ry^2)*x)-(2*(rx^2)*y)+rx^2;
            }
            plot(gl,x, y);
            plot(gl, -x, y);
            plot (gl, x,-y);
            plot (gl, -x, -y);


        }

    }
    public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
            int arg4) {


        // TODO Auto-generated method stub

    }

}

【问题讨论】:

  • 仅用于实验创建类变量并在您读取其中的值时为其分配 rx。或获取 Scanner bucky = new Scanner(System.in);仅在您想要半径值的地方。

标签: java opengl jogl


【解决方案1】:

要么在主函数上方创建一个静态 int rx

static int rx;

int pntX1 = 70,  pntY1=50,  ry=50;
private GLU glu;

public static void main(String[] args) {
   // your code here
   rx = bucky.nextInt();
   // your code here
}

或者给你的类椭圆添加一个方法:

int pntX1 = 70,  pntY1=50,  ry=50;
int rx;  // <-------------- Note this new line here
private GLU glu; 

 public static void main(String[] args) {
    // your code here
    ellipse l = new ellipse();
    l.readRadius();
    // your code here
}

public void readRadius() {
  Scanner bucky = new Scanner(System.in);

  this.rx = bucky.nextInt();
  bucky.close();
}

注意

中的static关键字

公共 静态 void main(String[] args)

static 方法中,您只能为同样具有 static 关键字的变量赋值:

static int myAwesomeNumber;
int myOtherNumber;

static void myMethod() {
   myAwesomeNumber = 123; // this works
   myOtherNumber   = 789 // this does not work
}

void myOtherMethod() {
   myAwesomeNumber = 123; // this works
   myOtherNumber   = 789 // this works aswell because the method is not static
}

【讨论】:

  • 我尝试了这两种方法,并从控制台获取输入,但是要显示圆圈的框架变为空白。问题是在我可以在控制台中输入之前显示框架并且没有生成圆圈。
  • 能否请您向我们展示您当前的代码,例如在 pastebin 上?
猜你喜欢
  • 2022-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-20
相关资源
最近更新 更多