How can i use UP and DOWN key?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: How can i use UP and DOWN key?

Hybrid View

  1. #1
    Join Date
    Dec 2006
    Posts
    10

    How can i use UP and DOWN key?

    Can i have any help please.. hmm is hard for me to move the ball through y axis. it seems all movements of my keys are in x axis.


    heres the code for my applet:

    Code:
    import java.applet.*;
    import java.awt.*;
    
    public class snake extends Applet implements Runnable
    {
    
    	int x_pos = 30;			// x - Position des Balles
    	int y_pos = 30;		// y - Position des Balles
    	int x_speed = 1;
    		int y_speed = 2;
    	int radius = 20;		// Radius des Balles
    	int appletsize_x = 300; // Größe des Applets in x - Richtung
    	int appletsize_y = 300;	// Größe des Applets in y - Richtung
    
    
    	private Image dbImage;
    	private Graphics dbg;
    
    	public void init()
    	{
    		setBackground (Color.blue);
    	}
    
    	public void start ()
    	{
    		
    		Thread th = new Thread (this);
    		// Starten des Threads
    		th.start ();
    	}
    
    	public void stop()
    	{
    
    	}
    
    	public void destroy()
    	{
    
    	}
    
    
    	public boolean keyDown (Event e, int key)
    	{
    		
    		if (key == Event.LEFT)
    		{
    			
    			x_speed = -1;
    		}
    	
    		else if (key == Event.RIGHT)
    		{
    		
    			x_speed = 1;
    		}
    			if (key == Event.DOWN)
    		{
    			
    			y_speed = -5;
    		}
    	
    		else if (key == Event.UP)
    		{
    		
    			y_speed = 5;
    		}
    	
    		else if (key == 32)
    		{
    			x_speed = 0;
    		}
    		else
    		{
    		
    			System.out.println ("Charakter: " + (char)key + " Integer Value: " + key);
    		}
    
    		return true;
    	}
    
    	public void run ()
    	{
    		
    		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    
    		
    		while (true)
    		{
    		
    			if (x_pos > appletsize_x - radius)
    			{
    			
    				x_speed = -1;
    			}
    			
    			else if (x_pos < radius)
    			{
    				
    				x_speed = +1;
    			}
    
    			
    			x_pos += x_speed;
    		
    			repaint();
    
    			try
    			{
    	
    				Thread.sleep (20);
    			}
    			catch (InterruptedException ex)
    			{
    				// do nothing
    			}
    
    			
    			Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    		}
    	}
    
    	
    	public void update (Graphics g)
    	{
    		// Initialisierung des DoubleBuffers
    		if (dbImage == null)
    		{
    			dbImage = createImage (this.getSize().width, this.getSize().height);
    			dbg = dbImage.getGraphics ();
    		}
    
    		
    		dbg.setColor (getBackground ());
    		dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
    
    	
    		dbg.setColor (getForeground());
    		paint (dbg);
    
    		
    		g.drawImage (dbImage, 0, 0, this);
    	}
    
    	public void paint (Graphics g)
    	{
    		g.setColor  (Color.red);
    
    		g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
    	}
    }

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    Unless I overlooked something, I don't see anywhere in your code where keyDown() will ever be called.

    Add a KeyListener and listen for KeyEvents.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center