2012-05-22 36 views
1

我已經打開了一個全屏窗口,但現在如何創建一個按鈕讓它退出應用程序?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(); 
     } 



    } 
} 
+0

*「我可以用我學與C++的java工作或者是OpenGL的完全不同的OpenGL的代碼?」 * - OpenGL的API是一樣的你是否使用LWJGL或C++,你應該沒有一旦學習其中之一就會遇到麻煩。 – Tim

+0

你知道我需要調用什麼函數關閉全屏模式下的應用程序嗎? – core16

+1

Display.setFullscreen(false);退出全屏或System.exit(0);終止應用程序 –

回答

1

LWJGL不提供任何高層控件,諸如按鈕。您需要使用gl調用來繪製按鈕(使用按鈕圖像作爲四邊形的紋理,在嘗試紋理之前從一個彩色矩形開始)。然後,您需要檢查按鈕區域中的鼠標點擊事件。您可能要考慮在lwjgl之上使用更高級別的庫來簡化此操作。

+0

如何使用LWJGL創建按鈕? – core16

+0

如何使用gl調用執行此操作 – core16

+0

在LWJGL wiki上繪製矩形(= Quad)有一個很好的例子,這將是第一步:http://www.lwjgl.org/wiki/index .php?title = LWJGL_Basics_3_(The_Quad)(注意鏈接末尾的關閉框) –

0

下面是我繪製和處理按鈕的一些代碼。

您可以指定每個按鈕的X,Y和紋理,並且當單擊該按鈕時,變量isClicked將變爲true。至於關閉應用程序,使用

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

Button類: 你需要LWJGL和油滑的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(); 
     } 

    } 
+0

這是一個老問題,但是謝謝! – core16

相關問題