Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [SOLVED] How to bind display() with the KeyBehaviour class?  (Read 616 times)
0 Members and 1 Guest are viewing this topic.
Offline Cyrus

Senior Newbie




Lean Mean Programming Machine


« Posted 2008-02-10 23:28:45 »

Sorry, it's very late, sometimes i make simple mistakes.

You can ofc always access variables/methods from other files by calling filename.variable

somehow it totally slipped my mind.




------------------------------------------
I have two seperate classes for handeling GLEvents and KeyEvents.

I want to bind the classes so, that if i press "W" that,the boolean statement becomes true.
now, nothing happens.

What should i do? Any tips are welcome!

(yeah ok, i'm new)


Greetings, Cyrus



///GfxFactory.java (GLEventListener)
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import java.awt.event.*;

public class GfxFactory extends KeyBehaviour implements GLEventListener {
   private final GLU glu = new GLU();
   ObjectLibrary ol = new ObjectLibrary();
   KeyBehaviour kb = new KeyBehaviour();
   
   float rot;
   
   public void display(GLAutoDrawable gld) {
      final GL gl = gld.getGL();
      
      gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
      
      gl.glMatrixMode(GL.GL_MODELVIEW);
      gl.glLoadIdentity();
      
      ol.camera(gl, kb.posx, kb.posy, kb.posz, kb.rotx, kb.roty, kb.rotz);
      
      gl.glTranslatef(0.0f, 0.0f, -5.0f);
      gl.glRotatef(rot, 1.0f, 0.0f, 0.0f);
      ol.cube(gl);
      
      rot += 4.0f;
      
      System.out.println(kb.FORWARD);
   }
   
   public void init(GLAutoDrawable gld) {
      final GL gl = gld.getGL();
      
      gl.glShadeModel(GL.GL_SMOOTH);
      gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
      gl.glClearDepth(1.0f);
      gl.glEnable(GL.GL_DEPTH_TEST);
      gl.glDepthFunc(GL.GL_LEQUAL);
      gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
   }
   
   public void reshape(GLAutoDrawable gld, int x, int y, int width, int height) {
      final GL gl = gld.getGL();
      
      if(height < 1)
         height = 1;
         
      float ratio = (float)width / (float)height;
      gl.glMatrixMode(GL.GL_PROJECTION);
      gl.glLoadIdentity();
      glu.gluPerspective(50.0f, ratio, 1.0f, 1000.0f);
      gl.glMatrixMode(GL.GL_MODELVIEW);
      gl.glLoadIdentity();
   }
   
   public void displayChanged(GLAutoDrawable gld, boolean modeChanged, boolean deviceChanged) {
   }
}


///KeyBehaviour.java
import java.awt.event.*;
import java.awt.*;
import javax.media.opengl.*;

public class KeyBehaviour implements KeyListener, MouseListener, MouseMotionListener {
   
   /*****************************
      GAME VARIABLES
   *****************************/
   
   final float SPEED = 2.0f;
   final float ANGLESPEED = 1.0f;
   
   float posx, posy, posz;
   float rotx, roty, rotz;
   float angle;
   
   boolean FORWARD = false;
   boolean BACKWARD = false;
   boolean STRAFELEFT = false;
   boolean STRAFERIGHT = false;
   
   
   /*****************************
      INTERFACE METHODS
   *****************************/
   
   GLCanvas glc;
   
   public void getGLCanvas(GLCanvas glc) {
      this.glc = glc;
   }
   
   
   /*****************************
      KEY PROCESSING
   *****************************/
   
   
   /*****************************
         KEYBOARD EVENTS
   *****************************/
   
   public void keyPressed(KeyEvent e) {
      switch(e.getKeyCode()) {
         case KeyEvent.VK_W:
               FORWARD = true;
            break;
         case KeyEvent.VK_S:
               BACKWARD = true;
            break;
         case KeyEvent.VK_A:
               STRAFELEFT = true;
            break;
         case KeyEvent.VK_D:
               STRAFERIGHT = true;
            break;
         case KeyEvent.VK_ESCAPE:
         case KeyEvent.VK_F10:
            System.exit(0);
            break;
      }
            
      glc.repaint();
   }
   
   public void keyReleased(KeyEvent e) {
      switch(e.getKeyCode()) {
         case KeyEvent.VK_W:
               FORWARD = false;
            break;
         case KeyEvent.VK_S:
               BACKWARD = false;
            break;
         case KeyEvent.VK_A:
               STRAFELEFT = false;
            break;
         case KeyEvent.VK_D:   
               STRAFERIGHT = false;
            break;
      }
            
      glc.repaint();
   }
   
   public void keyTyped(KeyEvent e) {
   }
   
   
   /*****************************
         MOUSE EVENTS
   *****************************/
   
   public void mousePressed(MouseEvent e) {
   }
   
   public void mouseReleased(MouseEvent e) {
   }
   
   public void mouseClicked(MouseEvent e) {
   }
   
   public void mouseEntered(MouseEvent e) {
   }
   
   public void mouseExited(MouseEvent e) {
   }
   
   
   /*****************************
      MOUSE MOTION EVENTS
   *****************************/
   
   public void mouseMoved(MouseEvent e) {
   }
   
   public void mouseDragged(MouseEvent e) {
   }
}

http://www.wehaveinternet.com

*Always driving the Learning Car of Programming*
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (143 views)
2013-05-17 21:29:12

alaslipknot (151 views)
2013-05-16 21:24:48

gouessej (182 views)
2013-05-16 00:53:38

gouessej (175 views)
2013-05-16 00:17:58

theagentd (186 views)
2013-05-15 15:01:13

theagentd (171 views)
2013-05-15 15:00:54

StreetDoggy (214 views)
2013-05-14 15:56:26

kutucuk (239 views)
2013-05-12 17:10:36

kutucuk (237 views)
2013-05-12 15:36:09

UnluckyDevil (243 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.099 seconds with 21 queries.