Need KeyListener Help on Tetris Program (small help)

/*

* Tetris - Java Enhacned

* By: Christopher

* AP Computer Science Final Project

*/

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;

}

}

}

Okay their is my code,

im currently having issues with my keylistener. Everytime i try to tap or press a key the tetris piece will not move or do anything. I tried putting print line statements but those println statements DO NOT show up! Im really confused on whats wrong and i thought someone can help me! im in really bad need!

please help me asap

Much Thanks Chris!

[15721 byte] By [Christhefish09a] at [2007-11-27 5:10:47]
# 1
you know what they say about system.out.printlns that dont show up, right? theyre lonely.
TuringPesta at 2007-7-12 10:31:01 > top of Java-index,Java Essentials,Java Programming...
# 2
what does that mean?
Christhefish09a at 2007-7-12 10:31:01 > top of Java-index,Java Essentials,Java Programming...
# 3
this.addKeyListener(this);Why the hell do you have this in your paint method?
floundera at 2007-7-12 10:31:01 > top of Java-index,Java Essentials,Java Programming...
# 4
> what does that mean?my charming word play is woefully underappreciated.IT MEANS ADD MORE!
TuringPesta at 2007-7-12 10:31:01 > top of Java-index,Java Essentials,Java Programming...
# 5
i really dont knowwhere should it be placed..PS: I asked my computer science teacher to help me on this problem and she obviously doesnt know that much on programming...
Christhefish09a at 2007-7-12 10:31:01 > top of Java-index,Java Essentials,Java Programming...
# 6
> Why the hell do you have this in your paint method?Ill see your "this.addKeyListener()" and raise you a "getContentPane()".
TuringPesta at 2007-7-12 10:31:01 > top of Java-index,Java Essentials,Java Programming...
# 7
Another thing, in your keyReleased method shouldn't you be setting the values to false?
floundera at 2007-7-12 10:31:01 > top of Java-index,Java Essentials,Java Programming...
# 8

/*

* Tetris - Java Enhacned

*

*/

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;

import static 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;

public class TetrisMain extends JFrame implements KeyListener

{

int s;

int p;

int xPos;

int yPos;

int level = 1;

int lines = 0;

int score = 0;

int switchPiece;

private boolean[] keys;

private int 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();

}

public static void main( String args[] )

{

TetrisMain pongLab = new TetrisMain();

}

public void paint (Graphics g)

{

s=0;

keys = new boolean[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(){

public void 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++;

}

}

public void drawGridVertical(Graphics g)

{

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

{

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

{

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

}

}

}

public void drawBlock(Graphics g)

{

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

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

{

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

delay(100000);

}

}

public void drawTpiece(Graphics g)

{

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

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

{

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

delay(100000);

}

}

public void drawZigZagpiece(Graphics g)

{

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

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

{

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

delay(100000);

}

}

public void Rectanglepiece(Graphics g)

{

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

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

{

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

delay(100000);

}

}

public void Unknownpiece(Graphics g)

{

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

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

{

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

delay(100000);

}

}

public void otherZigZagpiece(Graphics g)

{

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

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

{

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

delay(100000);

}

}

public void otherUnknownpiece(Graphics g)

{

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

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

{

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

delay(100000);

}

}

public static void delay(double n)

{

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

}

public boolean keyPressed(Event e, int key)

{

System.out.println("m keypressed");

if (key == Event.LEFT)

{

s=-1;

System.out.println("m keypressed");

}

else if (key == Event.RIGHT)

{

s+=1;

System.out.println("m keypressed");

}

else if (key == Event.UP)

{

p-=1;

System.out.println("m keypressed");

}

else if (key == Event.DOWN)

{

// fast=true;

}

else if (key == Event.ESCAPE)

{

// ingame=false;

}

return true;

}

public void 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;

}

}

public void keyReleased(KeyEvent e)

{

System.out.println("released");

switch(toUpperCase(e.getKeyChar()))

{

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

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

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

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

}

}

public void keyTyped(KeyEvent e)

{

//no code needed here

}

public void 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;

}

}

}

^thats my orignal code.

i did some testing on it to see where my keylistener is going wrong.

and i still have no idea where exactly the this.addKeyListener(this);

goes...

Message was edited by:

Christhefish09

Christhefish09a at 2007-7-12 10:31:01 > top of Java-index,Java Essentials,Java Programming...
# 9

Swing related questions should be posted in the Swing forum.

KeyEvents are only passed to the component that has focus. So you are probably adding the listener to the wrong component.

However, you should be using a KeyListener anyways. You should be using Key Bindings. Then you can handle the KeyStroke even is the component doesn't have direct focus.

Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html]How to Use Key Bindings[/url] for more information.

camickra at 2007-7-12 10:31:01 > top of Java-index,Java Essentials,Java Programming...
# 10

> i really dont know

> where should it be placed..

> PS: I asked my computer science teacher to help me on

> this problem and she obviously doesnt know that much

> on programming...

Ok my good friend.

You sound sufficiently pathetic, lol (i mean that in a funny way not in

a mean way). Im sorry about your teacher.

Anyway. Here are a couple things.

Override paintComponent() and not paint().

Maybe you can have a TetrisShape class and have each shape

do its own drawing (rather than making methods).

Or, if you think about it each TetrisShape is just a 4 block figure

that can be expressed in a general way. Like a 4x4 raster?

This will allow you to have one paint method that can paint

any configuration passed to it.

Also, this is my opinion only, but I try to avoid static imports.

They were added in J5 and definitely not warmly by me.

TuringPesta at 2007-7-12 10:31:01 > top of Java-index,Java Essentials,Java Programming...
# 11
ok so i have to use keybindings instead...if this doesnt workwhats my next ultimatum
Christhefish09a at 2007-7-12 10:31:01 > top of Java-index,Java Essentials,Java Programming...
# 12
you dont HAVE to use keybindings.try following a more modular or piecewise programming process.its hard to fix your code for you with all that extra stuff mixed in.
TuringPesta at 2007-7-12 10:31:01 > top of Java-index,Java Essentials,Java Programming...
# 13

Still really confused...

i tried every possible way

but still no results...

/*

* Tetris - Java Enhacned

*/

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;

import static java.lang.Character.*;

import java.awt.*;

import java.awt.image.*;

import java.applet.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.Random;

import java.awt.event.KeyAdapter;

public class TetrisMain extends JFrame implements KeyListener

{

int s;

int p;

int xPos;

int yPos;

int level = 1;

int lines = 0;

int score = 0;

int switchPiece;

private boolean[] keys;

private Timer timer;

int bologna = 11;

Color color;

public TetrisMain()

{

setSize(550,710);

setBackground(Color.black);

setVisible(true);

getContentPane();

s=0;

keys = new boolean[4];

addKeyListener(this);

}

public void paint (Graphics g)

{

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, 50);

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

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

while (bologna <= 30)

{

if ( keys[0] == true ){s+=3;}

if ( keys[1] == true ){p+=3;}

if ( keys[2] == true ){s-=3;}

if ( keys[3] == true ){p-=3;}

displayRandomPiece(g,s,p);

bologna++;

}

}

public void drawGridVertical(Graphics g)

{

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

{

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

{

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

}

}

}

public void drawBlock(Graphics g, int s, int p)

{

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

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

{

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

delay(100000);

}

}

public void drawTpiece(Graphics g,int s, int p)

{

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

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

{

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

delay(100000);

}

}

public void drawZigZagpiece(Graphics g,int s, int p)

{

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

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

{

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

delay(100000);

}

}

public void Rectanglepiece(Graphics g,int s, int p)

{

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

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

{

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

delay(100000);

}

}

public void Unknownpiece(Graphics g,int s, int p)

{

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

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

{

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

delay(100000);

}

}

public void otherZigZagpiece(Graphics g,int s, int p)

{

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

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

{

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

delay(100000);

}

}

public void otherUnknownpiece(Graphics g,int s, int p)

{

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

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

{

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

delay(100000);

}

}

public static void delay(double n)

{

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

}

public void keyPressed(KeyEvent e)

{

System.out.println("keypressed");

switch(toUpperCase(e.getKeyChar()))

{

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

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

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

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

}

}

public void keyReleased(KeyEvent e)

{

System.out.println("released");

switch(toUpperCase(e.getKeyChar()))

{

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

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

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

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

}

}

public void keyTyped(KeyEvent e)

{

//no code needed here

}

public void displayRandomPiece(Graphics g,int s, int p)

{

Random rand = new Random();

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

switch(c)

{

case 1:

drawBlock(g,s,p);

break;

case 2:

drawTpiece(g,s,p);

break;

case 3:

drawZigZagpiece(g,s,p);

break;

case 4:

Rectanglepiece(g,s,p);

break;

case 5:

Unknownpiece(g,s,p);

break;

case 6:

otherZigZagpiece(g,s,p);

break;

case 7:

otherUnknownpiece(g,s,p);

break;

}

}

public static void main( String args[] )

{

TetrisMain pongLab = new TetrisMain();

}

}

anyone can help me still

Message was edited by:

Christhefish09

Christhefish09a at 2007-7-12 10:31:01 > top of Java-index,Java Essentials,Java Programming...
# 14

You don't override the paint() method of JFrame.

You create a custom component for custom painting. So you would either extend JComponent or JPanel and then override the paintComponent() method (as was suggested above) for the custom painting. Then you make sure the component is focusable and you add the KeyListener to the component, not the frame. Finally you add the component to the content pane of the frame.

So start by writing a 10 line program the adds a custom component (without the custom painting) to the frame. Then you test your KeyListener code. Once you get that working you build on the simple program.

camickra at 2007-7-12 10:31:01 > top of Java-index,Java Essentials,Java Programming...