Home arrow static arrow Java Programming [Archive] - Key Listener Implementation Problem
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Key Listener Implementation Problem
This topic has 1 reply on 1 page.

Posts:112
Registered: 10/8/03
Key Listener Implementation Problem  
Aug 9, 2004 9:31 AM



 
All right guys, this is an easy one... but the answer has eluded me so far. I used to have this set up with mouseListeners, but the collision checking is getting on my nerves. So I decided to attempt to change it over to keyListeners but with little success. I am not getting any input from the keyboard in my application. Lets see if you guys have better luck than I.
Thanks,
Ryltar

//This starts out in a JFrame called frameworldTravelPanel = new worldTravelPanel();worldTravelPanel.setLayout(null);worldTravelPanel.setBounds(0, 0, programWidth, programHeight);worldTravelPanel.addKeyListener(worldTravelPanel);worldTravelPanel.setFocusable(true);frame.getContentPane().add(worldTravelPanel); worldTravelPanel.repaint();worldTravelPanel.setVisible(true); //Here is the JPanel code... At the end is my key listener methodsprivate class worldTravelPanel extends JPanel//Contains all the code for world creation and transportationimplements /*MouseListener, */KeyListener{	mapGround mapGround = new mapGround();	mapWall mapWall = new mapWall();	mapRoof mapRoof = new mapRoof();	mapDoor mapDoor = new mapDoor();	mapNPC mapNPC = new mapNPC(); 	Point2D start, end; 	double x, y, animationSet; 	Timer timer = new Timer(); 	boolean NPCCollision = falsepublic worldTravelPanel()	{		if(hero.map == null)		{			mapGround.loadTiles("olatheMap");			mapWall.loadTiles("olatheMap");			mapDoor.loadTiles("olatheMap");			mapNPC.loadTiles("olatheMap");			mapRoof.loadTiles("olatheMap"); 			x = programWidth / 2;			y = programHeight / 2;		}		else		{			mapGround.loadTiles(hero.map);			mapWall.loadTiles(hero.map);			mapDoor.loadTiles(hero.map);			mapNPC.loadTiles(hero.map);			mapRoof.loadTiles(hero.map); 			x = hero.location[0][0];			y = hero.location[0][1];		} 		animationSet = 7.0; 	} 	class moveTask extends TimerTask	{		Point2D start = new Point2D.Double(hero.location[0][0], hero.location[0][1]);;		Point2D end = new Point2D.Double(hero.location[1][0], hero.location[1][1]);		double distance = start.distance(end); 		public void run()		{			if(((Math.abs(start.getX() - end.getX()) < 0.5) && (Math.abs(start.getY() - end.getY()) < 0.5)) && start != end)			{				start = end;			}			else if (start != end)			{				double distance = start.distance(end);				x = (end.getX() - start.getX()) / distance;				y = (end.getY() - start.getY()) / distance;				//System.out.println("X1: " + x + "  Y1: " + y);				hero.location[1][0] = (int)(start.getX() + x);				hero.location[1][1] = (int)(start.getY() + y);				//System.out.println("X2: " + hero.location[1][0] + "  Y2: " + hero.location[1][1]);				//System.out.println("Coord X: " + (int)(hero.location[1][0]/32) + "  Coord Y: " + (int)(hero.location[1][1]/32)); 				checkForCollision(); 				repaint();			}			else			{				hero.location[0][0] = (int)start.getX();				hero.location[0][1] = (int)start.getY();				timer.cancel();			}		} 		public void checkForCollision()		{			int tempX = (int)(hero.location[1][0] / 32);			int tempY = (int)(hero.location[1][1] / 32); 			//System.out.println("X: " + tempX + "  Y: " + tempY); 			boolean wallCollision = false/*				if(end.getX() > start.getX())			 {			 if(mapWall.currentWallMap[tempY][tempX + 1] != 00)			 {			 wallCollision = true;			 }			 }			 else if(end.getX() < start.getX())			 {			 if(tempX <= 1)			 {			 tempX = 1;			 }			 if(mapWall.currentWallMap[tempY][tempX - 1] != 00)			 {			 wallCollision = true;			 }			 }			 if(end.getY() < start.getY())			 {			 if(mapWall.currentWallMap[tempY + 1][tempX] != 00)			 {			 wallCollision = true;			 }			 }			 else if(end.getY() > start.getY())			 {			 if(tempY <= 1)			 {			 tempY = 1;			 }			 if(mapWall.currentWallMap[tempY - 1][tempX] != 00)			 {			 wallCollision = true;			 }			 }*/ 			if(wallCollision == true)			{				/*				 System.out.println("Wall Collision: " + tempY + ", " + tempX);				 end.setLocation(hero.location[1][0], hero.location[1][1]);				 start.setLocation(hero.location[1][0], hero.location[1][1]);\				 */				start.setLocation(start.getX() + x, start.getY() + y);				x = start.getX();				y = start.getY();			}			else if(mapDoor.currentDoorMap[tempY][tempX] != 0)			{				frame.setTitle("Loading...");				hero = mapDoor.doorCollision(hero);				changeMap();				frame.setTitle(currName + " " + currVers);			}			else if(mapNPC.currentNPCMap[tempY][tempX] != 0)			{				NPCCollision = mapNPC.NPCCollision(hero, NPCCollision);				start.setLocation(start.getX() + x, start.getY() + y);				x = start.getX();				y = start.getY();			}			else			{				start.setLocation(start.getX() + x, start.getY() + y);				x = start.getX();				y = start.getY();			}		} 		public void changeMap()		{			mapGround.loadTiles(hero.map);			mapWall.loadTiles(hero.map);			mapDoor.loadTiles(hero.map);			mapNPC.loadTiles(hero.map);			mapRoof.loadTiles(hero.map); 			x = hero.location[0][0];			y = hero.location[0][1];			start.setLocation(hero.location[0][0], hero.location[0][1]);			end.setLocation(hero.location[0][0], hero.location[0][1]);		}	} 	protected void paintComponent(Graphics g)	{		g.clipRect(0, 0, programWidth, programHeight);		g.setColor(Color.white);		g.fillRect(0, 0, programWidth, programHeight);		mapGround.drawGround(g);//Floor Layer		mapWall.drawWall(g);//Wall Layer		mapDoor.drawDoor(g);//Interaction Layer 1		mapNPC.drawNPC(g);//Interaction Layer 2		charHero[(int)animationSet].paintIcon(this, g, (int)x, (int)y);		mapRoof.drawRoof(g);//Roof Layer 		if(NPCCollision != false)		{			BufferedImage temp = (BufferedImage)createImage(640, 100);			Graphics2D gra = temp.createGraphics();			gra.setColor(Color.white);			gra.fillRect(0, 0, 640, 100);			gra.dispose(); 			NPCCollision = mapNPC.NPCTalkDraw(g, temp, NPCCollision);		}	} 	public void keyPressed(KeyEvent e)	{		int keyPressed = e.getKeyCode();		System.out.println(keyPressed);	}	public void keyReleased(KeyEvent e)	{		int keyPressed = e.getKeyCode();		System.out.println(keyPressed);	}	public void keyTyped(KeyEvent e)	{		int keyPressed = e.getKeyCode();		System.out.println(keyPressed);	}}
 

Posts:112
Registered: 10/8/03
Re: Key Listener Implementation Problem  
Aug 9, 2004 11:00 AM (reply 1 of 1)



 
I figured out what to do... stupid me forgot to call for focus...
 
This topic has 1 reply on 1 page.