1

I have made it open a full screen window but now how can i create a button to have it exit the application?

Also, do you know any good tutorials to learn. I can't seem to find many?

Lastly can i use the opengl code that i learn to work with java in c++ or is that opengl completely different?

This is the code i have:

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();
        }



    }
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
core16
  • 113
  • 2
  • 17
  • *"can i use the opengl code that i learn to work with java in c++ or is that opengl completely different?"* - The OpenGL API is the same whether you use lwjgl or c++, you should have no trouble using either once learning one of them. – Tim May 22 '12 at 18:07
  • Would you know anything about what function i need to call to close the application from fullscreen mode? – core16 May 22 '12 at 18:09
  • 1
    Display.setFullscreen(false); to exit fullscreen or System.exit(0); to terminate the app – Stefan Haustein May 22 '12 at 20:20

2 Answers2

1

lwjgl does not provide any high level widgets such as buttons. You'll need to draw the button using gl calls (use the button image as texture for a quad. Start with a colored rectangle before trying textures). Then you'll need to check for mouse click events in the button area. You may want to consider using a higher level library on top of lwjgl to simplify this.

Stefan Haustein
  • 18,427
  • 3
  • 36
  • 51
0

Here is some code that I made that draws and handles buttons.

You can specify the X, Y and Texture of each button and when the button is clicked the variable isClicked becomes true. As for closing the application , use

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

Button Class: You need LWJGL and 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();
        }

    }