Results 1 to 9 of 9
- 02-02-2012, 01:21 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 5
- Rep Power
- 0
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:
Thanks in advance.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) { } }
- 02-02-2012, 01:57 AM #2
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.
- 02-02-2012, 02:12 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 5
- Rep Power
- 0
Re: Having Trouble with Keyboard Input with Applets
Added requestFocus() in the last line of the init() method. Nope, still won't work.
- 02-02-2012, 02:32 AM #4
Re: Having Trouble with Keyboard Input with Applets
Did it work when you clicked on it to give it focus?
- 02-02-2012, 03:58 AM #5
Member
- Join Date
- Feb 2012
- Posts
- 5
- Rep Power
- 0
- 02-02-2012, 04:10 AM #6
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.
- 02-02-2012, 11:12 PM #7
Member
- Join Date
- Feb 2012
- Posts
- 5
- Rep Power
- 0
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?
- 02-02-2012, 11:17 PM #8
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.
- 02-03-2012, 12:24 AM #9
Member
- Join Date
- Feb 2012
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Question about Keyboard input
By littlebirdpoo in forum New To JavaReplies: 3Last Post: 12-17-2011, 04:33 PM -
Printing keyboard input to screen.
By adwodon in forum New To JavaReplies: 4Last Post: 12-16-2010, 10:40 AM -
Read input from keyboard
By bison in forum New To JavaReplies: 2Last Post: 11-20-2010, 06:48 PM -
How to use another image using a keyboard input
By Rekuta in forum New To JavaReplies: 0Last Post: 05-13-2010, 05:00 PM -
Polled keyboard input through swing
By Prometheus in forum Advanced JavaReplies: 2Last Post: 02-04-2008, 04:05 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks