8 Replies - 5465 Views - Last Post: 23 December 2012 - 07:37 AM

#1 WolfCoder   User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 822
  • View blog
  • Posts: 7,692
  • Joined: 05-May 05

Async Keyboard Input

Posted 07 January 2006 - 03:34 PM

Description: Just build as an Applet.Use the arrow keys to move the image in eight different directions!
// Allows you to move an image using the keyboard
// Written by WolfCoder 1-07-2005
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 Keymove extends JApplet
{
     public class DemoPanel extends JPanel
     {
          private Image testImage; // Image used for testing (16*16 pixels)
          private int playerX = 0;
          private int playerY = 0; // The player's current position in the applet
          boolean key_right,key_left,key_down,key_up; // Input booleans
          void DemoPanel()
          {
               // Does nothing here
          }
          public void gameInit()
          {
               testImage = getImage(getDocumentBase(),"testimg.gif"); // Load the test image
               // Image note:
               // The transparent color in the GIF file is the transparent color of the GIF in the Applet
               setFocusable(true); // IMPORTANT IMPORTANT! This allows the KeyListener to work!!!
               GameInput game_input = new GameInput(); // Make a new video game KeyListener
               addKeyListener(game_input); // Add it to the JPanel
          }
          public void paintComponent(Graphics page)
          {
               super.paintComponent(page);
               setBackground(Color.black); // Use a black background
               page.drawImage(testImage,playerX,playerY,this); // Draw the image at player's position
               if(key_down) // Move the player down
                    playerY++;
               if(key_up) // Move the player up
                    playerY--;
               if(key_right) // Move the player to the right
                    playerX++;
               if(key_left) // Move the player to the left
                    playerX--;
               for(int index = 0;index < 10000000;index++){} // Slow the Applet down to comfortable speeds
               repaint(); // next frame...
          }
          private class GameInput implements KeyListener
          {
               public void keyTyped(KeyEvent e){}
               public void keyReleased(KeyEvent e)
               {
                    // Check using getKeyCode for all four directional keys!
                    if(e.getKeyCode() == e.VK_DOWN) // If down is released...
                         key_down = false;
                    if(e.getKeyCode() == e.VK_UP) // If up is released...
                         key_up = false;
                    // Ect...
                    if(e.getKeyCode() == e.VK_RIGHT)
                         key_right = false;
                    if(e.getKeyCode() == e.VK_LEFT)
                         key_left = false;
               }
               public void keyPressed(KeyEvent e)
               {
                    // Check using getKeyCode for all four directional keys!
                    if(e.getKeyCode() == e.VK_DOWN) // If down is pressed...
                         key_down = true;
                    if(e.getKeyCode() == e.VK_UP) // If up is pressed...
                         key_up = true;
                    // Ect...
                    if(e.getKeyCode() == e.VK_RIGHT)
                         key_right = true;
                    if(e.getKeyCode() == e.VK_LEFT)
                         key_left = true;
               }
          }
     }
     public void init()
     {
          setSize(256,256);
          DemoPanel demopanel = new DemoPanel(); // Make a new JPanel for the video demo
          demopanel.gameInit(); // Call the game initialization function before adding the JPanel
          getContentPane().add(demopanel); // Add the JPanel
     }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Async Keyboard Input

#2 maz1986   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 15-May 07

Re: Async Keyboard Input

Posted 22 November 2007 - 01:30 AM

Great code
Was This Post Helpful? 0
  • +
  • -

#3 mcckiller   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 01-January 08

Re: Async Keyboard Input

Posted 01 January 2008 - 01:02 AM

this is exactly what i've been looking for to learn about applets you have no idea how happy and thankful i am
Was This Post Helpful? 0
  • +
  • -

#4 WolfCoder   User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 822
  • View blog
  • Posts: 7,692
  • Joined: 05-May 05

Re: Async Keyboard Input

Posted 01 January 2008 - 06:09 PM

I do because i had the same problem :)
Was This Post Helpful? 0
  • +
  • -

#5 negie   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 14-July 08

Re: Async Keyboard Input

Posted 22 July 2008 - 06:58 PM

nice work
Was This Post Helpful? 0
  • +
  • -

#6 sErvEr79   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 14-May 09

Re: Async Keyboard Input

Posted 05 December 2009 - 12:07 AM

hi there... i already run the code, but nothing happen.. can somebody help me? i use Netbeans 6.7.1. TQ
Was This Post Helpful? 0
  • +
  • -

#7 sErvEr79   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 14-May 09

Re: Async Keyboard Input

Posted 06 December 2009 - 08:21 PM

my problem is my applet can not find the image but it run well now =) tq for such a great program.. as a newbie this helps me a lot ...
Was This Post Helpful? 0
  • +
  • -

#8 RyanKelly16   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-March 12

Re: Async Keyboard Input

Posted 21 March 2012 - 07:28 AM

A black window appears whenever I open the file. Nothing seems to start the programs, it's just a black window. Seems like very good code though, professional-like.
Was This Post Helpful? 0
  • +
  • -

#9 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1577
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: Async Keyboard Input

Posted 23 December 2012 - 07:37 AM

It really isn't async since the event thread cant paint and handle user inputs at the same time :) Having a loop in your paint method to slow down the execution is just wrong in every single way Use a timer which calls repaint and fires every 16ms instead :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1