我最近刚开始使用LWJGL,我制作了显示器和一条简单的线,但这条线一直闪烁着我知道这可能来自
while(!Display.isCloseRequested()){
Display.update();
}
部分以便保持刷新但是如果我删除了这部分并在制作线后添加了一个Display.update();
,则窗口打开一秒并关闭。这是完整的代码:
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);
}
}
答案 0 :(得分:1)
你必须在main方法中调用你的构造函数
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);
}}
答案 1 :(得分:0)
如何使用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);
}
}