Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2004

Tip: Looking for answers? Try searching our database.

applet keyboard

Thread view: 
Linus - 14 May 2004 17:00 GMT
ive got an applet that uses itself as a thread (mainly to display itself)
and i want to use it to get keyboard input. the applet sleeps for 19
(milliseconds i think?) and therefore keyboard input is only collected every
19 whatever. i wanted to create a seperate thread to collect keyboard input,
but i havent been able to work how i can get input from the applet in a
similar form to the keyDown method. i have used a BufferedReader but it
collects input from a command line interface (as opposed to the applet) and
only returns the keys pressed when the enter key has been pressed (i doubt
this is actually true but as far as i could get it to work it is).

is there any solution to this?

sorry if my english is crappy or if i havent described the problem well
enough, but any help would be grately appreciated.

Linus
Chris Smith - 14 May 2004 17:16 GMT
> ive got an applet that uses itself as a thread (mainly to display itself)
> and i want to use it to get keyboard input.

You'll have to be clearer here.  Some code might help.  There's really
no such thing as an "applet that uses itself as a thread".  Applets and
threads are different things, and no single object can be both.  Perhaps
you mean an applet that uses a thread?

> the applet sleeps for 19 (milliseconds i think?) and therefore keyboard
> input is only collected every 19 whatever.

That shouldn't be the case.  Keyboard events should be delivered in the
event dispatch thread, and the event dispatch thread should never be
sleeping at all.

> i wanted to create a seperate thread to collect keyboard input,

You can't.  It's required to be done in the event dispatch thread.

If you post more code, perhaps someone can help you more.

Signature

www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Linus - 14 May 2004 21:40 GMT
ok im a bit tired so sorry if this makes even less sense. the aim is to make
a game similar to break out. i have an applet which implements Runnable,
then when its initialised (in init() ) has:

Thread pong = new Thread(this)
pong.start();

the applet uses double buffering, and i am using it as a thread so i can
control how often the screen is updated like this:

   public void run() {
       requestFocus();
       while (pong == Thread.currentThread() && currentLevel.play()) {
           repaint();
           try { Thread.sleep(19); } catch (InterruptedException e) {
System.out.println(e.getMessage()); }
       }
   }

if there is a better way of doing this (and there propably is) someone
please tell me. Ill note here that currentLevel.play() updates the position
of the balls on the screen etc and returns true if there are still balls in
play. And that i am also using a thread to control how often a games cycle
(recalculation of ball pos' etc) is. although im thinking of making the
level a thread itself and doing it that way (some one please tell me if this
is advisable)

So thats the background and the problem is that because im putting the
thread to sleep the keyDown function only detects a key when its awake (i
think) so the paddle jerks along slowly, rather than smoothly

Please note that this has been done on my own - and i have little experience
with OOP, so there is likely to be a lot of bad (ok terrible) design and i
would appreciate any advice / corrections / flaming :)

Linus
Chris Smith - 14 May 2004 22:01 GMT
> ok im a bit tired so sorry if this makes even less sense. the aim is to make
> a game similar to break out. i have an applet which implements Runnable,
> then when its initialised (in init() ) has:
>
> Thread pong = new Thread(this)
> pong.start();

Okay.  That will work, but it's very sloppy.  Your applet is a window,
and it doesn't make sense for it to be "run".  Instead, you could
replace this with:

   Thread pong = new Thread(new Runnable() {
       public void run()
       {
           // Your code here
       }
   });

   pong.start();

And then later, if your game logic code becomes too complex, you can
move it to a separate named, top-level class instead.  Either one is
better than shoving too many responsibilities on one class.

If possible, then using java.util.Timer to schedule screen updates is
probably a better idea that coordinating it with your own thread anyway,
but you'll still need a class, inner or otherwise, to act as a
TimerTask.

> So thats the background and the problem is that because im putting the
> thread to sleep the keyDown function only detects a key when its awake (i
> think) so the paddle jerks along slowly, rather than smoothly

First of all, I'd guess your problem with jerkiness is elsewhere.  The
19-second sleep doesn't explain it.  A 19-millisecond delay is too short
for the average human brain to notice.  Even adjusting for clock
resolution on a typical system, this shouldn't be making anything appear
jerky.

Second of all, keyDown?!?!?  That event model was deprecated with the
release of Java 1.1, back on the latter 90s.  It's best to steer clear
of it, and use KeyListener instead.

> Please note that this has been done on my own - and i have little experience
> with OOP, so there is likely to be a lot of bad (ok terrible) design and i
> would appreciate any advice / corrections / flaming :)

You'll need to post code that demonstrates the problem first.

Signature

www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Mikael Laine - 16 May 2004 16:25 GMT
I agree with what has been said before: your sleeping thread is not a
problem (in fact increasing the sleep time would only give the event
thread more time). To make gameplay smooth you need to have (boolean)
variables to hold if your keys are down or not. Then toggle these in
your keyDown/keyUP or KeyPressed/KeyReleased methods.
Roedy Green - 16 May 2004 18:42 GMT
>I agree with what has been said before: your sleeping thread is not a
>problem (in fact increasing the sleep time would only give the event
>thread more time)

Make sure you don't make the EVENT thread sleep though, or that has
exactly the opposite effect to the one you want. That's the #1 newbie
problem with Threads.

See http://mindprod.com/jgloss/thread.html

Signature

Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Roedy Green - 14 May 2004 17:33 GMT
>is there any solution to this?

see http://mindprod.com/products.html#KEYPLAY

Signature

Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.