KeyListener Help Needed for Tetris Game!

/*

* Tetris - Java Enhacned

* By: Kunnel Zachariah, Johnathan Smith, Johnathan Adkins

*/

import javax.swing.JFrame;

import java.awt.Color;

import java.awt.Graphics;

import javax.swing.Timer;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.event.KeyListener;

import java.awt.event.KeyEvent;

importstatic java.lang.Character.*;

import java.awt.*;

import java.awt.image.*;

import java.applet.*;

import java.lang.Math;

import java.lang.System;

import java.awt.event.*;

import javax.swing.*;

import java.util.Random;

import java.awt.event.KeyAdapter;

publicclass TetrisMainextends JFrameimplements KeyListener

{

int s;

int p;

int xPos;

int yPos;

int level = 1;

int lines = 0;

int score = 0;

int switchPiece;

privateboolean[] keys;

privateint SLEEP = 50;//bigger # = slower animation

private Timer timer;

int bologna = 11;

int posX[], posY[];

Color color;

boolean isSquare =false;

public TetrisMain()

{

setSize(550,710);

setBackground(Color.black);

setVisible(true);

getContentPane();

}

publicstaticvoid main( String args[] )

{

TetrisMain pongLab =new TetrisMain();

}

publicvoid paint (Graphics g)

{

s=0;

keys =newboolean[4];

this.addKeyListener(this);

g.setColor(Color.blue);

g.fillRect(0,0,550,710);

g.setColor(Color.black);

g.fillRect(0,0,460,600);

g.setColor(Color.blue);

g.setColor (Color.black);

g.setFont(new Font("Helvetica",Font.BOLD,14));

g.drawString ("Level: " + level, 485, 40);

g.drawString ("Lines: " + lines, 485, 80);

g.drawString ("Score: " + score, 485, 120);

ActionListener paintCaller =new ActionListener(){

publicvoid actionPerformed(ActionEvent event)

{

repaint();//recalls paint every SLEEP milliseconds

}

};

timer =new Timer(SLEEP, paintCaller);

timer.start();

while (bologna <= 30)

{

if ( keys[0] ==true )

{

//move left paddle up and draw it on the window

s+=3;

}

if ( keys[1] ==true )

{

//move left paddle down and draw it on the window

p+=3;

}

if ( keys[2] ==true )

{

s-=3;

}

if ( keys[3] ==true )

{

p-=3;

}

displayRandomPiece(g);

bologna++;

}

}

publicvoid drawGridVertical(Graphics g)

{

for(xPos = 0; xPos <=460; xPos+=23)

{

for(yPos=0; yPos <=600; yPos+=23)

{

g.drawLine(xPos,yPos,xPos,yPos);

}

}

}

publicvoid drawBlock(Graphics g)

{

ImageIcon animatedIcon1 =new ImageIcon("untitled.gif");

for(p =0; p <=530; p+=2)

{

animatedIcon1.paintIcon(this,g,s,p);

delay(100000);

}

}

publicvoid drawTpiece(Graphics g)

{

ImageIcon animatedIcon =new ImageIcon("tpiece.gif");

for(p =0; p <=530; p+=2)

{

animatedIcon.paintIcon(this,g,s,p);

delay(100000);

}

}

publicvoid drawZigZagpiece(Graphics g)

{

ImageIcon animatedIcon2 =new ImageIcon("zigzagpiece.gif");

for(p =0; p <=530; p+=2)

{

animatedIcon2.paintIcon(this,g,s,p);

delay(100000);

}

}

publicvoid Rectanglepiece(Graphics g)

{

ImageIcon animatedIcon3 =new ImageIcon("rectangle.gif");

for(p =0; p <=530; p+=2)

{

animatedIcon3.paintIcon(this,g,s,p);

delay(100000);

}

}

publicvoid Unknownpiece(Graphics g)

{

ImageIcon animatedIcon4 =new ImageIcon("unknownpiece.gif");

for(p =0; p <=530; p+=2)

{

animatedIcon4.paintIcon(this,g,s,p);

delay(100000);

}

}

publicvoid otherZigZagpiece(Graphics g)

{

ImageIcon animatedIcon5 =new ImageIcon("otherzigzagpiece.gif");

for(p =0; p <=530; p+=2)

{

animatedIcon5.paintIcon(this,g,s,p);

delay(100000);

}

}

publicvoid otherUnknownpiece(Graphics g)

{

ImageIcon animatedIcon6 =new ImageIcon("otherunknownpiece.gif");

for(p =0; p <=530; p+=2)

{

animatedIcon6.paintIcon(this,g,s,p);

delay(100000);

}

}

publicstaticvoid delay(double n)

{

for (double x = 0; x <= n; x += .01);

}

publicboolean keyPressed(Event e,int key)

{

if (key == Event.LEFT)

{

s=-1;

}

elseif (key == Event.RIGHT)

{

s+=1;

}

elseif (key == Event.UP)

{

p-=1;

}

elseif (key == Event.DOWN)

{

// fast=true;

}

elseif (key == Event.ESCAPE)

{

// ingame=false;

}

returntrue;

}

publicvoid keyPressed(KeyEvent e)

{

System.out.println("keypressed");

switch(toUpperCase(e.getKeyChar()))

{

case'W' : keys[0]=true;System.out.println("W keypressed");break;

case'Z' : keys[1]=true; System.out.println("z keypressed");break;

case'I' : keys[2]=true; System.out.println("i keypressed");break;

case'M' : keys[3]=true; System.out.println("m keypressed");break;

}

}

publicvoid keyReleased(KeyEvent e)

{

System.out.println("released");

switch(toUpperCase(e.getKeyChar()))

{

case'W' : keys[0]=true; System.out.println("W realeased");break;

case'Z' : keys[1]=true; System.out.println("Z realeased");break;

case'I' : keys[2]=true; System.out.println("i realeased");break;

case'M' : keys[3]=true; System.out.println("m realeased");break;

}

}

publicvoid keyTyped(KeyEvent e)

{

//no code needed here

}

publicvoid displayRandomPiece(Graphics g)

{

Random rand =new Random();

int c = rand.nextInt((7)+1);

switch(c)

{

case 1:

drawBlock(g);

break;

case 2:

drawTpiece(g);

break;

case 3:

drawZigZagpiece(g);

break;

case 4:

Rectanglepiece(g);

break;

case 5:

Unknownpiece(g);

break;

case 6:

otherZigZagpiece(g);

break;

case 7:

otherUnknownpiece(g);

break;

}

}

}

This is my code so far for Tetris. I am having issues with my keylisteners! I keep pressing the specified keys and the println statements wont show up or the piece wont move! i really need major help. can anyone give me some advice

Much Thanks,

Chris!

[15594 byte] By [Christhefish09a] at [2007-11-27 5:10:49]
# 1
http://forum.java.sun.com/thread.jspa?threadID=5175447Don't double post! No help for you!
floundera at 2007-7-12 10:31:03 > top of Java-index,Java Essentials,New To Java...