我一直试图制作一个简单的程序,当按下按钮时会显示一条线,但是当我按下按钮没有任何反应时,只有在我调整屏幕大小后,才会显示该行。任何帮助将非常感谢!
我注意到只有在调整大小时才会调用display()。我正在使用一个简单的getter-setter类,键盘记录器,用于按下按钮的事件。
public class Main implements GLEventListener {
JFrame myframe;
GLCanvas mycanvas;
keyLogger KL = new keyLogger();
@Override
public void display(GLAutoDrawable drawable) {
final GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glColor3f(0.0f,0.0f,0.0f);
gl.glPointSize(4.0f);
//System.out.println("Dsp");
//System.out.println(KL.getButton1());
if(KL.getButton1() == 1) {
//System.out.println("If");
//System.out.println(KL.getButton1());
gl.glBegin(GL2.GL_LINES);
gl.glVertex3f(0.50f, -0.50f, 0);
gl.glVertex3f(-0.50f, 0.50f, 0);
gl.glEnd();
//gl.glFlush();
}
}
@Override
public void dispose(GLAutoDrawable arg0) {
}
@Override
public void init(GLAutoDrawable drawable) {
final GL2 gl = drawable.getGL().getGL2();
gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
gl.glMatrixMode(gl.GL_PROJECTION);
gl.glLoadIdentity();
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height){
GL2 gl = drawable.getGL().getGL2();
gl.glViewport(x, y, width, height);
}
//main method
public static void main(String[] args) {
Main main = new Main();
main.Canvas();
}
public void Canvas() {
final GLProfile profile = GLProfile.get(GLProfile.GL2);
GLCapabilities capabilities = new GLCapabilities(profile);
//
mycanvas = new GLCanvas(capabilities);
Main mainInstance = new Main();
mycanvas.addGLEventListener(mainInstance);
//
mycanvas.setSize(800, 600);
//
mainInstance.makeCanvas(mycanvas);
}
public void makeCanvas(GLCanvas mycanvas) {
//
myframe = new JFrame("Paint");
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//
myframe.getContentPane().add(BorderLayout.CENTER, mycanvas);
//
JButton button1 = new JButton("Button1");
button1.addActionListener(new button1_listener());
myframe.getContentPane().add(BorderLayout.NORTH, button1);
//
myframe.setSize(myframe.getContentPane().getPreferredSize());
myframe.setVisible(true);
//mycanvas.repaint();
}
class button1_listener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
//System.out.println("Lstn");
//System.out.println(KL.getButton1());
KL.setButton1(1);
}
}