LWJGL - How to create a button to close the application - java

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

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.

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

Related

Image moving side to side

My question is I'm creating a game where I have to get an image moving from one side of the screen to the other, and then coming back from the same side of the screen. For example I have an image of a jellyfish, it starts moving from the right side of the screen, to the left side, and then I want it to come back from the right side. I'm stuck and don't know what to do. I'm using LIBGDX and JAVA.
My code so far is :
package gdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import java.awt.Graphics;
public class Main extends ApplicationAdapter {
SpriteBatch batch;
Texture img;
Sprite sprite;
float fGrav, fVelo, fX, fY;
#Override
public void create() {
batch = new SpriteBatch();
img = new Texture("JellyFish.png");
sprite = new Sprite(img);
sprite.setScale(0.3f);
fX=0;
fY=0;
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
sprite.draw(batch);
batch.end();
}
#Override
public void dispose() {
batch.dispose();
img.dispose();
}
}
If you want to continuously bounce an image from left to right, create an infinite loop that increases x until it is >= your display width, and then decrease it until it is <= 0, and then repeat.
I've never worked with LIBGDX, but if you wanted to use the Graphics class it would be as follows:
Example:
boolean f = true;
int x = 0;
while (f) {
while (x <= SCREEN_WIDTH) {
x++;
}
while (x >= 0) {
x--;
}
}
This should loop indefinitely.

In my flappy bird game, why isn't the bird updating every time I move it?

