Member Avatar for mrkm1188
mrkm1188

I'm trying to get the objects I draw on the screen when I press the up and down arrows. This is the code I have so far but I don't know how to call redrawRoom() from the keyPressed() method.

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;

import com.jogamp.newt.event.KeyEvent;
import com.jogamp.newt.event.KeyListener;
import com.jogamp.opengl.util.gl2.GLUT;

public class CGGame implements GLEventListener, KeyListener{
	
	private int w, h, z;
	private GLU glu;
	private GLUT glut;
	private GLAutoDrawable draw = null;

	
	public CGGame(){
	}
	
	public static void main(String[] args){
		GLProfile.initSingleton(false);
		GLProfile glp = GLProfile.getDefault();
		GLCapabilities caps = new GLCapabilities(glp);
		GLCanvas canvas = new GLCanvas(caps);
		canvas.addGLEventListener(new CGGame());
		Frame frame = new Frame("CG Game");
		frame.setSize(520, 640);
		frame.setVisible(true);
		frame.add(canvas);
		
		frame.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
	}
	
	
	@Override
	//this function will hold the code for drawing the room
	public void display(GLAutoDrawable drawable) {
			GL2 gl = drawable.getGL().getGL2();
	        
	        w = drawable.getWidth();
	        h = drawable.getHeight();
	        
	        gl.glClear(GL2.GL_COLOR_BUFFER_BIT|GL2.GL_DEPTH_BUFFER_BIT);			
	          
	        gl.glViewport(0, 0, w, h);											
	        
	        gl.glMatrixMode(GL2.GL_PROJECTION);									
	        gl.glLoadIdentity();												
	    	
	        glu.gluPerspective(45.0f,(float)w/(float)h,0.1f,100.0f);			

	        gl.glMatrixMode(GL2.GL_MODELVIEW);									
	        gl.glLoadIdentity();												     

	        //drawStartRoom(drawable);													
	}
	//this function draws the room. This is the view of what the room
	//looks like each time they start the game, or advance to the next level
	public void drawStartRoom(GLAutoDrawable drawable){

		GL2 gl = drawable.getGL().getGL2();		
		gl.glMatrixMode(GL2.GL_PROJECTION);
		gl.glLoadIdentity();
		gl.glMatrixMode(GL2.GL_MODELVIEW);
		gl.glLoadIdentity();
		
		gl.glPushMatrix();//enter the code for drawing the left wall
			gl.glBegin(GL2.GL_LINES);
				gl.glVertex3d(-1.0f,-1.0f,0.0f);//point A
				gl.glVertex3f(-0.5f,-0.4f,1.0f);//point B
				gl.glVertex3f(1.0f, -1.0f,0.0f);//point H
				gl.glVertex3f(0.5f,-0.4f,1.0f);//point G
				gl.glVertex3f(-1.0f,1.0f,0.0f);//point D
				gl.glVertex3f(-0.5f,0.4f,1.0f);//point C
				gl.glVertex3f(1.0f,1.0f,0.0f);//point E
				gl.glVertex3f(0.5f,0.4f,1.0f);//point F
				//bottom far wall point B to G
				gl.glVertex3f(-0.5f,-0.4f,1.0f);//point B
				gl.glVertex3f(0.5f,-0.4f,1.0f);//point G	
				//top far wall point C to F
				gl.glVertex3f(-0.5f,0.4f,1.0f);//point C
				gl.glVertex3f(0.5f,0.4f,1.0f);//point F
				//right far wall, point G to F
				gl.glVertex3f(0.5f,-0.4f,1.0f);//point G
				gl.glVertex3f(0.5f,0.4f,1.0f);//point F
				//left far wall, point B to C
				gl.glVertex3f(-0.5f,-0.4f,1.0f);//point B
				gl.glVertex3f(-0.5f,0.4f,1.0f);//point C
				//draw the door
				//left side of frame
				gl.glVertex3f(-0.2f,-0.4f,1.0f);//bottom left
				gl.glVertex3f(-0.2f,0.17f,1.0f);//top left
				//right side of frame
				gl.glVertex3f(0.2f,-0.4f,1.0f);//bottom right
				gl.glVertex3f(0.2f,0.17f,1.0f);//top right
				//top door frame
				gl.glVertex3f(-0.2f,0.17f,1.0f);//top left
				gl.glVertex3f(0.2f,0.17f,1.0f);//top right
				//draw the door handle
				gl.glVertex3f(-0.15f,-0.1f,1.0f);
				gl.glVertex3f(-0.15f,-0.2f,1.0f);
			gl.glEnd();
		gl.glPopMatrix();
	}
	
