简体   繁体   English

我无法在Eclipse中运行此Java文件

[英]I can't run this java file in Eclipse

When ever i try to run this in eclipse as a Java project, nothing happens but if i put it all in the "public static void main(String[] args)", then it works but thats not how it's done in the video i'm learning from 每当我尝试在eclipse中将其作为Java项目运行时,什么都不会发生,但是如果我将其全部放入“ public static void main(String [] args)”中,则它可以工作,但是那不是在视频中完成的方式正在学习

 package Indeed;


import static org.lwjgl.opengl.GL11.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.lwjgl.opengl.*;
import org.lwjgl.*;
import org.lwjgl.input.Mouse;
import org.lwjgl.input.Keyboard;    


public class InputDemo {


public static void main(String[] args) {

}

List<Box> shapes = new ArrayList<Box>(16);

public InputDemo() {

    try {
        Display.setDisplayMode(new DisplayMode(640, 480));
        Display.setTitle("Hello, LWJGL!");
        Display.create();
    } 
    catch (LWJGLException e) 
    {

        e.printStackTrace();
    }

    shapes.add(new Box(15, 15));
    shapes.add(new Box(100, 150));

    //Initialization code OpenGL
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 640, 480, 0, 1, -1);
    glMatrixMode(GL_MODELVIEW);

    while(!Display.isCloseRequested())
    {
        //Render
        glClear(GL_ACCUM_BUFFER_BIT);

        if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
        {
            Display.destroy();
            System.exit(0);
        }

        for(Box box : shapes)
        {
            box.draw();
        }




        Display.update();
        Display.sync(60);
    }

    Display.destroy();
}

private static class Box
{
    public int x, y;
    private float red, blue, green;
    public boolean selected = false;

    Box (int x, int y)
    {
        this.x = x;
        this.y = y;

        Random rand = new Random();
        red = rand.nextFloat(); 
        blue = rand.nextFloat();
        green = rand.nextFloat();
    }

    void update(int dx, int dy)
    {
        x += dx;
        y += dy;
    }

    boolean inBounds(int mouseX, int mouseY)
    {
        if(mouseX > x && mouseX < x + 50 && mouseY > y && mouseY < y + 50)
        {
            return true;
        }
        else
        {
            return false;
        }

    }

    void RandomColor()
    {
        Random rand = new Random();
        red = rand.nextFloat(); 
        blue = rand.nextFloat();
        green = rand.nextFloat();
    }

    void draw()
    {
        glColor3f(red, green, blue);

        glBegin(GL_QUADS);
        glVertex2f(x, y);
        glVertex2f(x + 50, y);
        glVertex2f(x+ 50, y+ 50);
        glVertex2f(x, y + 50);
        glEnd();
    }
}


}

You dont need to put it ALL in the main function, but be aware that ONLY the code put within it will be run. 您无需将其全部放入main函数中,但要注意,只有其中的代码将运行。 If your video claims otherwise, it is lying to you . 如果您的视频有其他要求,则说明撒谎

You have all your code being called from the constructor. 您可以从构造函数中调用所有代码。 Ideally, you would move that to the main function. 理想情况下,您可以将其移至主要功能。 If you choose to leave it as it is, you would need to create an instance of the class for it to be executed. 如果选择保留原样,则需要创建该类的实例才能执行。 So you can add InputDemo demo = new InputDemo(); 因此,您可以添加InputDemo demo = new InputDemo(); to your main function and that might suffice.. 到您的主要功能,这可能就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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