Here is my code:
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import acm.graphics.GLabel;
import acm.program.GraphicsProgram;
import javax.imageio.ImageIO;
import acm.util.MediaTools;
public class FlappyBird extends GraphicsProgram {
public Background background; //background image
public UpTube uptube; //one of the pipes
public DownTube downtube; //other pipe
public Bird bird;
//image for the bird
public static final int APPLICATION_WIDTH = 882;
public static final int APPLICATION_HEIGHT = 772;
public void run(){
addKeyListeners();
background = new Background();
add(background);
uptube = new UpTube();
add(uptube);
downtube = new DownTube();
add(downtube);
bird = new Bird();
add(bird);
public void jump(){
for(int i =0;i<5;i++){
bird.move(3,-7);
pause(100);
}
for(int i =0;i<15;i++){
bird.move(5, -4);
pause(100);
}
for(int i =0;i<15;i++){
bird.move(7,0);
pause(100);
}
for(int i =0;i<15;i++){
bird.move(5,7);
pause(100);
}
for(int i =0;i<15;i++){
bird.move(3,-7);
pause(100);
}
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_SPACE){
jump();
However, when I run this and I press the Space Bar, it doesn't show the bird's individual movements, it just teleports the bird to the end location after the pause(100) is over for each for statement. How do I make it so that it updates the bird's location each time I move it?
I dont know about the API you are using but I have made many graphics utilities, games and programs in java and there are some basic principles you must know; the one you seem to be having an 'issue' with is you assume the rendering is done one another thread (happening while this code is running) or will completely redraw whenever the bird moves - this is not the case in most graphics renderers, instead they simply redraw after all processing of every frame.
So, what you will need to do is either get rendering on another thread so the for loop can run while the rendering is happening at a different rate, or, implement more state-machine like code where it knows what it should do each frame (e.g move the bird every 100ms, 15 times).

Move a ball on mouse click in Java

I'm trying to create the classic Breakout game as part of my programming assignment. I have to start moving the ball on a mouse click from the user. So I'm using a mouselistener to achieve that. The code below is just a smaller, simpler version of what I'm trying to do. But it does not move the ball in gradual steps. It just displays the ball at it's final position after the while loop is done executing.
import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class BallMoveTest extends GraphicsProgram {
public void run() {
ball = new GOval(20,20);
ball.setFilled(true);
add(ball, 100, 100);
addMouseListeners();
}
public void mouseClicked(MouseEvent e) {
while (counter < 100) {
moveBall();
counter++;
pause(20);
}
}
public void moveBall(){
ball.move(2, 2);
}
// Private instance variables
private GOval ball;
private int counter = 1;
}
However this alternate code works wonderfully well, but does not allow the user to click to start the movement of the ball.
import acm.program.*;
import acm.graphics.*;
public class TestGOval extends GraphicsProgram {
public void run() {
int counter = 1;
GOval ball = new GOval(20,20);
ball.setFilled(true);
add(ball,100,100);
while (counter < 100) {
ball.move(2, 2);
counter++;
pause(20);
}
}
}
Could someone point out what I'm doing wrong here and more importantly, why the first code block not work as intended?
PS: This is my first question, and I'm a novice at programming. Go easy on me if you can. :)
It may just be that you aren't showing all of your code, but you should have a class that implements MouseListener. Just having the mouse clicked method isn't enough for java to recognise that this is your intention; there's a tutorial here that has more detail: http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
One conceptual solution could be to add a thread class which can access all the objects positions (at least the ball in your case). This thread must be able to refresh the canvas of your GraphicsProgram class. You can give to this thread a refresh frequency of 30Hz, making it sleep for 33ms after each refresh.
If you need more details about how to refresh your canvas you should provide us more details.
With such solution you also need to put a sleep of 33 ms into your while loop.
use this code:
import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class BallMoveTest extends GraphicsProgram {
public void run() {
ball = new GOval(20,20);
ball.setFilled(true);
add(ball, 100, 100);
addMouseListeners();
waitForClick();
animation();
}
public void animation() {
while(counter<100){
moveBall();
pause(DELAY);
}
}
public void moveBall(){
ball.move(2, 2);
}
// Private instance variables
private GOval ball;
private int counter = 1;
private int DELAY=20;
}

Java - LWJGL - OS X Retina Support

I searched so long for a solution to run a simple LWJGL application on my MacBook Pro with Retina display. The problem is that the Retina display is flickering. But I just found uninformative hints.
Do you know any code solution to handle this? For example changing the viewport or something?
What have I to add to the following code?
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
public class MainDisplay {
final int DISPLAY_WIDTH = 640;
final int DISPLAY_HEIGHT = 480;
public void start() {
try {
Display.setDisplayMode(new DisplayMode(DISPLAY_WIDTH, DISPLAY_HEIGHT));
Display.create();
}
catch(LWJGLException exception) {
exception.printStackTrace();
}
while(!Display.isCloseRequested()) {
Display.update();
}
Display.destroy();
}
}
Did you actually try to render anything?
It sounds like the back buffer contains garbage data, and you're not painting anything in that loop. Every time you invoke the Display.update() it flips the back-buffer, and if you've not painted on it then you get the flickering of garbage data.
Try something like:
while(!Display.isCloseRequested()) {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glEnd();
Display.update();
}
Which clears the display before flipping the back-buffer.
In general, if you don't actually display anything then you're at the whims of the graphics driver and memory as to what ends up on the screen. If I used your code, I see the following in the window:

Java - Scroll image by mouse dragging

I would like to make a join.me like in Java.
I've made the screen capture part but now I want to scroll in the image by dragging the mouse.
Here is a screen of what i've made:
First of all, I want to replace the scroll bars by mouse dragging the image. Is it possible?
Then I want to remove those scroll bars.
Today, users are able to change the zoom and use their mouse wheel to zoom in/out.
Could you help me?
Thanks.
Edit: I've found the way to hide the scroll bar using:
this.jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
this.jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
Finally, I did it myself. Here is the solution if someone need it:
Create a new class named HandScrollListener with the following code:
import java.awt.Cursor;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;
import javax.swing.JViewport;
public class HandScrollListener extends MouseAdapter
{
private final Cursor defCursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
private final Cursor hndCursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
private final Point pp = new Point();
private JLabel image;
public HandScrollListener(JLabel image)
{
this.image = image;
}
public void mouseDragged(final MouseEvent e)
{
JViewport vport = (JViewport)e.getSource();
Point cp = e.getPoint();
Point vp = vport.getViewPosition();
vp.translate(pp.x-cp.x, pp.y-cp.y);
image.scrollRectToVisible(new Rectangle(vp, vport.getSize()));
pp.setLocation(cp);
}
public void mousePressed(MouseEvent e)
{
image.setCursor(hndCursor);
pp.setLocation(e.getPoint());
}
public void mouseReleased(MouseEvent e)
{
image.setCursor(defCursor);
image.repaint();
}
}
Then in your frame put:
HandScrollListener scrollListener = new HandScrollListener(label_to_move);
jScrollPane.getViewport().addMouseMotionListener(scrollListener);
jScrollPane.getViewport().addMouseListener(scrollListener);
It should work!

Categories