	//redraw the room given how deep you want it to be 
	public void redrawRoom(GLAutoDrawable drawable){
		//have the same code form the original room, but put glTranslatef after each push matrix
		//and have the z in all of the drawing coordinates to be the current depth (+/-) 
		// the depth given as a parameter (zDeep). 
		GL2 gl = drawable.getGL().getGL2();
		gl.glMatrixMode(GL2.GL_PROJECTION);
		gl.glLoadIdentity();
		gl.glMatrixMode(GL2.GL_MODELVIEW);
		gl.glLoadIdentity();
		gl.glTranslatef(0.0f,0.0f,-75.0f);
		
		gl.glPushMatrix();//enter the code for drawing the left wall
			gl.glBegin(GL2.GL_LINES);
				gl.glVertex3d(-1.0f,-1.0f,0.0f);//point A
				gl.glVertex3f(-0.5f,-0.4f,1.0f);//point B
				gl.glVertex3f(1.0f, -1.0f,0.0f);//point H
				gl.glVertex3f(0.5f,-0.4f,1.0f);//point G
				gl.glVertex3f(-1.0f,1.0f,0.0f);//point D
				gl.glVertex3f(-0.5f,0.4f,1.0f);//point C
				gl.glVertex3f(1.0f,1.0f,0.0f);//point E
				gl.glVertex3f(0.5f,0.4f,1.0f);//point F
				//bottom far wall point B to G
				gl.glVertex3f(-0.5f,-0.4f,1.0f);//point B
				gl.glVertex3f(0.5f,-0.4f,1.0f);//point G	
				//top far wall point C to F
				gl.glVertex3f(-0.5f,0.4f,1.0f);//point C
				gl.glVertex3f(0.5f,0.4f,1.0f);//point F
				//right far wall, point G to F
				gl.glVertex3f(0.5f,-0.4f,1.0f);//point G
				gl.glVertex3f(0.5f,0.4f,1.0f);//point F
				//left far wall, point B to C
				gl.glVertex3f(-0.5f,-0.4f,1.0f);//point B
				gl.glVertex3f(-0.5f,0.4f,1.0f);//point C
				//draw the door
				//left side of frame
				gl.glVertex3f(-0.2f,-0.4f,1.0f);//bottom left
				gl.glVertex3f(-0.2f,0.17f,1.0f);//top left
				//right side of frame
				gl.glVertex3f(0.2f,-0.4f,1.0f);//bottom right
				gl.glVertex3f(0.2f,0.17f,1.0f);//top right
				//top door frame
				gl.glVertex3f(-0.2f,0.17f,1.0f);//top left
				gl.glVertex3f(0.2f,0.17f,1.0f);//top right
				//draw the door handle
				gl.glVertex3f(-0.15f,-0.1f,1.0f);
				gl.glVertex3f(-0.15f,-0.2f,1.0f);
			gl.glEnd();
		gl.glPopMatrix();	
	}
	
	
	@Override
	public void dispose(GLAutoDrawable drawable) {
		
		
	}

	@Override
	public void init(GLAutoDrawable drawable) {
		w = drawable.getWidth();
        h = drawable.getHeight();
        
        GL2 gl = drawable.getGL().getGL2();
        glu = new GLU();
        glut = new GLUT();
        
        gl.glShadeModel(GL2.GL_SMOOTH);
        gl.glClearColor(0.0f,0.0f,0.0f,0.0f);
        gl.glClearDepth(1.0f);
        gl.glClearDepth(1.0f);												
    	gl.glEnable(GL2.GL_DEPTH_TEST);										
    	gl.glDepthFunc(GL2.GL_LEQUAL);										
    	gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);			
	}

	@Override
	public void reshape(GLAutoDrawable drawable, int x, int y, int w2, int h2) {	
		GL2 gl = drawable.getGL().getGL2();
        
        w2 = drawable.getWidth();
        h2 = drawable.getHeight();
        
        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();
        
        // perspective view
        gl.glViewport(10, 10, w-20, h-20);
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(45.0f,(float)w/(float)h,0.1f,100.0f);
	}
	
	public void keyPressed(KeyEvent key){
		GLAutoDrawable drawable = null;
		
		if(key.getKeyCode() == KeyEvent.VK_UP){	
			redrawRoom(drawable);
		}
		if(key.getKeyCode() == KeyEvent.VK_DOWN){
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.