Archived

This topic is now archived and is closed to further replies.

krez

[java] input in JAVA applets

Recommended Posts

i have just started "playing" with java, and i have a few newbie question... just to get me started, i am trying to write a terminal applet (later i might turn it into a MUD or something along those lines, or i might learn it and then throw it out, i don''t know yet)... but i have a few problems: (1) how do you get input for an applet? i was planning on storing several strings, for the lines already on the terminal, and another for the last line where the user input is shown. i found a tutorial about keyup and mousedown events and such, but my compiler says i am using a deprecated API, so there must be a better newer and accepted way to do this? (2) how do i make the applet update the screen? right now, it only updates when i switch to another window that covers the applet box, then switch back. how do i make it update? for example, every time i press a key it should add one character to my string and then re-draw the screen so the new string is displayed. i''d appreciate any help. thanks in advance --- krez (krezisback@aol.com)

Share this post


Link to post
Share on other sites
1) What you want is to use the event listener model introduced with JDK1.1. Check out the Java Tutorial page on how to do this: Java Tutorial: Writing Event Listeners.

2) What you're looking for is the repaint() method. Call it inside your applet and it will clear the screen, then it will call your paint() method where you re-draw the screen.

"If consquences dictate our course of action, it doesn't matter what's right, it's only wrong if you get caught."
- Tool



Edited by - WayfarerX on October 17, 2001 11:15:16 AM

Share this post


Link to post
Share on other sites
quote:
Original post by krez
i have just started "playing" with java, and i have a few newbie question...
just to get me started, i am trying to write a terminal applet (later i might turn it into a MUD or something along those lines, or i might learn it and then throw it out, i don''t know yet)... but i have a few problems:
(1) how do you get input for an applet? i was planning on storing several strings, for the lines already on the terminal, and another for the last line where the user input is shown. i found a tutorial about keyup and mousedown events and such, but my compiler says i am using a deprecated API, so there must be a better newer and accepted way to do this?
(2) how do i make the applet update the screen? right now, it only updates when i switch to another window that covers the applet box, then switch back. how do i make it update? for example, every time i press a key it should add one character to my string and then re-draw the screen so the new string is displayed.
i''d appreciate any help. thanks in advance

--- krez (krezisback@aol.com)



I haven''t really messed with Input yet, but I do know that you have to call repaint() for the screen to update.


BeS
It''s Da BOMB Baby!!!
. o O ~
A little nonsense now and then,
is relished by the wisest men
~ O o .
-- Willy Wonka

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
hey guy? The hole thang about getting text to come up on the screen and to make a action is called a event-handler. When you import the java.awt.*; also inport java.awt.event.*; ...I use
JDK1.3 by the way. Its avilable at Sun''s web site. I forgot the
address. Any ways theres a ton of event handlers you can implement of the java.awt.event.*; If you want key handleing you should implement the "KeyListener" interface. How ever you have to override all the functions it comes with. Well unless you go abstract. The 3 methods are :

public void keyTyped(KeyEvent e)
public void keyPressed(KeyEvent e)
public void keyReleased(KeyEvent e)

If you want to show the text your typing, you would use the keyTyped method. If you wanted to perform action on a setain key that had been pressed you would override the keyPressed method.
The exact oppisite (I think I spelled it right) for thr keyReleased. Pretty easy if you ask me.

public void keyPressed(KeyEvent e){
if(e.getSource() == VK_SPACE)
{
this.setBackground(Color.BLUE);
}
} and that is about it. Plus overriding the other methods. They don''t have to have code in them. Other event handling is ActionEventListener interface witch deals with
buttons and stuff like that being pressed. All this info is on JDK1.3. Get 1.3 not 1.1 or 1.2 they suck. Well 1.2 isn''t to bad.
But 1.1 is horrible. Hope this helps.

Share this post


Link to post
Share on other sites