public class HelloWorld extends Applet{
int a=50;
int b=8;
int c=a;
int d=99;
int e=d;
String where;
public int keyPressed(KeyEvent gg) {
int buttondown;
if (gg.getKeyCode() == KeyEvent.VK_RIGHT ) {
buttondown=1;
} else if (gg.getKeyCode() == KeyEvent.VK_LEFT ) {
buttondown=2;
} else if (gg.getKeyCode() == KeyEvent.VK_UP ) {
buttondown=3;
} else if (gg.getKeyCode() == KeyEvent.VK_DOWN ) {
buttondown=4;
}
return buttondown;
}
public void paint(Graphics g){
//g.drawString( "Hello World", 50,25);
g.drawRect (a,b,d,e);
g.drawLine(a,b,c,b);
g.drawLine(a,b+d,c,b+e);
g.drawLine(a,b,c,b+d);
int www=keyPressed();
if (www==1){
a+=1;
c-=1;
d-=1;
e-=1;
}
}
}
This is the only error that comes up:
keyPressed(java.awt.event.KeyEvent) in HelloWorld cannot be applied to ()
int www=keyPressed();
^
1 error
non-static variable sx cannot be referenced from a static context
while( sx!=5 ){
non-static variable sx cannot be referenced from a static context
used[sx][10]=ship;
^
non-static variable ship cannot be referenced from a static context
used[sx][10]=ship;
^
^
jrcdude
Posted: Sat Jul 16, 2011 5:44 pm Post subject: RE:Java Keyboard Event
That's more of a Java syntax error.
It's the public static main void joke
Vish
Posted: Sat Jul 16, 2011 6:44 pm Post subject: Re: Java Keyboard Event
is there any way to make it go away? is there any way to run a program that doesn't extend Applet or anything else, without a static main statement.
RandomLetters
Posted: Sat Jul 16, 2011 6:49 pm Post subject: RE:Java Keyboard Event
Nope.
Just create an instance of your program in the main method.
public etc main(String[] args) {
MyClass me = new MyClass();
me.doStuff();
}
DemonWasp
Posted: Sat Jul 16, 2011 9:30 pm Post subject: RE:Java Keyboard Event
Well...you could also make the instance variable a static variable by adding "static" to its declaration. In general, though, it's better to make an instance.
I'm still having the problem that my program doesn't seem to be able use the key listener.
Here is the debugging version:
import java.awt.*;
import java.awt.event.*;
public class InitArray
implements KeyListener{
final String ship="M";
static int sx=5;
public void keyReleased( KeyEvent e ) { }
public void keyTyped( KeyEvent e ) { }
public void keyPressed( KeyEvent e ) {
int j=e.getKeyCode();
if (j==KeyEvent.VK_RIGHT){
sx+=1;
Err, no it shouldn't. You don't have "hey" anywhere in your source code. You also haven't bothered to add an instance of the InitArray class to anything, and even if you had, you haven't done anything with the values you've changed in the keyPressed method, so you'll never see the effect of pressing the keys, even if the KeyListener was actively listening.
The pattern for using listeners is as follows:
1. Create a subclass that implements a Listener interface (done: InitArray implements KeyListener).
2. Create an instance of that subclass (not done). Use the new keyword.
3. Add that instance to the dispatcher (not done). In AWT, anything that extends Component is a dispatcher for KeyEvents, and you can add KeyListeners with addKeyListener().
class Display extends InitArray
implements KeyListener{
public void keyReleased( KeyEvent e ) { }
public void keyTyped( KeyEvent e ) { }
public void keyPressed( KeyEvent e ) {
int j=e.getKeyCode();
if (j==KeyEvent.VK_RIGHT){
super.sx+=1;
}
if (j==KeyEvent.VK_LEFT){
super.sx-=1;
}
}
}
This is what I have so far. Where do I put the "addKeyListener" and what would I add it to? The variable that is an instance of the subclass with the keylistener in it is called "disp".
First, I'm not sure why you introduced the Display class. It definitely is NOT a display of any kind. It's a keylistener for the player, so perhaps PlayerKeyListener would be a better name.
Second, why does Display extend InitArray? That means that "A Display is an InitArray," which makes no sense whatsoever. You can remove that "extends InitArray".
You've managed step 2 that I listed above. Now, where are the AWT objects? Component? JButton? JTextArea? Any of those? If you don't have any of those, go read a tutorial on AWT, because you'll need to add some. Once you find the AWT object you want to add the listener to, it's just:
code:
[object that is listening].addKeyListener ( playerKeyListener );
S_Grimm
Posted: Sun Sep 11, 2011 10:51 pm Post subject: Re: RE:Java Keyboard Event
DemonWasp @ Tue Jul 19, 2011 8:45 pm wrote:
First, I'm not sure why you introduced the Display class. It definitely is NOT a display of any kind. It's a keylistener for the player, so perhaps PlayerKeyListener would be a better name.
Second, why does Display extend InitArray? That means that "A Display is an InitArray," which makes no sense whatsoever. You can remove that "extends InitArray".
You've managed step 2 that I listed above. Now, where are the AWT objects? Component? JButton? JTextArea? Any of those? If you don't have any of those, go read a tutorial on AWT, because you'll need to add some. Once you find the AWT object you want to add the listener to, it's just:
code:
[object that is listening].addKeyListener ( playerKeyListener );
It appears that he doesn't want to create an AWT object to assign the listener to. Rather he wants to have the user push a key and have the cmd prompt display the output based on what key was pressed.
Vish
Posted: Mon Sep 12, 2011 6:13 am Post subject: Re: Java Keyboard Event
That's exactly what I want. I want the command to recognize it as a KeyEvent without tying it down to any one element. Then it should send that event to the keyPressed, keyTyped etc. and then act accordingly.
DemonWasp
Posted: Mon Sep 12, 2011 8:00 am Post subject: RE:Java Keyboard Event
It's a little early in the morning so I'm still foggy. Can you explain the use-case here? For example, what kind of application is this? What is the user doing (inputting text, using shortcuts, ...)? What does your code want to do when it gets input?