Computer Science Canada Ready to Program IDE - Keyboard Help? |
Author: | KeijiAurion [ Mon Jan 15, 2007 10:13 am ] | ||
Post subject: | Ready to Program IDE - Keyboard Help? | ||
Hi, I'm basically computer illiterate, and I am stuck on how to make keyboard input move things across a GUI in Java. I'm using the Ready to Program Java IDE compiler, and I've run into a problem. I'm guessing I have to create whatever I want to move in the paint area (For this example, I'm using a circle). Will I have to somehow classify this circle as an object? I have found a tutorial on how to read keyboard input and determine the key code, if it is an action key, a modifier, and the location on the keyboard; but I'm unsure of what coding I'll need to modify this to move this circle around. Oh, by the way, I already know that this program is junk. Please, spare me from any flames like that; if I could use a proper Java compiler, I would. This is the tutorial I got:
mod edit: wtd added code tags |
Author: | KeijiAurion [ Tue Jan 16, 2007 9:51 am ] | ||
Post subject: | Re: Ready to Program IDE - Keyboard Help? | ||
Okay, I've started to try and compile on my own; but it's just not working. *sighs* I think I understand KeyListeners and KeyEvents and such; but I'm getting a lot of events. This is my code:
The things in Crimson represent errors I am facing; and here's what is coming up. Unfortunately, I don't understand what the errors are saying to me. - The abstract method "void actionPerformed(java.awt.event.ActionEvent $1);", inherited from type "java.awt.event.ActionListener", is not implemented in the non-abstract class "Fire Emblem". <--- I'm getting this for the error that's apparently on the first line of code: public class FireEmblem extends Applet - No method named "getGraphics" was found in type "java.awt.Graphics". <--- I'm getting this error at the area of Graphics g = g.getGraphics (); - An expression of type "void" is not valid in this context where a value is expected. <--- I'm getting this error when I'm trying to call the Object Player and giving it values; [Player = g.fillOval (x, y, 32, 32);] I'm thinking that I can't define the Player circle as an object; but I wouldn't know how I could easily discern between the player's circle and the enemy circles. Any help is appreciated! |
Author: | HellblazerX [ Tue Jan 16, 2007 2:07 pm ] | ||
Post subject: | Re: Ready to Program IDE - Keyboard Help? | ||
KeijiAurion wrote: - The abstract method "void actionPerformed(java.awt.event.ActionEvent $1);", inherited from type "java.awt.event.ActionListener", is not implemented in the non-abstract class "Fire Emblem". <--- I'm getting this for the error that's apparently on the first line of code: public class FireEmblem extends Applet
Your class, FireEmblem, implements the interface ActionListener, so FireEmblem inherits all the methods within ActionListener. Because ActionListener is an interface, all methods within it are abstract, meaning they are not fully implemented. Your class, FireEmblem, is non-abstract, so therefore it cannot have any abstract methods within it. The only way to get rid of these abstract methods is to override them, by declaring your own methods with the same name. In this case, you need a method within your class called "void actionPerformed (ActionEvent e)". KeijiAurion wrote: - No method named "getGraphics" was found in type "java.awt.Graphics". <--- I'm getting this error at the area of Graphics g = g.getGraphics ();
This one is pretty obvious. There is no method named getGraphics in your Graphics object "g". Also, you haven't instantiated "g", so you can't call any methods within it. I think what your really looking for is this:
KeijiAurion wrote: - An expression of type "void" is not valid in this context where a value is expected. <--- I'm getting this error when I'm trying to call the Object Player and giving it values; [Player = g.fillOval (x, y, 32, 32);]
The method fillOval does not return any values, hence the "void", so you cannot use that line. KeijiAurion wrote: I'm thinking that I can't define the Player circle as an object; but I wouldn't know how I could easily discern between the player's circle and the enemy circles. Any help is appreciated!
What you need to do is to define your own class called Circle that will contain the neccessary values you need. |
Author: | KeijiAurion [ Tue Jan 16, 2007 6:03 pm ] | ||||||
Post subject: | Re: Ready to Program IDE - Keyboard Help? | ||||||
HellblazerX @ Tue Jan 16, 2007 2:07 pm wrote: Your class, FireEmblem, implements the interface ActionListener, so FireEmblem inherits all the methods within ActionListener. Because ActionListener is an interface, all methods within it are abstract, meaning they are not fully implemented. Your class, FireEmblem, is non-abstract, so therefore it cannot have any abstract methods within it. The only way to get rid of these abstract methods is to override them, by declaring your own methods with the same name. In this case, you need a method within your class called "void actionPerformed (ActionEvent e)".
Hrm... I don't quite understand, but am I simply going to make something like this, where I simply leave it empty?:
HellblazerX @ Tue Jan 16, 2007 2:07 pm wrote:
Ah, didn't know it was this.getGraphics (). Thanks for that. I'm pretty sure that's what I'm looking for, too. HellblazerX @ Tue Jan 16, 2007 2:07 pm wrote: KeijiAurion wrote: - An expression of type "void" is not valid in this context where a value is expected. <--- I'm getting this error when I'm trying to call the Object Player and giving it values; [Player = g.fillOval (x, y, 32, 32);]
The method fillOval does not return any values, hence the "void", so you cannot use that line. KeijiAurion wrote: I'm thinking that I can't define the Player circle as an object; but I wouldn't know how I could easily discern between the player's circle and the enemy circles. Any help is appreciated!
What you need to do is to define your own class called Circle that will contain the neccessary values you need. Ah, that makes sense. Is it going to be something like:
Or is there something else I put for object o? Thanks for all your help, too! You're a lifesaver. |
Author: | HellblazerX [ Tue Jan 16, 2007 6:52 pm ] | ||||||||
Post subject: | Re: Ready to Program IDE - Keyboard Help? | ||||||||
KeijiAurion wrote:
That's fine if you're going to use these methods, but if you're only going to use KeyListeners, then you don't need to implement the ActionListeners. So, in your class declaration line:
You can omit the last part, ActionListener:
This way you don't need the "actionPerformed" method. KeijiAurion wrote:
Not quite. I said you should create a new class for your Player circle, like your FireEmblem class, not a method. I sounds like to me you haven't quite gotten a full understanding of object oriented programming, so I'm going to recommend that you check out wtd's tutorial on Java. It's a great tutorial on Java in general, and object oriented programming, which is a must for the type of game you're trying to make. |
Author: | KeijiAurion [ Wed Jan 17, 2007 9:23 am ] | ||||||||||
Post subject: | Re: Ready to Program IDE - Keyboard Help? | ||||||||||
HellblazerX @ Tue Jan 16, 2007 6:52 pm wrote: KeijiAurion wrote:
That's fine if you're going to use these methods, but if you're only going to use KeyListeners, then you don't need to implement the ActionListeners. So, in your class declaration line:
You can omit the last part, ActionListener:
This way you don't need the "actionPerformed" method. I believe I will be, since I'm probably going to try and make some sort of menu screen that will take mouse input. But, I guess, for now, I can omit that since my game is run without any sort of menu screen. Thanks for the tip. [quote HellblazerX] KeijiAurion wrote:
Not quite. I said you should create a new class for your Player circle, like your FireEmblem class, not a method. I sounds like to me you haven't quite gotten a full understanding of object oriented programming, so I'm going to recommend that you check out wtd's tutorial on Java. It's a great tutorial on Java in general, and object oriented programming, which is a must for the type of game you're trying to make.[/quote][/quote] Truthfully, I don't have any understanding at all of object oriented programming; I'm severely computer illiterate. Thanks for the link, however. I'll be sure to check that out. EDIT: Ugh. I'm stressing, and so confused, and I don't understand this one bit. I've made the following code:
But it's just not working. -_- I know something's wrong; but I just can't understand what. I feel really stupid. Someone, please save me! |
Author: | wtd [ Wed Jan 17, 2007 1:14 pm ] | ||
Post subject: | RE:Ready to Program IDE - Keyboard Help? | ||
There are a great many things wrong with it. Please don't take that as an insult or flame. Let's look at your simplest class (cleaned up a bit, stylistically):
Some observations: PlayerCircle has no constructor. The playerPaint method has no return type. The fillOval method that belongs to the Graphics abstract class returns void. You cannot assign that to an Object variable and return it. The resulting question is: Why is this method not simply a static one? |
Author: | KeijiAurion [ Thu Jan 18, 2007 7:42 am ] |
Post subject: | Re: Ready to Program IDE - Keyboard Help? |
I really, really don't know. I don't know what I'm doing anymore, and I think I'm going to abandon this project. I'm running out of time, and it seems like I've gotten nowhere. *sighs* Java is so confusing to me, that I'm finding it hard to get a grasp on how to code things. Okay, one last try at understanding this. On the constructor problem, should it be public class PlayerCircle? The playerPaint method, should I have (Graphics g, Object o) and then return the object? Hmm, I still don't really understand between the difference of void and such. Then again, I'm only returning it for the movement section of the code; at least, from what I see... I don't know why that method isn't static. We were never taught what static meant. We were taught very little; and I think I've tried to tackle a program too big for me. |
Author: | wtd [ Thu Jan 18, 2007 2:06 pm ] |
Post subject: | RE:Ready to Program IDE - Keyboard Help? |
Try reading my Introduction to Java. |
Author: | KeijiAurion [ Thu Jan 18, 2007 7:11 pm ] | ||
Post subject: | Re: Ready to Program IDE - Keyboard Help? | ||
Okay, I rewrote my program (to an extent), and used some of another person's source code; and I'm down to one single error.
My only problem is in the line: public class FireEmblem2 extends JApplet It says my problem is: The abstract method "void keyTyped(java.awt.event.KeyEvent $1);", inherited from type "java.awt.event.KeyListener", is not implemented in the non-abstract class "FireEmblem2". What does it mean by abstract and non-abstract class? T_T.. |
Author: | KeijiAurion [ Fri Jan 19, 2007 5:32 pm ] |
Post subject: | Re: Ready to Program IDE - Keyboard Help? |
*blushes* Ahh... I found out the problem. I had forgotten a keyTyped class, which is apparently required by all KeyListeners. Haha. I feel stupid. Thanks for all the help, though. I really appreciate it, guys. You've helped me learn a lot about Java! |