LWJGL - 如何创建一个按钮来关闭应用程序

时间:2012-05-22 16:59:58

标签: java opengl lwjgl

我已经打开了一个全屏窗口,但现在我如何创建一个按钮让它退出应用程序?

另外,你知道任何好的教程吗?我似乎找不到很多?

最后我可以使用我学习在c ++中使用java的opengl代码,还是那个opengl完全不同?

这是我的代码:

package game;

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

public class Main {

    public Main() {
        try {
            Display.setDisplayMode(Display.getDesktopDisplayMode());
            Display.setFullscreen(true);
            Display.create();
        } catch(LWJGLException e) {
            e.printStackTrace();
        }



    }
}

2 个答案:

答案 0 :(得分:1)

lwjgl不提供任何高级小部件,例如按钮。你需要使用gl调用绘制按钮(使用按钮图像作为四边形的纹理。在尝试纹理之前先用彩色矩形开始)。然后,您需要检查按钮区域中的鼠标单击事件。您可能需要考虑在lwjgl之上使用更高级别的库来简化此操作。

答案 1 :(得分:0)

以下是我制作的一些绘制和处理按钮的代码。

您可以指定每个按钮的X,Y和纹理,单击该按钮时变量isClicked变为true。至于关闭应用程序,请使用

if(EXITBUTTON.isClicked)
{
System.exit(0);
}

按钮类: 你需要LWJGL和Slick Util。

import java.awt.Rectangle;
import java.io.IOException;

import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;


public class Button {

    public int X;
    public int Y;
    public Texture buttonTexture;
    public boolean isClicked=false;
    Rectangle bounds = new Rectangle();


    public void addButton(int x, int y , String TEXPATH){
        X=x;
        Y=y;
        try {
            buttonTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(TEXPATH));
            System.out.println(buttonTexture.getTextureID());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        bounds.x=X;
        bounds.y=Y;
        bounds.height=buttonTexture.getImageHeight();
        bounds.width=buttonTexture.getImageWidth();
        System.out.println(""+bounds.x+" "+bounds.y+" "+bounds.width+" "+bounds.height);
    }

    public void Draw(){
        if(bounds.contains(Mouse.getX(),(600 - Mouse.getY()))&&Mouse.isButtonDown(0)){
            isClicked=true;
        }else{
            isClicked=false;
        }
        Color.white.bind();
        buttonTexture.bind(); // or GL11.glBind(texture.getTextureID());

        GL11.glBegin(GL11.GL_QUADS);
            GL11.glTexCoord2f(0,0);
            GL11.glVertex2f(X,Y);
            GL11.glTexCoord2f(1,0);
            GL11.glVertex2f(X+buttonTexture.getTextureWidth(),Y);
            GL11.glTexCoord2f(1,1);
            GL11.glVertex2f(X+buttonTexture.getTextureWidth(),Y+buttonTexture.getTextureHeight());
            GL11.glTexCoord2f(0,1);
            GL11.glVertex2f(X,Y+buttonTexture.getTextureHeight());
        GL11.glEnd();
        }

    }