11 Replies - 997 Views - Last Post: 06 March 2012 - 01:49 PM Rate Topic: -----

#1 zehawk  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 213
  • Joined: 23-February 12

Having some KeyCode problems...

Posted 03 March 2012 - 10:51 PM

Ok, this sounds kinda weird, but my KeyListener class seems to work only like, half the time whereas my mouse events work 100% of the time. The problem is only at the beginning, but whenever I run the java program, the program will only work if i havent clicked on something else before the whole thing loads... Heres the code:

Frame class:
import javax.swing.JFrame;
public class Frame {
	public static void main(String args[]){
		JFrame frame = new JFrame("Ferraria");
		frame.add(new Board());
		frame.setSize(1200, 600);
		frame.setVisible(true);
		frame.setResizable(false);
		frame.setLocation(200, 200);
		frame.setDefaultCloseOperation(3);
	}
}


Board class:
public Board(){
		addKeyListener(new AL());
		setFocusable(true);
		addMouseListener(new ML());
		setFocusable(true);
		addMouseWheelListener(new ML());
		setFocusable(true);
}
private class AL extends KeyAdapter{
		public void keyPressed(KeyEvent e){

			int key = e.getKeyCode();
			
			if(key == KeyEvent.VK_A){
				Player.setChangeX(20);
			}
			else if(key == KeyEvent.VK_D){
				Player.setChangeX(-20);
			}
			
			if(key == KeyEvent.VK_1){
				Player.setCurrentType(1);
			}else if(key == KeyEvent.VK_2){
				Player.setCurrentType(2);
			}else if(key == KeyEvent.VK_3){
				Player.setCurrentType(3);
			}else if(key == KeyEvent.VK_4){
				Player.setCurrentType(4);
			}else if(key == KeyEvent.VK_5){
				Player.setCurrentType(5);
			}else if(key == KeyEvent.VK_6){
				Player.setCurrentType(6);
			}else if(key == KeyEvent.VK_7){
				Player.setCurrentType(7);
			}else if(key == KeyEvent.VK_8){
				Player.setCurrentType(8);
			}else if(key == KeyEvent.VK_9){
				Player.setCurrentType(9);
			}
			
			if(key == KeyEvent.VK_S){
				Player.setSpawn();
			}else if(key == KeyEvent.VK_R){
				Player.respawn();
			}
			
			if(!Player.getAirborne() && key == KeyEvent.VK_SPACE){
				Player.setUp(true);
				Player.setAirborne(true);
				Player.setChangeY(4);
			}
		}
		public void keyReleased(KeyEvent e){
			int key = e.getKeyCode();
			if(key == KeyEvent.VK_A){
				Player.setChangeX(0);
			}
			else if(key == KeyEvent.VK_D){
				Player.setChangeX(0);
			}
		}
	}
	
	private class ML extends MouseAdapter{
		public void mouseClicked(MouseEvent e){
			int mouse = e.getButton();
			boolean collisions = SpecialTypes.placeBlock(Player.getCurrentType());

			try{
				if(mouse == MouseEvent.BUTTON1){
					MouseEvents.clicked(false, false, e, 0);
				}
				if(mouse == MouseEvent.BUTTON3){
					MouseEvents.clicked(true, collisions, e, Player.getCurrentType());
				}
			}catch(Exception e1){}
		}
		
		public void mouseWheelMoved(MouseWheelEvent e){
			Player.setCurrentType(e.getScrollAmount() / 3 + Player.getCurrentType());
			if(Player.getCurrentType() >= 10){
				Player.setCurrentType(1);
			}
		}
		
		
	}


Anyone know whats going on? Its only the private AL class that sometimes registers, the mouse class works fine always!

Is This A Good Question/Topic? 0
  • +

Replies To: Having some KeyCode problems...

#2 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Having some KeyCode problems...

Posted 04 March 2012 - 02:35 AM

Put this line in your keyPressed method:
System.out.println("The key pressed was: " + e.getKeyCode());
That will tell you for sure if the key accounted for each time it was pressed, or if your methods are just not updating properly. Maybe you forgot to repaint the screen? We can't tell with this code. What is your board class? A JPanel?

I have a feeling this has something to do with focus. Why do you setFocusable(true) three times back to back?
Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1399
  • View blog
  • Posts: 3,101
  • Joined: 05-April 11

Re: Having some KeyCode problems...

Posted 04 March 2012 - 05:15 AM

You dont have to set focusable to true three times in a row ^^

