2 Replies - 868 Views - Last Post: 30 January 2014 - 11:00 PM Rate Topic: -----

#1 Skeczi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-January 14

Binding Keys to an Applet.

Posted 30 January 2014 - 03:15 AM

New to the forums and new to coding java so my experience is quite limited.
I wanted to make a very simple game as a way to expand my knowledge of java and have found only a little success on my own.
Here's my current code:

import java.awt.event.*; // Use events
import java.awt.*; // Use graphics
import java.applet.*; // Use applets
import javax.swing.*; // Use swing stuff (GUI)
import java.util.*; // Use util
public class Tutorial extends JApplet
{
   public class DemoPanel extends JPanel
	{
		 //private int linePos = 0; // Line's X value
		 //private int lineDir = 1; // Line's direction
		 private int bx = 200; //Box x value
		 private int by = 200; //Box y value
		 private int bw = 50;  //Box width
		 private int bh = 50;  //Box height
		  
		 
		 public boolean key_right,key_left,key_up,key_down,key_z,key_x; //All this code is keyboard code.
	    public void keyTyped(KeyEvent e){}	 
		 
	    public void keyPressed(KeyEvent e)
	    {
			  if(e.getKeyCode() == e.VK_DOWN)
			     key_down = true;
			  if(e.getKeyCode() == e.VK_UP)
			     key_up = true;
			  if(e.getKeyCode() == e.VK_LEFT)
			     key_left = true;
			  if(e.getKeyCode() == e.VK_RIGHT)
			     key_right = true;
			  if(e.getKeyCode() == e.VK_Z)
			     key_z = true;
			  if(e.getKeyCode() == e.VK_X)
			     key_x = true;
	    }
		 
	    public void keyReleased(KeyEvent e)
	    {
			  if(e.getKeyCode() == e.VK_DOWN)
			     key_down = false;				  
			  if(e.getKeyCode() == e.VK_UP)
			     key_up = false;
			  if(e.getKeyCode() == e.VK_LEFT)
			     key_left = false;
			  if(e.getKeyCode() == e.VK_RIGHT)
			     key_right = false;
			  if(e.getKeyCode() == e.VK_Z)
			     key_z = false;				  
			  if(e.getKeyCode() == e.VK_X)
			     key_x = false;
				  //end of keyboard code
	    } 		 
		  	 
		 void DemoPanel()
 	 	 {
 	    	// Does nothing here
 		 }
 		 
		 public void paintComponent(Graphics page) //sort of the primary game thing? It paints the square and refreshes. 
       {
		 	 //From here to....			 
			 setFocusable(true); // Required for keyboard input          
		    GameKey game_key = new GameKey(); //Variable needed to get key states
		    addKeyListener(game_key); // Add it to your game                        
			 
			 /*if(game_key.key_z)
			 {
				bx = bx + 10;
			 }*/

			  			 	       
			 super.paintComponent(page);
		
		    page.setColor(Color.red); // Use the stock color, red
			 
			 page.fillRect(bx, by, bw, bh);	
			 
			 if(game_key.key_z)
			 {
			 	page.fillRect(50, 50, 50, 50);
			 }

			long index = 1999999999; 
			 while(index > -100000000)
			 {
			   index = index - 1;
			 }
			 		    
		    repaint();
      }
	}
	
	public void init()
	{
 		setSize(500,500);
 		getContentPane().add(new DemoPanel()); // Add the video panel
	}
}



I currently have several components that should, I think, allow me to make a very simple game.
My end goal is to create a red square which you move around a maze using the keyboard.
Making the maze, the applet, the square and the game state I've figured out.
What I'm having trouble with is binding the keys. What I want is this: After running the code, I'm using JGrasp as my compiler because it's the one I'm familiar with, the user clicks on the applet to focus on it then uses the mouse keys to move around the maze to the end.

I also haven't figured out how to make the program end or load a new maze after the user reaches the end...I'm stuck on that.

All that being said, my biggest question is how do I use this code:
  public void keyPressed(KeyEvent e)
	    {
			  if(e.getKeyCode() == e.VK_DOWN)
			     key_down = true;
			  if(e.getKeyCode() == e.VK_UP)
			     key_up = true;
			  if(e.getKeyCode() == e.VK_LEFT)
			     key_left = true;
			  if(e.getKeyCode() == e.VK_RIGHT)
			     key_right = true;
			  if(e.getKeyCode() == e.VK_Z)
			     key_z = true;
			  if(e.getKeyCode() == e.VK_X)
			     key_x = true;
	    }
		 
	    public void keyReleased(KeyEvent e)
	    {
			  if(e.getKeyCode() == e.VK_DOWN)
			     key_down = false;				  
			  if(e.getKeyCode() == e.VK_UP)
			     key_up = false;
			  if(e.getKeyCode() == e.VK_LEFT)
			     key_left = false;
			  if(e.getKeyCode() == e.VK_RIGHT)
			     key_right = false;
			  if(e.getKeyCode() == e.VK_Z)
			     key_z = false;				  
			  if(e.getKeyCode() == e.VK_X)
			     key_x = false;
				  
	    } 		 

to move the square around? I know that the keys should, depending on which key is pressed, change the value of the integers I have listed at the top..But how do I do that? I need to link the code with the graphics

Is This A Good Question/Topic? 0
  • +

Replies To: Binding Keys to an Applet.

#2 Skeczi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-January 14

Re: Binding Keys to an Applet.

Posted 30 January 2014 - 03:22 AM

So it posted without me finishing and getting all my code commented. Can't edit it yet so I'm going to have to double post. ...I need to link the code with the graphics component so that when I press a key it will change the value of an integer for a rectangle and then make the paintcomponent redraw the square in a new position. Hmm..

If what I'm asking is unclear please ask for clarification.
Was This Post Helpful? 0
  • +
  • -

#3 Skeczi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-January 14

Re: Binding Keys to an Applet.

Posted 30 January 2014 - 11:00 PM

This is thetutorial I used for keyboard input. I'm just not sure what code I need to put where in order to change the integers that control the X and Y position of the square created in the paint component.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1