Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Tetris KeyListener Problem

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Tetris KeyListener Problem

    Hi I'm currently building a Tetris game (still a work in progress), but my KeyListener won't work, I've never used a KeyListener on a program this big and am unsure if i have done something wrong. Currently the game runs however, the KeyListener does nothing. Can someone please point out my mistake? I changed the color of everything pertaining to the keyListener to red(only keyListener code is in the first 2 classes), and the smileys symbolize the end of a class (sorry couldn't find line breaks)

    import java.awt.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.applet.*;
    import java.io.File;
     
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    @SuppressWarnings("serial")
    public class Tetris extends JFrame{
     
    	GameState g = new GameState();
     
    	Screen s = new Screen();
    	private static int resX = 1024;
    	private static int resY = 768;
     
    	public static void main(String[] args)
    			{
     
    				DisplayMode dm = new DisplayMode(resX,resY,16,DisplayMode.REFRESH_RATE_UNKNOWN);
    				Tetris g = new Tetris();
    				g.run(dm);
    			}
     
    			public void run(DisplayMode dm)
    			{
    				try{
    					s.setFullScreen(dm, this);
    				}catch(Exception e){}		
    			}
     
    	public void paint(Graphics g)
    	{
    		Movement m = new Movement();
    		g.setColor(Color.WHITE);
    		g.fillRect(0, 0, resX, resY);
    			m.game(g);
    	}
     
    }
     
    :rolleyes:
     
    import java.awt.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.applet.*;
    import java.util.Random;
    public class GameState extends Applet implements KeyListener
    {
    	int xMovement;
    	int yMovement;
    	Color pieceColor = Color.BLACK;
    	Random rand = new Random();
    	public GameState()
    	{
    		[COLOR="#FF0000"]addKeyListener(this);[/COLOR]
    		xMovement = 0;
    		yMovement = 0;
    	}
     
    	public boolean grid[][] = new boolean[20][10];
     
    	public boolean gamestate = true;
    	GamePiece p;
     
    	public void setGrid(){
    		for(int r = 0;r < grid.length; ++r){	
    			for(int c = 0; c < grid[r].length; ++c){
    				grid[r][c] = false;
    			}
    		}
    	}
    	public void drawPiece(Graphics g, Cordinate[] a ,Color c)
    	{
    		for(int i = 0;i < 4;++i)
    		{
    			g.setColor(c);
    		g.fill3DRect((a[i].x*20)+300, (a[i].y*20), 20, 20,true);
    		}
    	}
    	public void erasePiece(Graphics g, Cordinate[] a)
    	{
    		for(int i = 0;i < 4;++i)
    		{
    			g.setColor(Color.WHITE);
    		g.fillRect((a[i].x*20)+ 300, (a[i].y*20), 20, 20);
    		}
    	}
    	[COLOR="#FF0000"]public void keyPressed(KeyEvent e)
    	{
    		System.out.print("pressed!!!");
    		int keyCode = e.getKeyCode();
    		if(keyCode == KeyEvent.VK_A)
    		{
     
    			xMovement += 1;
    			System.out.print("Worked!!!!");
    			e.consume();
    		}
    		else if(keyCode == KeyEvent.VK_D)
    		{
    			xMovement -= 1;
    			e.consume();
    		}
    		else if(keyCode == KeyEvent.VK_S)
    		{
    			yMovement += 1;
    			e.consume();
     
    		}
    		else if(keyCode == KeyEvent.VK_SPACE)
    		{
    			yMovement += 1;
    			e.consume();
     
    		}
    		else if(keyCode == KeyEvent.VK_ESCAPE)
    		{
    			System.out.print("DSFASDDFSDS!!!");
    			gamestate = false;
    			e.consume();
    		}
    	}
     
    	public void keyReleased(KeyEvent e)
    	{
    		System.out.print("pressed!!!");
    		e.consume();
    	}
     
    	public void keyTyped(KeyEvent e)
    		{
    		System.out.print("pressed!!!");
    			e.consume();
    		}[/COLOR]
     
    }
     
    :rolleyes:
     
     
    import java.awt.event.KeyEvent;
    import java.awt.*;
    import java.awt.event.KeyListener;
    import java.util.Random;
     
     
    public class Movement extends Thread{
    GameState s = new GameState();
    GamePiece p;
    private Cordinate beenSet[] = new Cordinate[4];
     
    	public void game(Graphics g)
    	{
    		System.out.println("Test");
     
    		while(s.gamestate)
    		{
    			switch ((new Random()).nextInt(8)+1)
    			{
    				case 1 : p = new Piece1(); break;
    				case 2 : p = new Piece2(); break;
    				case 3 : p = new Piece3(); break;
    				case 4 : p = new Piece4(); break;
    				case 5 : p = new Piece5(); break;
    				case 6 : p = new Piece6(); break;
    				case 7 : p = new Piece7();
    			}
     
    			while(p.dropping)
    			{
    				refresh();
    				s.drawPiece(g, p.cord, p.pieceColor);
    				try{
    					Thread.sleep(200);
    				}catch(Exception e){System.out.println("Thread Error");}
    				s.yMovement += 1;
    				if(p.dropping){
    					s.erasePiece(g, p.cord);
    				}
    			}
    		}
    	}
     
    	public GamePiece getPiece()
    	{
    		return p;
    	}
     
    	public void refresh()
    	{
    		int k = 0;
    		 GamePiece p = getPiece();
    		 for(int i = 0;i < 4; ++i)
    		 {
    			 p.cord[i].x += s.xMovement;
    			 p.cord[i].y += s.yMovement;
    		 }
    		 if((p.cord[0].y >= 31) || (p.cord[1].y >= 31) || (p.cord[2].y >= 31) || (p.cord[3].y >= 31))
    		 {
    			 p.dropping = false;
    		 }
    		 s.yMovement = 0;
    		 s.xMovement = 0;
    		 for(int r = 0;r < s.grid.length; ++r)
    		 {
    			 for(int c = 0; c < s.grid[r].length; ++c)
    			 {
    				 for(int i = 0;i < 4; ++i)
    				 {
    					 if((p.cord[i].x==r)&&(p.cord[i].y==c))
    					 {
    						 if((s.grid[r][c] == true)&&(r!=beenSet[0].x)&&(c!= beenSet[0].y)&&(r!=beenSet[1].x)&&(c!= beenSet[1].y)&&(r!=beenSet[2].x)&&(c!= beenSet[2].y)&&(r!=beenSet[3].x)&&(c!= beenSet[3].y)){
    							 p.dropping = false;
    						 }
    						 else if (s.grid[r][c] != true)
    						 {
    							//s.grid[r][c] = true;
    						 	//beenSet[k].x = r;
    					     	//beenSet[k].y = c;
    					     	k++;
    					     	if (k == 4)
    					     		k = 0;
    						 }
    						 else{
     
    						 }
    					 }
    				 }
    			 }
    		 }
     
    	}
    }
    :rolleyes:
     
    import java.awt.*;
     
    import javax.swing.JFrame;
     
    public class Screen{
     
     
    	private GraphicsDevice vc;
    	public Screen()
    	{
    		GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    		vc = env.getDefaultScreenDevice();
    	}
     
    	public void setFullScreen(DisplayMode dm, JFrame window)
    	{
    		window.setUndecorated(true);
    		window.setResizable(false);
    		vc.setFullScreenWindow(window);
     
    		if(dm != null && vc.isDisplayChangeSupported())
    		{
    			try{
    				vc.setDisplayMode(dm);
    			}catch(Exception e){}
    		}
    	}
     
    	public Window getFullScreenWindow()
    	{
    		return vc.getFullScreenWindow();
    	}
     
    	public void restoreScreen()
    	{
    		Window w = vc.getFullScreenWindow();
    		if(w != null)
    			w.dispose();
    		vc.setFullScreenWindow(null);
    	}
    }
     
    :rolleyes:
     
    import java.awt.Color;
     
    public abstract class GamePiece 
    {
    	protected Cordinate cord[];
    	Color pieceColor;
    	public boolean dropping = true;
    	public GamePiece()
    	{
    		this.cord = new Cordinate[4];
     
    		for(int i = 0; i < 4; ++i)
    		{
    			this.cord[i] = new Cordinate();
    		}
    	}
     
     
    }
    :rolleyes:
     
     
    public class Cordinate {
     
    	int x;
    	int y;
     
     
    	public void set(int x, int y)
    	{
    		this.x = x;
    		this.y = y;
    	}
     
    	public void swap()
    	{
    		int temp = y;
    		this.y = this.x;
    		this.x = temp;
    	}
    }
     
    :rolleyes:
     
    import java.awt.Color;
     
     
    public class Piece1 extends GamePiece 
    {
     
    	public Piece1()
    	{
    		cord[0].set(0, 0);
    		cord[1].set(0, 1);
    		cord[2].set(0, 2);
    		cord[3].set(0, 3);
    		pieceColor = Color.RED;
    	}  
     
    	public void rotate()
    	{
    		for (int i = 0; i < 4; ++i)
    		{
    			this.cord[i].swap();
    		}
    	}
    }
    :rolleyes:
    import java.awt.Color;
     
     
    public class Piece2 extends GamePiece 
    {
     
    	public Piece2()
    	{
    		cord[0].set(0, 0);
    		cord[1].set(0, 1);
    		cord[2].set(1, 0);
    		cord[3].set(1, 1);
    		pieceColor = Color.BLUE;
    	}  
     
    	public void rotate()
    	{
     
    	}
    }
     
    :rolleyes:
     
    import java.awt.Color;
     
     
    public class Piece3 extends GamePiece 
    {
     
    	public Piece3()
    	{
    		cord[0].set(0, 0);
    		cord[1].set(0, 1);
    		cord[2].set(1, 1);
    		cord[3].set(2, 1);
    		pieceColor = Color.YELLOW;
    	}  
     
    	public void rotate()
    	{
    		for (int i = 0; i < 4; ++i)
    		{
    			this.cord[i].swap();
    			if (cord[i].y == 0)
    			{
    				cord[i].y = 1;
    			}
    			else
    			{
    				cord[i].y = 0;
    			}
    		}
     
    	}
    }
     
    :rolleyes:
     
    import java.awt.Color;
     
     
    public class Piece4 extends GamePiece 
    {
    	private int state;
     
    	public Piece4()
    	{
    		cord[0].set(0, 1);
    		cord[1].set(1, 1);
    		cord[2].set(1, 0);
    		cord[3].set(2, 0);
    		pieceColor = Color.GREEN;
    		state = 0;
    	}  
     
    	public void rotate()
    	{
    		++state;
    		if (state == 0) // set to state 0
    		{
    			cord[0].set(0, 1);
    			cord[3].set(2, 0);
    		}
    		if (state == 1)  //set to state 1
    		{
    			cord[0].set(0, 0);
    			cord[3].set(1, 2);
     
    		}
     
     
    	}
    }
     
    :rolleyes:
     
    import java.awt.Color;
     
     
    public class Piece5 extends GamePiece 
    {
    	private int state;
     
    	public Piece5()
    	{
    		cord[0].set(0, 0);
    		cord[1].set(0, 1);
    		cord[2].set(0, 2);
    		cord[3].set(1, 2);
    		pieceColor = Color.ORANGE;
    		state = 0;
    	}  
     
    	public void rotate()
    	{
    		++state;
    		switch (state)
    		{
    			case 0 : for (int i = 0; i < 4; ++i)
    			{
    				cord[0].set(0, 0);
    				cord[1].set(0, 1);
    				cord[2].set(0, 2);
    				cord[3].set(1, 2);
    			}
    			case 1 : for (int i = 0; i < 4; ++i)
    			{
    				cord[0].set(0, 1);
    				cord[1].set(1, 1);
    				cord[2].set(2, 1);
    				cord[3].set(2, 0);
    			}
    			case 2 : for (int i = 0; i < 4; ++i)
    			{
    				cord[0].set(1, 2);
    				cord[1].set(1, 1);
    				cord[2].set(1, 0);
    				cord[3].set(0, 0);
    			}
    			case 3 : for (int i = 0; i < 4; ++i)
    			{
    				cord[0].set(2, 0);
    				cord[1].set(1, 0);
    				cord[2].set(0, 0);
    				cord[3].set(0, 1);
    			}
     
    		}	
    	}
    }
     
    :rolleyes:
     
    import java.awt.Color;
     
     
    public class Piece6 extends GamePiece 
    {
    	private int state;
     
    	public Piece6()
    	{
    		cord[0].set(1, 0);
    		cord[1].set(1, 1);
    		cord[2].set(1, 2);
    		cord[3].set(0, 2);
    		pieceColor = Color.CYAN;
    		state = 0;
    	}  
     
    	public void rotate()
    	{
    		++state;
    		switch (state)
    		{
    			case 0 : for (int i = 0; i < 4; ++i)
    			{
    				cord[0].set(1, 0);
    				cord[1].set(1, 1);
    				cord[2].set(1, 2);
    				cord[3].set(0, 2);
    			}
    			case 1 : for (int i = 0; i < 4; ++i)
    			{
    				cord[0].set(0, 0);
    				cord[1].set(1, 0);
    				cord[2].set(2, 0);
    				cord[3].set(2, 1);
    			}
    			case 2 : for (int i = 0; i < 4; ++i)
    			{
    				cord[0].set(0, 2);
    				cord[1].set(0, 1);
    				cord[2].set(0, 0);
    				cord[3].set(1, 0);
    			}
    			case 3 : for (int i = 0; i < 4; ++i)
    			{
    				cord[0].set(2, 1);
    				cord[1].set(1, 1);
    				cord[2].set(0, 1);
    				cord[3].set(0, 0);
    			}
     
    		}	
    	}
    }
     
     
    import java.awt.Color;
     
     
    public class Piece7 extends GamePiece 
    {
    	private int state;
     
    	public Piece7()
    	{
    		cord[0].set(0, 1);
    		cord[1].set(1, 0);
    		cord[2].set(1, 1);
    		cord[3].set(2, 1);
    		pieceColor = Color.PINK;
    		state = 0;
    	}  
     
    	public void rotate()
    	{
    		++state;
    		switch (state)
    		{
    			case 0 : for (int i = 0; i < 4; ++i)
    			{
    				cord[0].set(0, 1);
    				cord[1].set(1, 0);
    				cord[2].set(1, 1);
    				cord[3].set(2, 1);
    			}
    			case 1 : for (int i = 0; i < 4; ++i)
    			{
    				cord[0].set(1, 2);
    				cord[1].set(0, 1);
    				cord[2].set(1, 1);
    				cord[3].set(1, 0);
    			}
    			case 2 : for (int i = 0; i < 4; ++i)
    			{
    				cord[0].set(2, 0);
    				cord[1].set(1, 1);
    				cord[2].set(1, 0);
    				cord[3].set(0, 0);
    			}
    			case 3 : for (int i = 0; i < 4; ++i)
    			{
    				cord[0].set(0, 0);
    				cord[1].set(1, 1);
    				cord[2].set(0, 1);
    				cord[3].set(0, 2);
    			}
     
    		}	
    	}
    }
    Last edited by JavaPF; November 8th, 2011 at 06:07 AM. Reason: USE HIGHLIGHT TAGS!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: Tetris KeyListener Problem

    First, please use the code highlight tags (see the link in my signature entitled getting help). Second, that is way too much code for us to go through - I highly recommend creating an SSCCE that demonstrates the problem. All that being said, a common problem for KeyListeners is that the component must have focus for them to fire. Thus I recommend either calling requestFocus on the component, or use KeyBindings instead.
    How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

  3. The Following User Says Thank You to copeg For This Useful Post:

    Martinnikol (November 5th, 2011)

Similar Threads

  1. KeyListener bug
    By nivangerow in forum AWT / Java Swing
    Replies: 1
    Last Post: September 5th, 2011, 07:10 AM
  2. [SOLVED] KeyListener won't listen
    By Fermen in forum AWT / Java Swing
    Replies: 8
    Last Post: June 22nd, 2011, 09:11 PM
  3. help with KeyListener
    By all_pro in forum AWT / Java Swing
    Replies: 4
    Last Post: April 14th, 2011, 07:51 AM
  4. keylistener small problem
    By matecno in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 14th, 2010, 08:51 PM
  5. Problem with KeyListener
    By r12ki in forum AWT / Java Swing
    Replies: 3
    Last Post: October 1st, 2009, 02:18 PM

Tags for this Thread