Results 1 to 9 of 9
  1. #1
    cg2916 is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default Having Trouble with Keyboard Input with Applets

    I am creating an applet with keyboard input. On one of my computers, keyboard input worked. Now, it doesn't. Here is my code:

    Java Code:
    import java.applet.AudioClip;
    import java.awt.Cursor;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.AbstractAction;
    import javax.swing.JApplet;
    import javax.swing.Timer;
    
    public class ChuckNorrisApplet extends JApplet implements KeyListener, Runnable {
    	private static final long serialVersionUID = 1L;
    	private Thread thread = null;
    	private BufferedImage title, i1, i2, i3, i4;
    	private Timer timer;
    	private boolean b1 = true, b2 = false, b3 = false, b4 = false;
    	private boolean b1s = true, b2s = false, b3s = false, b4s = false;
    	private AudioClip theme;
    
    	public void init() {
    		thread = new Thread(this);
    		thread.start();
    		setBlankCursor();
    		loadImages();
    		loadSound();
    		timer();
    		addKeyListener(this);
    	}
    
    	private void loadSound() {
    		theme = getAudioClip(getDocumentBase(), "theme.wav");
    		theme.loop();
    	}
    
    	private void timer() {
    		timer = new javax.swing.Timer(250, new AbstractAction() {
    			private static final long serialVersionUID = 1L;
    
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				b1 = !b1;
    				b2 = !b2;
    				b3 = !b3;
    				b4 = !b4;
    			}
    		});
    		timer.start();
    	}
    
    	private void loadImages() {
    		try {
    			title = ImageIO.read(new File("title.png"));
    			i1 = ImageIO.read(new File("new.png"));
    			i2 = ImageIO.read(new File("continue.png"));
    			i3 = ImageIO.read(new File("options.png"));
    			i4 = ImageIO.read(new File("quit.png"));
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    	private void setBlankCursor() {
    		BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
    		Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0), "blank cursor");
    		getContentPane().setCursor(blankCursor);
    	}
    
    	public void paint(Graphics g) {
    		if (b1 && b1s)
    			g.drawImage(i1, 0, 0, this);
    		else if (b2 && b2s)
    			g.drawImage(i2, 0, 0, this);
    		else if (b3 && b3s)
    			g.drawImage(i3, 0, 0, this);
    		else if (b4 && b4s)
    			g.drawImage(i4, 0, 0, this);
    		else
    			g.drawImage(title, 0, 0, this);
    	}
    
    	@SuppressWarnings("unused")
    	public void run() {
    		int frames = 0;
    		double unprocessedSeconds = 0;
    		long lastTime = System.nanoTime();
    		double secondsPerTick = 1 / 60.0;
    		int tickCount = 0;
    
    		while (true) {
    			long time = System.currentTimeMillis();
    			repaint();
    
    			time = (1000 / 60) - (System.currentTimeMillis() - time);
    			if (time > 0) {
    				try {
    					Thread.sleep(time);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		}
    	}
    
    	@Override
    	public void keyPressed(KeyEvent e) {
    		if (e.getKeyCode() == KeyEvent.VK_DOWN) {
    			if (b1s) {
    				b1s = false;
    				b2s = true;
    			} else if (b2s) {
    				b2s = false;
    				b3s = true;
    			} else if (b3s) {
    				b3s = false;
    				b1s = true;
    			} else if (b4s) {
    				b4s = false;
    				b1s = true;
    			}
    		}
    		if (e.getKeyCode() == KeyEvent.VK_UP) {
    			if (b1s) {
    				b1s = false;
    				b3s = true;
    			} else if (b2s) {
    				b2s = false;
    				b1s = true;
    			} else if (b3s) {
    				b3s = false;
    				b2s = true;
    			} else if (b4s) {
    				b4s = false;
    				b2s = true;
    			}
    		}
    		if (e.getKeyCode() == KeyEvent.VK_LEFT) {
    			if (b1s) {
    				b1s = false;
    				b3s = true;
    			} else if (b2s) {
    				b2s = false;
    				b1s = true;
    			} else if (b3s) {
    				b3s = false;
    				b4s = true;
    			} else if (b4s) {
    				b4s = false;
    				b3s = true;
    			}
    		}
    		if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
    			if (b1s) {
    				b1s = false;
    				b2s = true;
    			} else if (b2s) {
    				b2s = false;
    				b3s = true;
    			} else if (b3s) {
    				b3s = false;
    				b4s = true;
    			} else if (b4s) {
    				b4s = false;
    				b3s = true;
    			}
    		}
    	}
    
    	@Override
    	public void keyTyped(KeyEvent e) {
    	}
    
    	@Override
    	public void keyReleased(KeyEvent e) {
    	}
    }
    Thanks in advance.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,878
    Rep Power
    25

    Default Re: Having Trouble with Keyboard Input with Applets

    Does the component with KeyListener have the focus? Try click on it to see if that changes how the program works.
    If that's the problem look at the Component class for methods to get the focus to a component.

  3. #3
    cg2916 is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default Re: Having Trouble with Keyboard Input with Applets

    Added requestFocus() in the last line of the init() method. Nope, still won't work.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,878
    Rep Power
    25

    Default Re: Having Trouble with Keyboard Input with Applets

    Did it work when you clicked on it to give it focus?

  5. #5
    cg2916 is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default Re: Having Trouble with Keyboard Input with Applets

    Quote Originally Posted by Norm View Post
    Did it work when you clicked on it to give it focus?
    Nope

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,878
    Rep Power
    25

    Default Re: Having Trouble with Keyboard Input with Applets

    Not all components are capable of getting the focus. You need to call a method to tell the system that the component can get the focus. Look at the Component class's methods to see.

  7. #7
    cg2916 is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default Re: Having Trouble with Keyboard Input with Applets

    I found something! Apparently, after trying requestFocus() in various places, it only works when I put it in my while(true) loop. That's weird. If I put it in my loop, will it slow down my applet?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,878
    Rep Power
    25

    Default Re: Having Trouble with Keyboard Input with Applets

    That's weird. I've never seen that. Usually you only need to request it once when the app starts.
    I don't think it will slow down the applet.
    Try measuring it to see.

  9. #9
    cg2916 is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default Re: Having Trouble with Keyboard Input with Applets

    Doesn't slow it down. It works!

Similar Threads

  1. Question about Keyboard input
    By littlebirdpoo in forum New To Java
    Replies: 3
    Last Post: 12-17-2011, 05:33 PM
  2. Printing keyboard input to screen.
    By adwodon in forum New To Java
    Replies: 4
    Last Post: 12-16-2010, 11:40 AM
  3. Read input from keyboard
    By bison in forum New To Java
    Replies: 2
    Last Post: 11-20-2010, 07:48 PM
  4. How to use another image using a keyboard input
    By Rekuta in forum New To Java
    Replies: 0
    Last Post: 05-13-2010, 06:00 PM
  5. Polled keyboard input through swing
    By Prometheus in forum Advanced Java
    Replies: 2
    Last Post: 02-04-2008, 05:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •