user3314952
user3314952

Reputation: 21

Java LWJGL display keeps flashing

I have lately started in LWJGL and I made the display and a simple line but this line keeps blinking on and off I know that was probably from the

while(!Display.isCloseRequested()){
    Display.update();
}

part so that keeps refreshing but If I removed this part and added one Display.update(); after making the line, the window opens for one second and closes. Here is the whole code:

package gaming;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

public class game {

public static void setGL(){


    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glOrtho(0, Display.getWidth(), 0, Display.getHeight(), -1, 1);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);

}

public game(int w, int h){

    try {
        Display.setDisplayMode(new DisplayMode(800, 600));
        Display.create();

        setGL();

        GL11.glColor3d(0.25, 0.75, 0.50);

        GL11.glClear( GL11.GL_COLOR_BUFFER_BIT );
        GL11.glBegin(GL11.GL_LINES);    
        GL11.glVertex2i(100, 100);
        GL11.glVertex2i(200, 200);          
        Display.update();
        GL11.glEnd();


        while(!Display.isCloseRequested()){


            Display.update();

        }
        while(Display.isCloseRequested()){
            Display.destroy();
            }



    } catch (LWJGLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}


public static void main(String[] args) {

    new game(800, 600);

}
}

Upvotes: 1

Views: 310

Answers (2)

aterai
aterai

Reputation: 9808

How about using the ScheduledExecutorService:

import java.awt.*;
import java.util.concurrent.*;
import javax.swing.*;

public class pro7 {
  public static void main(String[] args) {
    final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    scheduler.schedule(new Runnable() {
      @Override public void run() {
        EventQueue.invokeLater(new Runnable() {
          @Override public void run() {
            // TODO Auto-generated method stub
            JOptionPane.showMessageDialog(null, "This is a new Message.");
            scheduler.shutdown();
          }
        });
      }
    }, 5000, TimeUnit.MILLISECONDS);
  }
}

Upvotes: 0

Karim Massi
Karim Massi

Reputation: 77

you have to call your constructor in the main method

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.Timer;

public class pro6 extends JFrame{
public  static Timer t;
public pro6() {
        System.out.println("heloo");

     t = new Timer(5000, new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
            JOptionPane.showMessageDialog(null, "This is a new Message.");
        }

    });
 t.start();


 setSize(new Dimension(200, 60));
 setDefaultCloseOperation(DISPOSE_ON_CLOSE);

}
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       new pro6().setVisible(true);
    }}

Upvotes: 1

Related Questions