Try and change your main into the following
import javax.swing.JFrame;
public class Frame {
	public static void main(String args[]){
		Board board = new Board();
		
		JFrame frame = new JFrame("Ferraria");
		frame.add(board);
		frame.setFocusable(false);
		frame.setSize(1200, 600);
		frame.setVisible(true);
		frame.setResizable(false);
		frame.setLocation(200, 200);
		frame.setDefaultCloseOperation(3);
		
		board.requestFocus();
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 zehawk  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 213
  • Joined: 23-February 12

Re: Having some KeyCode problems...

Posted 04 March 2012 - 07:25 AM

Yes, board is a jpanel and repaint is constantly called. However, repaint works fully: If you want the whole board class here you go:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

@SuppressWarnings("serial")
public class Board extends JPanel implements ActionListener{

	
	Timer time;

	static Block[][] blocks;
	static Graphics2D Drawer;
	public Board(){
		addKeyListener(new AL());
		setFocusable(true);
		addMouseListener(new ML());
		setFocusable(true);
		addMouseWheelListener(new ML());
		setFocusable(true);
		blocks = new Block[5000][5000];
		time = new Timer(10, this);
		time.start();
		WorldGen.Generate();
	}
	
	public void actionPerformed(ActionEvent e) {
		repaint();
		UpdateWorld.Update();
	}
	
	public void paint(Graphics g){
		super.paint(g);
		Drawer = (Graphics2D) g;
		
		drawBackground();
		drawBlocks();
		drawPlayer();
		drawShaders();
		drawHUD();
		drawStats();
	}

	private void drawBackground(){
		Drawer.drawImage(WorldBackground.getBackground(), 0 , 0, null);
	}
	
	private void drawPlayer(){
		Drawer.drawImage(Player.getImage(), 600, 320, null);
	}
	
	private void drawBlocks(){
		for(int getX = Player.getBlockX() - 1; getX != Player.getBlockX() + 61; getX++){
			for(int getY = Player.getBlockY() - 1; getY != Player.getBlockY() + 31; getY++){
				try{
				if(blocks[getX][getY].getVisible() && blocks[getX][getY].getLighting() < 13){
					Drawer.drawImage(blocks[getX][getY].getImage(), blocks[getX][getY].getX() - Player.getX(), blocks[getX][getY].getY() - Player.getY(), null);
				}
				}catch(Exception e){
					
				}
			}
		}
	}
	
	private void drawShaders() {
		for(int getX = Player.getBlockX() - 1; getX != Player.getBlockX() + 61; getX++){
			for(int getY = Player.getBlockY() - 1; getY != Player.getBlockY() + 31; getY++){
				try{
					if(blocks[getX][getY].getLighting() < 13){
						Drawer.drawImage(blocks[getX][getY].getLightingImage(), blocks[getX][getY].getX() - Player.getX(), blocks[getX][getY].getY() - Player.getY(), null);
					}
					else if(blocks[getX][getY].getLighting() >= 13){
						Drawer.drawImage(blocks[getX][getY].getImage(), blocks[getX][getY].getX() - Player.getX(), blocks[getX][getY].getY() - Player.getY(), null);
					}
				}catch(Exception e){}
			}
		}
	}
	
	private void drawStats(){
		Drawer.drawString(Player.getPlayerX()  + " " + Player.getPlayerY(), 10, 20);
		Drawer.drawString("Walk: A/D", 10, 40);
		Drawer.drawString("Jump: SPACE", 10, 60);
		Drawer.drawString("Set Spawn: S", 10, 80);
		Drawer.drawString("Set Spawn: R", 10, 100);
	}
	
	private void drawHUD(){
		for(int i = 0; i != 9; i++){
			Drawer.drawImage(GUISelection.getSelected(), 110 + (i * 40), 30, null);
		}
		
		Drawer.drawImage(GUISelection.getSelection(), 75 + (Player.getCurrentType() * 40), 35, null);
		Drawer.drawImage(BlockDirt.getImage(), 120, 40, null);
		Drawer.drawImage(BlockDirtGrass.getImage(), 160, 40, null);
		Drawer.drawImage(BlockStone.getImage(), 200, 40, null);
		Drawer.drawImage(BlockPeat.getImage(), 240, 40, null);
		Drawer.drawImage(BlockLava.getImage(), 280, 40, null);
		Drawer.drawImage(BlockWoodPlanks.getImage(), 320, 40, null);
		Drawer.drawImage(BlockWoodTotem.getImage(), 360, 40, null);
	}
	
	
	private class AL extends KeyAdapter{
		public void keyPressed(KeyEvent e){

			int key = e.getKeyCode();
			
			if(key == KeyEvent.VK_A){
				Player.setChangeX(2);
			}
			else if(key == KeyEvent.VK_D){
				Player.setChangeX(-2);
			}
			
			if(key == KeyEvent.VK_1){
				Player.setCurrentType(1);
			}else if(key == KeyEvent.VK_2){
				Player.setCurrentType(2);
			}else if(key == KeyEvent.VK_3){
				Player.setCurrentType(3);
			}else if(key == KeyEvent.VK_4){
				Player.setCurrentType(4);
			}else if(key == KeyEvent.VK_5){
				Player.setCurrentType(5);
			}else if(key == KeyEvent.VK_6){
				Player.setCurrentType(6);
			}else if(key == KeyEvent.VK_7){
				Player.setCurrentType(7);
			}else if(key == KeyEvent.VK_8){
				Player.setCurrentType(8);
			}else if(key == KeyEvent.VK_9){
				Player.setCurrentType(9);
			}
			
			if(key == KeyEvent.VK_S){
				Player.setSpawn();
			}else if(key == KeyEvent.VK_R){
				Player.respawn();
			}
			
			if(!Player.getAirborne() && key == KeyEvent.VK_SPACE){
				Player.setUp(true);
				Player.setAirborne(true);
				Player.setChangeY(4);
			}
		}
		public void keyReleased(KeyEvent e){
			int key = e.getKeyCode();
			if(key == KeyEvent.VK_A){
				Player.setChangeX(0);
			}
			else if(key == KeyEvent.VK_D){
				Player.setChangeX(0);
			}
		}
	}
	
	private class ML extends MouseAdapter{
		public void mouseClicked(MouseEvent e){
			int mouse = e.getButton();
			boolean collisions = SpecialTypes.placeBlock(Player.getCurrentType());

			try{
				if(mouse == MouseEvent.BUTTON1){
					MouseEvents.clicked(false, false, e, 0);
				}
				if(mouse == MouseEvent.BUTTON3){
					MouseEvents.clicked(true, collisions, e, Player.getCurrentType());
				}
			}catch(Exception e1){}
		}
		
		public void mouseWheelMoved(MouseWheelEvent e){
			Player.setCurrentType(e.getScrollAmount() / 3 + Player.getCurrentType());
			if(Player.getCurrentType() >= 10){
				Player.setCurrentType(1);
			}
		}
		
		
	}
	
	
}




I tried both of your solutions, they both dont work. Well,. actually only the second guy's solution didnt work, but i used the System.out.println method mentioned and it turns out the keycode doesnt register at all sometimes! I'm getting rather mad, and can't seem to find the solutoin!>:(

This post has been edited by zehawk: 04 March 2012 - 07:33 AM

Was This Post Helpful? 0
  • +
  • -

#5 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Having some KeyCode problems...

Posted 04 March 2012 - 04:10 PM

This is all part of diagnosing and debugging the program. You are going to have to learn some patience if you want to get far in programming. Since you took my suggestion, we know that the keysevents aren't even being caught, so now let's try some other things.

Maybe your JPanel doesn't have focus, so do a requestFocus() call in the constructor of your Board.

You may also want to check out GregBrannon's tutorial on keybinding.

This post has been edited by Sheph: 04 March 2012 - 04:18 PM

Was This Post Helpful? 2
  • +
  • -

#6 zehawk  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 213
  • Joined: 23-February 12

Re: Having some KeyCode problems...

Posted 04 March 2012 - 04:41 PM

Ive got patience, I'm just new to swing and jpanels so I know a very small amount about this all...
Was This Post Helpful? 0
  • +
  • -

#7 zehawk  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 213
  • Joined: 23-February 12

Re: Having some KeyCode problems...

Posted 05 March 2012 - 11:43 AM

Still doesnt work...
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Having some KeyCode problems...

Posted 05 March 2012 - 11:59 AM

time = new Timer(10, this);

calling repaint() every 10 milli is absolutly useless
The hardware repaint the screen every 16.666 milli so you are wasting CPU cycle for nothing and that may produce some flash into the screen
Set tyour timer to 20 milli to start with
Was This Post Helpful? 0
  • +
  • -

#9 zehawk  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 213
  • Joined: 23-February 12

Re: Having some KeyCode problems...

Posted 05 March 2012 - 12:06 PM

I did not know that, i was told the user had to personally use repaint every time they wanted it to repaint. ok got it. Next?
Was This Post Helpful? 0
  • +
  • -

#10 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Having some KeyCode problems...

Posted 05 March 2012 - 05:11 PM

Try adding the KeyListener to the JFrame instead. This worked for me once when I had a similar problem, but it's not really a solution as a workaround. I think rewriting your code to GregBrannon's approach would be a good solution.
Was This Post Helpful? 0
  • +
  • -

#11 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Having some KeyCode problems...

Posted 05 March 2012 - 09:02 PM

Quote

To simplify keyboard focus, the controller implements KeyListener on behalf of the view. *

That's a quote from the javadoc of a game called RoboChase, whose javadoc can be found here. It appears I'm not the only one who choose to have the JFrame implement KeyListener. ;) "To simplify keyboard focus" probably means they didn't know how to fix it either... However, you should also check out the way that they do key bindings. I think that could help you a lot. Keep up the good work!
Was This Post Helpful? 0
  • +
  • -

#12 zehawk  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 213
  • Joined: 23-February 12

Re: Having some KeyCode problems...

Posted 06 March 2012 - 01:49 PM

Hey thanks sheph! more importantly, thanks for checking out my youtube channel!:D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1