gpt4 book ai didi

java - 使用用户输入半径在 JOGL 中创建圆

转载 作者:太空宇宙 更新时间:2023-11-04 10:21:17 24 4
gpt4 key购买 nike

我想接受用户输入来获取半径值,并使用它在 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

}

}

最佳答案

要么在主函数上方创建一个 static 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关键字

public 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
}

关于java - 使用用户输入半径在 JOGL 中创建圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51135821/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com