Java experts! I call on thee!

Baal

Tank
Joined
Sep 22, 2003
Messages
4,357
Reaction score
1
:dozey:

So anyways, I have been assigned to create a basic D&D game for my Java class.

I have four classes set up: An armor class, weapon class, character class, and the form: NewJFrame

I am attempting to pass a variable from the form class (selectedIndex of a combo box) so that I can pick weapons/armor from an armory and then have the code execute (battle take place) based on the weapons/armor selected.

I have basically everything set up already, except for the ability to pass variables from the form to the armor/weapon classes.

armoryForm.JPG


There is my form, just to get an idea of what I'm getting at. Basically, when I click the "enter battle" button, I need to pass variables from the form class to the armor/weapon classes

Some of my code:

Code:
    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
// TODO add your handling code here:
        
        int setSelArmor = 0;
        
        setSelArmor = jComboBox2.getSelectedIndex();
        
        
        if (setSelArmor == 1 || setSelArmor == 2) {
            jComboBox1.setEnabled(true);
        }
        else {
            jComboBox1.setEnabled(false);
            jComboBox1.setSelectedIndex(1);
        }          
    }

This is the action event for my combo box. So, for example, I would like to be able to take the setSelArmor variable and pass it, but I get one of two errors:

Code:
public class armor extends NewJFrame {
int calcArm(char cType){
int armPicked = NewJFrame.setSelArmor;
}
}
//results in "cannot find symbol" (cannot find the variable)

Then if I try to use a set/get method it says I cannot reference a non-static variable from a static context
 
You need to implement an action listener interface for the drop down list on the form, and when something is selected it should set a value in an instance variable that you can use in your class.
 
Ok thanks, I'll do a little research and give that a go.
 
Okay... this where a basic understanding of OOP comes in handy. The problem is that you're referencing the property setSelArmor like its a static property of the NewJFrame class, when its not actually a static property. Static properties are properties that are accessible without a reference to an instantiation of the class. To access that property, you'll need a reference to your instantiated NewJFrame object. I'm not familiar with Java itself, but OOP follows pretty much the same patterns across any language.

Somewhere, you have an object that is an instantiation of NewJFrame. In C#, it might look like this:

Code:
NewJFrame myFrame = new NewJFrame();
int armPicked = myFrame.setSelArmor;

In this case, it looks like the "armor" is extending (also called inheriting) "NewJFrame", so try just referencing base.setSelArmor instead. I'm not sure if the "base" or "this" keyword is valid in Java, but in C#, "this" gives you access to the members of the current instantiation of whatever object you're working in, and "base" gives you access to the members of the base class of the object.

Also, I don't think its really necessary to inherit a form class for "armor"... you'd be better off creating a base "avatar" class, and then creating armor, mage, whatever else as data object classes, and then have your form class just be solely for your UI.

Hit me up on IM if you want to talk... DreamThrall on AIM or [email protected] on MSN. I love helping people out learning this stuff.
 
edit:scratch that , it appears you have a very different problem. you're trying to access variables from the parent , this can only be done by passing an instance of the parent into the constructor of the class because the variable is only defined at run time and even then you wont be able to access the variable because its not public or in the global scope. im not really sure why the armor class extends from the jframe class arent you just using an object to keep track of the armor the player has chosen?. if thats so then you should just create an instance of the armor class within the frame class and then manipulate the object inside the actionperformed method. if you need more help mail me your code [email protected] .
 
You are referencing the class as if it were static, when you should be referencing the methods of an instantiated object.

You shouldn't use, for instance:
int armPicked = NewJFrame.setSelArmor;

instead call whatever your JFrame is.

JFrame frame= new JFrame();
int armPicked=frame.setSelArmor();

Something like that.

EDIT: Either that or you can make JFrame Static, but since its a reserved class you can't.
 
You are referencing the class as if it were static, when you should be referencing the methods of an instantiated object.

You shouldn't use, for instance:
int armPicked = NewJFrame.setSelArmor;

instead call whatever your JFrame is.

JFrame frame= new JFrame();
int armPicked=frame.setSelArmor();

Something like that.

EDIT: Either that or you can make JFrame Static, but since its a reserved class you can't.

that would in effect create two main frame windows because he has one instance of the object in the main loop or "run" method and one in the class.
 
DreamThrall is a god. He helped me so much. Just look at the chatlogs!


Date Time From To Message
11/17/2007 11:42:32 AM Garry Daniel Schaffer Hi there!
11/17/2007 11:42:35 AM Daniel Schaffer Garry I am currently away from the computer.
11/17/2007 12:34:08 PM Daniel Schaffer Garry hey
11/17/2007 12:34:21 PM Garry Daniel Schaffer hi there!
11/17/2007 12:34:31 PM Garry Daniel Schaffer I'm the guy with the java issues from HL2.net :P
11/17/2007 12:34:33 PM Daniel Schaffer Garry right
11/17/2007 12:34:37 PM Daniel Schaffer Garry you get that stuff figured out?
11/17/2007 12:34:53 PM Garry Daniel Schaffer no, I didn't get much of a chance to work on it last night - birthday party for my brother in law
11/17/2007 12:34:59 PM Daniel Schaffer Garry ah
11/17/2007 12:35:21 PM Daniel Schaffer Garry so are they teaching you oop fundamentals in that class before they throw the actual language at you?
11/17/2007 12:35:33 PM Garry Daniel Schaffer yea, kind of
11/17/2007 12:35:59 PM Daniel Schaffer Garry honestly, i have no idea how this stuff is taught... i just learned from books
11/17/2007 12:36:08 PM Garry Daniel Schaffer ah, right on
11/17/2007 12:36:23 PM Daniel Schaffer Garry its pretty easy once you get the hang of it though
11/17/2007 12:36:34 PM Garry Daniel Schaffer Well, I took VB last year, and PHP this year as well. I have some basic programming knowledge but the OOP thing is screwing me I'd say
11/17/2007 12:36:51 PM Daniel Schaffer Garry heh, yeah that can be a tough one to get your head around
11/17/2007 12:37:13 PM Daniel Schaffer Garry i can try to explain some stuff if you've got any questions
11/17/2007 12:37:44 PM Garry Daniel Schaffer I just need to be able to pass a variable from my JFrame class to another class based on what the user does in the JFrame
11/17/2007 12:38:07 PM Daniel Schaffer Garry do you have a mic?
11/17/2007 12:38:09 PM Garry Daniel Schaffer I don't quite see how instantiating a new JFrame class would work either
11/17/2007 12:38:12 PM Garry Daniel Schaffer nope
11/17/2007 12:38:20 PM Daniel Schaffer Garry that's not actually what you want to do
11/17/2007 12:38:23 PM Garry Daniel Schaffer errr
11/17/2007 12:38:25 PM Garry Daniel Schaffer object, I guess
11/17/2007 12:38:31 PM Daniel Schaffer Garry instantiating anew object i mean
11/17/2007 12:38:52 PM Daniel Schaffer Garry alright... based on what i read in your post, you should have a couple different classes
11/17/2007 12:39:26 PM Daniel Schaffer Garry you should have one for your UI form - I'm guessing this is JFrame. that's where you have your dropdown boxes and other controls
11/17/2007 12:39:35 PM Garry Daniel Schaffer yep!
11/17/2007 12:39:38 PM Daniel Schaffer Garry oka
11/17/2007 12:39:39 PM Daniel Schaffer Garry y
11/17/2007 12:39:57 PM Daniel Schaffer Garry then, i would also have a "base" class for avatars
11/17/2007 12:40:14 PM Daniel Schaffer Garry call "Avatar" or "AvatarBase" or whatever
11/17/2007 12:40:42 PM Garry Daniel Schaffer ok
11/17/2007 12:40:55 PM Daniel Schaffer Garry it'll have methods and properties used for performing the combat calculations and such
11/17/2007 12:41:06 PM Daniel Schaffer Garry but that's getting ahead a bit i think, heh
11/17/2007 12:41:31 PM Daniel Schaffer Garry now, what i believe you need to do for when someone selects something on your form is use event handlers
11/17/2007 12:41:57 PM Garry Daniel Schaffer "private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt)"
11/17/2007 12:42:13 PM Daniel Schaffer Garry okay, so that fires when someone selects something?
11/17/2007 12:42:29 PM Garry Daniel Schaffer yep
11/17/2007 12:42:32 PM Daniel Schaffer Garry good
11/17/2007 12:42:55 PM Daniel Schaffer Garry alright, so when that event fires, its still in the context of the JFrame object
11/17/2007 12:43:00 PM Garry Daniel Schaffer yea
11/17/2007 12:43:17 PM Daniel Schaffer Garry so you should have access to the control itself, as well as the other controls that are on the form
11/17/2007 12:43:43 PM Garry Daniel Schaffer yep, I think so
11/17/2007 12:43:56 PM Garry Daniel Schaffer that sounds about right
11/17/2007 12:45:08 PM Daniel Schaffer Garry okay, so then you should be able to do something like int selectedIndex = jComboBox2.selectedIndex (or whatever the property name is)
11/17/2007 12:45:13 PM Daniel Schaffer Garry ;
11/17/2007 12:45:25 PM Garry Daniel Schaffer that's what I have, yea
11/17/2007 12:45:49 PM Garry Daniel Schaffer but I need to get that variable into another class
11/17/2007 12:46:04 PM Daniel Schaffer Garry okay, what class?
11/17/2007 12:46:08 PM Garry Daniel Schaffer armor class
11/17/2007 12:46:16 PM Daniel Schaffer Garry what's that do?
11/17/2007 12:46:34 PM Garry Daniel Schaffer defines what armor the character has on when the battle begins
11/17/2007 12:46:55 PM Garry Daniel Schaffer well, it sets the armor value of the armor instance, I guess
11/17/2007 12:47:04 PM Daniel Schaffer Garry okay to clarify... you have a class for armor that might have properties like absorption, etc
11/17/2007 12:47:37 PM Daniel Schaffer Garry you could also have an array property that defines weapon types the armor is effective against
11/17/2007 12:47:49 PM Garry Daniel Schaffer yea but I'm not going that deep
11/17/2007 12:47:51 PM Daniel Schaffer Garry ah ok
11/17/2007 12:48:06 PM Daniel Schaffer Garry well, to do what you're trying to do, i would use an instantiator
11/17/2007 12:48:06 PM Garry Daniel Schaffer just, if the user selects an armor from the drop down, it is defined in the armor class
11/17/2007 12:48:10 PM Garry Daniel Schaffer ok
11/17/2007 12:48:21 PM Garry Daniel Schaffer JFrame frame= new JFrame(); int armPicked=frame.setSelArmor(); ?
11/17/2007 12:48:31 PM Daniel Schaffer Garry oops, i meant constructor
11/17/2007 12:48:48 PM Daniel Schaffer Garry in your armor class, you define a contructor that will take the value of the armor
11/17/2007 12:49:05 PM Daniel Schaffer Garry so in armor, you might have a property called value
11/17/2007 12:49:37 PM Daniel Schaffer Garry so then, you define the constructor like this: public amor(int value) this.value = value;
11/17/2007 12:49:47 PM Daniel Schaffer Garry oops, the im client took out the brackets
11/17/2007 12:49:57 PM Daniel Schaffer Garry public armor(int value) {
11/17/2007 12:50:03 PM Daniel Schaffer Garry this.value = value; }
11/17/2007 12:50:41 PM Daniel Schaffer Garry and then when you create your armor object on the form: armor theArmor = new armor(jComboBox2.selectedValue);
11/17/2007 12:51:10 PM Daniel Schaffer Garry keep in mind that I don't *actually* know java syntax, so the code I'm typing is just an approximation
11/17/2007 12:51:32 PM Garry Daniel Schaffer hmm ok
11/17/2007 12:52:10 PM Daniel Schaffer Garry heh, are you lost?
11/17/2007 12:52:51 PM Garry Daniel Schaffer Not exactly, but doing all of that might be a little tough because I basically already have it done, I just need the form to pass a variable but it's proving difficult and painful
11/17/2007 12:53:12 PM Daniel Schaffer Garry okay, so you've already written your armor class?
11/17/2007 12:53:21 PM Garry Daniel Schaffer everything, basically
11/17/2007 12:53:38 PM Daniel Schaffer Garry okay, so what properties does the armor class have?
11/17/2007 12:53:51 PM Daniel Schaffer Garry actually
11/17/2007 12:53:59 PM Garry Daniel Schaffer well, it returns a value is what it does
11/17/2007 12:54:01 PM Daniel Schaffer Garry what property of the armor class are you trying to set by passing the variable to it?
11/17/2007 12:54:06 PM Daniel Schaffer Garry oh
11/17/2007 12:54:11 PM Daniel Schaffer Garry so its a method then, not a class?
11/17/2007 12:54:47 PM Garry Daniel Schaffer kind of, I guess
11/17/2007 12:55:07 PM Garry Daniel Schaffer it returns a value, and then in the character class
11/17/2007 12:55:08 PM Garry Daniel Schaffer I have
11/17/2007 12:55:12 PM Garry Daniel Schaffer arm = new armor();
11/17/2007 12:55:15 PM Garry Daniel Schaffer and then:
11/17/2007 12:55:30 PM Daniel Schaffer Garry heh, do you think you could email me what you've got?
11/17/2007 12:55:37 PM Garry Daniel Schaffer might be a good idea
11/17/2007 12:55:41 PM Daniel Schaffer Garry [email protected]
11/17/2007 12:56:16 PM Daniel Schaffer Garry take a look through this: http://www.ccnyddm.com/AdvJava/java_constructor_tutorial.htm
11/17/2007 12:58:27 PM Garry Daniel Schaffer let me log onto MSN on my laptop, brb

Thanks so much, and all hail thee Dan.
 
Needed two posts!

Date Time From To Message
11/17/2007 1:00:43 PM Garry sends C:\Documents and Settings\Garry\Desktop\D&D-Java.txt
11/17/2007 1:00:59 PM Garry Daniel Schaffer if it's commented out, chances are it was something I tried that didn't work
11/17/2007 1:01:05 PM Transfer of "D&D-Java.txt" is complete.
11/17/2007 1:01:09 PM Daniel Schaffer Garry ah
11/17/2007 1:01:24 PM Garry Daniel Schaffer what it does now, is loads the form and does the battle immediately in system.out
11/17/2007 1:01:24 PM Daniel Schaffer Garry you generally just want to delete that stuff ;)
11/17/2007 1:01:40 PM Garry Daniel Schaffer well, it might give you an idea of what I tried that didn't work / what my thought process is
11/17/2007 1:01:47 PM Daniel Schaffer Garry ah ok
11/17/2007 1:02:02 PM Garry Daniel Schaffer I don't care about how it conducts the battle at the moment, I just need to pass variables from the form to the armor class
11/17/2007 1:02:11 PM Garry Daniel Schaffer I don't care how I do it, as long as it gets done
11/17/2007 1:02:31 PM Garry Daniel Schaffer I guess I need to pass variables to the weapon class too, since I have two combo boxes, but if I get one done i should be able to get the other done as well
11/17/2007 1:02:43 PM Garry Daniel Schaffer brb, feel free to ask any questions and I'll answer them when I get back
11/17/2007 1:02:51 PM Daniel Schaffer Garry okay, so why does armor extend NewJFrame?
11/17/2007 1:03:56 PM Daniel Schaffer Garry okay, i think i'm starting to understand...
11/17/2007 1:04:14 PM Daniel Schaffer Garry you have a form class where you select each attribute of your character
11/17/2007 1:07:11 PM Daniel Schaffer Garry er no
11/17/2007 1:08:40 PM Garry Daniel Schaffer the form class is just for loading the form, pretty much
11/17/2007 1:08:48 PM Garry Daniel Schaffer maybe that's a better way to do that...
11/17/2007 1:09:02 PM Garry Daniel Schaffer I guess I could just load the form from the other classes in a constructor?
11/17/2007 1:09:04 PM Daniel Schaffer Garry alright, well for now, why does armor extend NewJFrame?
11/17/2007 1:09:20 PM Garry Daniel Schaffer I guess that was something that I just tried because I thought it might work
11/17/2007 1:09:35 PM Garry Daniel Schaffer I don't even really know what that does
11/17/2007 1:09:57 PM Daniel Schaffer Garry okay... you understand superclasses and inheritance?
11/17/2007 1:10:04 PM Garry Daniel Schaffer vageuly
11/17/2007 1:10:12 PM Garry Daniel Schaffer err, spelling, but you get it
11/17/2007 1:10:15 PM Daniel Schaffer Garry heh right :)
11/17/2007 1:10:28 PM Daniel Schaffer Garry okay... do you understand the difference between a class and an object?
11/17/2007 1:10:51 PM Garry Daniel Schaffer Our teacher explains it as a car analogy
11/17/2007 1:11:07 PM Garry Daniel Schaffer There's no such thing as a car object.
11/17/2007 1:11:07 PM Daniel Schaffer Garry right, vehicle, truck, motorcycle, etc
11/17/2007 1:11:16 PM Garry Daniel Schaffer There's a car class
11/17/2007 1:11:17 PM Garry Daniel Schaffer I guess
11/17/2007 1:11:27 PM Daniel Schaffer Garry okay, but that's not the same question
11/17/2007 1:11:53 PM Daniel Schaffer Garry a class is like a definiton, while an object is an instantiation of a class
11/17/2007 1:12:05 PM Garry Daniel Schaffer right!
11/17/2007 1:12:23 PM Garry Daniel Schaffer I'm starting to understand this stuff a little better
11/17/2007 1:12:41 PM Daniel Schaffer Garry okay, so when you extend the NewJFrame class, it doesn't actually do anything, because your object (in this case, your form) is of the NewJFrame class
11/17/2007 1:13:16 PM Daniel Schaffer Garry in order for it to do anything, you'd have to instantiate a new armor class, which in this case would mean opening up a new form
11/17/2007 1:14:12 PM Daniel Schaffer Garry now i think all you need to do is move the methods from armor into your NewJFrame class
11/17/2007 1:15:03 PM Garry Daniel Schaffer could I do like
11/17/2007 1:15:15 PM Garry Daniel Schaffer arm = new armor(); or...
11/17/2007 1:15:26 PM Garry Daniel Schaffer well no, I don' think that would work
11/17/2007 1:15:31 PM Garry Daniel Schaffer at least, not serve the purpose
11/17/2007 1:15:36 PM Daniel Schaffer Garry right
11/17/2007 1:15:59 PM Daniel Schaffer Garry all you're trying to do is just get an integer that represents the value of the armor, right?
11/17/2007 1:16:03 PM Garry Daniel Schaffer yea
11/17/2007 1:16:13 PM Daniel Schaffer Garry okay, so you don't really need an entirely separate class for that
11/17/2007 1:16:42 PM Daniel Schaffer Garry unless that's part of the assignment
11/17/2007 1:16:52 PM Garry Daniel Schaffer haha yea, I think that it is
11/17/2007 1:16:56 PM Daniel Schaffer Garry lol ok
11/17/2007 1:17:05 PM Daniel Schaffer Garry alright, well let's try and restructure your classes then
11/17/2007 1:17:23 PM Daniel Schaffer Garry you kinda did weapon the right way
11/17/2007 1:17:53 PM Garry Daniel Schaffer doesn't seem any different than armor
11/17/2007 1:17:56 PM Daniel Schaffer Garry for armor, what I would do is define a "Value" property
11/17/2007 1:18:12 PM Daniel Schaffer Garry remove the extends NewJFrame
11/17/2007 1:18:15 PM Garry Daniel Schaffer done
11/17/2007 1:18:19 PM Daniel Schaffer Garry armor shouldn't extend anything
11/17/2007 1:18:27 PM Daniel Schaffer Garry so you'd have your Value property, which is an int
11/17/2007 1:18:40 PM Garry Daniel Schaffer what about my arm variable?
11/17/2007 1:18:47 PM Garry Daniel Schaffer that's my armor value I am returning
11/17/2007 1:18:47 PM Daniel Schaffer Garry hold on :)
11/17/2007 1:18:49 PM Daniel Schaffer Garry right
11/17/2007 1:18:51 PM Garry Daniel Schaffer oh :(
11/17/2007 1:19:03 PM Daniel Schaffer Garry do you understand constructors?
11/17/2007 1:19:16 PM Garry Daniel Schaffer they seem to be the thing that executes stuff
11/17/2007 1:19:26 PM Daniel Schaffer Garry kinda sorta not really
11/17/2007 1:19:35 PM Daniel Schaffer Garry a constructor is the method that instantiates a class
11/17/2007 1:19:47 PM Daniel Schaffer Garry they don't have a return type, and they can accept parameters
11/17/2007 1:20:08 PM Daniel Schaffer Garry in fact, when you do weap = new weapon();
11/17/2007 1:20:19 PM Daniel Schaffer Garry that's using a constructor - the default constructor
11/17/2007 1:20:42 PM Garry Daniel Schaffer ah
11/17/2007 1:21:09 PM Daniel Schaffer Garry so what i think you should do is create a constructor for the armor class that accepts the cType variable
11/17/2007 1:21:35 PM Daniel Schaffer Garry except i'd call it something more descriptive
11/17/2007 1:22:10 PM Garry Daniel Schaffer that's just the character type
11/17/2007 1:22:12 PM Garry Daniel Schaffer soldier or creature
11/17/2007 1:22:22 PM Daniel Schaffer Garry okay
11/17/2007 1:23:00 PM Daniel Schaffer Garry so have a CharacterType property that is a char
11/17/2007 1:23:41 PM Daniel Schaffer Garry your constrcutor would then look like this: public armor(Char cType) { this.setCharacterType(cType); }
11/17/2007 1:24:04 PM Daniel Schaffer Garry (i think that's the right java syntax for a property set accessor?)
11/17/2007 1:24:18 PM Garry Daniel Schaffer not sure
11/17/2007 1:25:06 PM Daniel Schaffer Garry looking at the rest of your code, it looks like it is
11/17/2007 1:25:26 PM Daniel Schaffer Garry so then, the get accessor for your Value property would actually do all the wokr
11/17/2007 1:25:27 PM Daniel Schaffer Garry work **
11/17/2007 1:26:09 PM Daniel Schaffer Garry so instead of int calcAmr(char cType) you'd have: int getValue()
11/17/2007 1:26:23 PM Daniel Schaffer Garry you follow me?
11/17/2007 1:26:47 PM Garry Daniel Schaffer trying to, but not exactly
11/17/2007 1:27:02 PM Daniel Schaffer Garry okay, what are you foggy on?
11/17/2007 1:27:35 PM Garry Daniel Schaffer just not really sure why that would do anything
11/17/2007 1:27:43 PM Garry Daniel Schaffer the setCharacterType(cType) etc.
11/17/2007 1:28:18 PM Daniel Schaffer Garry well you'd have to change your code to use that property instead of getting it from the method parameter
11/17/2007 1:29:28 PM Garry Daniel Schaffer hmm
11/17/2007 1:30:20 PM Garry Daniel Schaffer I'm missing visual basic at this point
11/17/2007 1:30:26 PM Garry Daniel Schaffer haha :(
11/17/2007 1:30:42 PM Daniel Schaffer Garry so you'd have this: public class Armor { private char characterType; public Armor(char cType){ this.characterType = cType; } public int getValue() { // do all your calcArm work here instead }
11/17/2007 1:30:44 PM Daniel Schaffer Garry }
11/17/2007 1:31:19 PM Garry Daniel Schaffer ok
11/17/2007 1:31:27 PM Daniel Schaffer Garry then, from your form, you call: Armor armor = new Armor(cType);
11/17/2007 1:31:37 PM Garry Daniel Schaffer hmm...
11/17/2007 1:32:07 PM Daniel Schaffer Garry closer now?
11/17/2007 1:32:49 PM Garry Daniel Schaffer yea I think so
11/17/2007 1:33:14 PM Daniel Schaffer Garry now, you have a real reason for having separate classes
11/17/2007 1:33:38 PM Daniel Schaffer Garry because having a completely separate class for a single method is frankly a bit silly ;)
11/17/2007 1:34:22 PM Daniel Schaffer Garry now, are you also supposed to be demonstrating superclasses?
11/17/2007 1:35:58 PM Garry Daniel Schaffer typing this out now, one sec
11/17/2007 1:36:05 PM Daniel Schaffer Garry k
11/17/2007 1:37:17 PM Garry Daniel Schaffer oh, and no, i don't think superclasses is an issue
11/17/2007 1:37:38 PM Daniel Schaffer Garry okay, we don't have to go there then... unless you really want to impress teacher ;)
11/17/2007 1:37:47 PM Garry Daniel Schaffer nope, that's quite ok!
11/17/2007 1:37:58 PM Daniel Schaffer Garry alright then :)
11/17/2007 1:38:06 PM Garry Daniel Schaffer so when I type out
11/17/2007 1:38:11 PM Garry Daniel Schaffer public Armor(char cType){ this.characterType = cType; }
11/17/2007 1:38:17 PM Garry Daniel Schaffer It stays "return type required"
11/17/2007 1:38:40 PM Daniel Schaffer Garry okay, is your class named armor or Armor?
11/17/2007 1:38:58 PM Daniel Schaffer Garry the naming convention is to capitalize the first letter of class names
11/17/2007 1:39:11 PM Garry Daniel Schaffer damn you
11/17/2007 1:39:16 PM Garry Daniel Schaffer that was it
11/17/2007 1:39:18 PM Daniel Schaffer Garry :)
11/17/2007 1:41:43 PM Daniel Schaffer Garry now, the interest of keeping things uniform, you should follow the same pattern for your Weapon class
11/17/2007 1:42:06 PM Daniel Schaffer Garry use the constructor that takes the cType parameter
11/17/2007 1:43:53 PM Daniel Schaffer Garry now, for calcDamage, what does "stunned" do?
11/17/2007 1:44:10 PM Garry Daniel Schaffer oh yea, I haven't really implemented that
11/17/2007 1:44:13 PM Daniel Schaffer Garry okay
11/17/2007 1:44:18 PM Garry Daniel Schaffer it's a % chance to miss a turn
11/17/2007 1:44:31 PM Daniel Schaffer Garry that's what I thought... you'll probably need to use an out paramter for that
11/17/2007 1:45:05 PM Daniel Schaffer Garry so it'd be like: int CalculateDamage(out boolean stunned) { ... }
11/17/2007 1:45:57 PM Daniel Schaffer Garry and then when you call it: boolean stunned; int dmg = weap.CalculateDamage(out stunned);
11/17/2007 1:46:47 PM Daniel Schaffer Garry if(stunned) { System.out.println("You wimp! You got stunned and miss a turn!"); }
11/17/2007 1:47:02 PM Garry Daniel Schaffer hah, yea
11/17/2007 1:47:52 PM Daniel Schaffer Garry although... you probably need to have "stunned" be a property of weapon
11/17/2007 1:48:01 PM Daniel Schaffer Garry so that way, it'll be propegated to the next round
11/17/2007 1:48:32 PM Daniel Schaffer Garry then, in CalculateDamage, you'll be able to check if the player is stunned from the previous round
 
Three!

11/17/2007 1:48:40 PM Garry Daniel Schaffer I think I might be getting in over my head here. Your suggestions aren't work and I don't know why they're not working or even why I'm doing them so I can't debug :(
11/17/2007 1:49:06 PM Daniel Schaffer Garry eek
11/17/2007 1:49:07 PM Daniel Schaffer Garry ok
11/17/2007 1:49:42 PM Daniel Schaffer Garry where are you now?
11/17/2007 1:49:50 PM Garry Daniel Schaffer I appreciate your help but I might need to tackle this a different way if possible
11/17/2007 1:50:31 PM Daniel Schaffer Garry well maybe i can try explaining a little more instead of throwing code at you :\ sorry, i get a little overzealous with programming
11/17/2007 1:50:39 PM Garry Daniel Schaffer haha no, it's fine
11/17/2007 1:51:13 PM Garry Daniel Schaffer I just want to cheat and do it the way we were discussing earlier
11/17/2007 1:51:19 PM Garry Daniel Schaffer get rid of the armor and weapon class
11/17/2007 1:51:29 PM Daniel Schaffer Garry heh ok
11/17/2007 1:51:36 PM Garry Daniel Schaffer err I meant
11/17/2007 1:51:40 PM Garry Daniel Schaffer "I'm tempting to...."
11/17/2007 1:51:50 PM Garry Daniel Schaffer I don't know if I should actually do that
11/17/2007 1:51:53 PM Daniel Schaffer Garry ah
11/17/2007 1:52:05 PM Daniel Schaffer Garry well does the assignment say you're supposed to use multiple classes?
11/17/2007 1:52:28 PM Garry Daniel Schaffer yea I believe so, but I don't know if that's feasable at this point
11/17/2007 1:52:36 PM Daniel Schaffer Garry well its certainly feasible
11/17/2007 1:53:19 PM Daniel Schaffer Garry let's try this... what don't you understand about the code i was explaining?
11/17/2007 1:54:01 PM Garry Daniel Schaffer well, when I tried to create a new instance of armor in my jframe, it wasn't quite working
11/17/2007 1:54:17 PM Daniel Schaffer Garry what error did you get?
11/17/2007 1:54:29 PM Garry Daniel Schaffer let me see, one sec
11/17/2007 1:56:59 PM Garry Daniel Schaffer ok, one thing was my filename was "armor.java"
11/17/2007 1:57:08 PM Garry Daniel Schaffer not "Armor.java" - I don't think it liked that very much
11/17/2007 1:57:26 PM Daniel Schaffer Garry hm... okay
11/17/2007 1:57:57 PM Garry Daniel Schaffer now, where do I put "Armor armor = new Armor(cType);"
11/17/2007 1:58:07 PM Garry Daniel Schaffer in the NewJFrame constructor?
11/17/2007 1:58:54 PM Daniel Schaffer Garry it looks like it should go in the character class
11/17/2007 1:59:15 PM Daniel Schaffer Garry *cough* which should be Character *cough* ;)
11/17/2007 2:00:13 PM Garry Daniel Schaffer ok...well that's done I guess
11/17/2007 2:02:14 PM Daniel Schaffer Garry alright, there's a couple things that don't match up here
11/17/2007 2:02:29 PM Garry Daniel Schaffer I'm sure there's more than that too :P
11/17/2007 2:02:37 PM Daniel Schaffer Garry heh
11/17/2007 2:02:45 PM Daniel Schaffer Garry character Soldier = new character (400, 50, 50, 15, 's'); character Creature = new character (700, 75, 75, 100, 'c');
11/17/2007 2:03:03 PM Daniel Schaffer Garry you hard code the armor value there... so what's the point of calcArmor?
11/17/2007 2:03:21 PM Garry Daniel Schaffer I guess I should have clarified this at the beginning
11/17/2007 2:03:29 PM Garry Daniel Schaffer But I didn't expect to get this indepth with you
11/17/2007 2:03:50 PM Garry Daniel Schaffer The idea of the game is that you're basically the soldier - you select the armor/weapons for the soldier and you fight the creature
11/17/2007 2:03:51 PM Daniel Schaffer Garry heh its cool
11/17/2007 2:04:04 PM Garry Daniel Schaffer the Creature has a base value of 700 armor
11/17/2007 2:04:14 PM Daniel Schaffer Garry that's 700 hp
11/17/2007 2:04:17 PM Garry Daniel Schaffer oops
11/17/2007 2:04:22 PM Daniel Schaffer Garry armor is the 2nd to last parameter ;)
11/17/2007 2:04:41 PM Garry Daniel Schaffer base armor of 100, plus a random int of 1-50
11/17/2007 2:04:51 PM Daniel Schaffer Garry okay so calcarmor is the random int
11/17/2007 2:04:54 PM Garry Daniel Schaffer basically, the idea is to get some randomness to the game, as opposed to static boringness
11/17/2007 2:04:57 PM Daniel Schaffer Garry right
11/17/2007 2:05:22 PM Garry Daniel Schaffer so the creature always gets 100+(1-50)
11/17/2007 2:05:45 PM Garry Daniel Schaffer and the soldier gets 75+(armor type selected)
11/17/2007 2:05:57 PM Garry Daniel Schaffer even the armor type selected has randomness though
11/17/2007 2:06:06 PM Daniel Schaffer Garry ok
11/17/2007 2:06:26 PM Daniel Schaffer Garry i just don't see where the armor is taken into account at all though
11/17/2007 2:07:25 PM Daniel Schaffer Garry oh ok calcDefend
11/17/2007 2:07:36 PM Garry Daniel Schaffer yea
11/17/2007 2:08:02 PM Daniel Schaffer Garry OKAY
11/17/2007 2:08:03 PM Daniel Schaffer Garry heh
11/17/2007 2:08:30 PM Daniel Schaffer Garry so in the Character constructor, change this: arm = new armor();
11/17/2007 2:08:38 PM Daniel Schaffer Garry to this: arm = new Armor(cType);
11/17/2007 2:08:43 PM Garry Daniel Schaffer ahhhh
11/17/2007 2:08:54 PM Daniel Schaffer Garry and then in calcDefend()
11/17/2007 2:09:12 PM Daniel Schaffer Garry arm.calcArm changes to arm.Value
11/17/2007 2:09:24 PM Garry Daniel Schaffer ok
11/17/2007 2:09:30 PM Daniel Schaffer Garry or getValue() ...
11/17/2007 2:09:42 PM Daniel Schaffer Garry i'm not sure how java compiles properties
11/17/2007 2:09:43 PM Garry Daniel Schaffer no this.cType ?
11/17/2007 2:09:57 PM Garry Daniel Schaffer yea need that
11/17/2007 2:09:58 PM Garry Daniel Schaffer nm
11/17/2007 2:10:08 PM Daniel Schaffer Garry nope, because the Armor class already knows the cType because you passed it in as a parameter in the constructor
11/17/2007 2:10:12 PM Garry Daniel Schaffer oh, ok
11/17/2007 2:10:25 PM Daniel Schaffer Garry clearer now?
11/17/2007 2:10:45 PM Garry Daniel Schaffer no, it didn't like that
11/17/2007 2:10:52 PM Daniel Schaffer Garry what's the error?
11/17/2007 2:11:41 PM Garry sends C:\Documents and Settings\Garry\Desktop\errorsinJavaD&D.txt
11/17/2007 2:11:52 PM Transfer of "errorsinJavaD&D.txt" is complete.
11/17/2007 2:12:35 PM Daniel Schaffer Garry heh, you need to change wherever you say "character" to "Character"
11/17/2007 2:12:54 PM Garry Daniel Schaffer I did
11/17/2007 2:12:56 PM Garry Daniel Schaffer I think
11/17/2007 2:13:04 PM Daniel Schaffer Garry character Soldier = new character (400, 50, 50, 15, 's');
11/17/2007 2:13:24 PM Daniel Schaffer Garry i think you maybe need to rename the file too?
11/17/2007 2:13:41 PM Garry Daniel Schaffer did that, one sec
11/17/2007 2:14:19 PM Garry Daniel Schaffer ok now it's just
11/17/2007 2:14:39 PM Garry sends C:\Documents and Settings\Garry\Desktop\errorsinJavaD&D.txt
11/17/2007 2:14:52 PM Transfer of "errorsinJavaD&D.txt" is complete.
11/17/2007 2:15:17 PM Garry Daniel Schaffer it ran when I put "return armor + this.arm.getValue(cType);"
11/17/2007 2:15:25 PM Garry Daniel Schaffer instead of "return armor + this.arm.getValue();"
11/17/2007 2:15:31 PM Daniel Schaffer Garry really?
11/17/2007 2:15:38 PM Daniel Schaffer Garry what's the signature of the getValue method look like?
11/17/2007 2:16:00 PM Garry Daniel Schaffer private char characterType; public Armor(char cType){ this.characterType = cType; } public int getValue(char cType){
11/17/2007 2:16:05 PM Daniel Schaffer Garry ah
11/17/2007 2:16:17 PM Daniel Schaffer Garry it should just look like this: public int getValue() { }
11/17/2007 2:16:24 PM Daniel Schaffer Garry well with the code inside it of course
11/17/2007 2:16:36 PM Garry Daniel Schaffer :$
11/17/2007 2:16:48 PM Daniel Schaffer Garry public int getValue()
11/17/2007 2:16:53 PM Garry Daniel Schaffer but now...
11/17/2007 2:16:56 PM Daniel Schaffer Garry { // all your code }
11/17/2007 2:17:04 PM Garry Daniel Schaffer it can't find the variable cType
11/17/2007 2:17:12 PM Garry Daniel Schaffer do I need to define that as a public variable or something?
11/17/2007 2:17:25 PM Daniel Schaffer Garry replace that with characterType;
11/17/2007 2:17:42 PM Garry Daniel Schaffer so
11/17/2007 2:17:46 PM Garry Daniel Schaffer if (cType == 's') {
11/17/2007 2:17:54 PM Garry Daniel Schaffer if (characterType == 's' {
11/17/2007 2:17:54 PM Garry Daniel Schaffer ?
11/17/2007 2:17:56 PM Daniel Schaffer Garry right
11/17/2007 2:18:23 PM Daniel Schaffer Garry remember, characterType is a private member of Armor: private char characterType;
11/17/2007 2:18:33 PM Garry Daniel Schaffer yea I see
11/17/2007 2:19:13 PM Garry Daniel Schaffer appears to work!
11/17/2007 2:19:17 PM Daniel Schaffer Garry awesome
11/17/2007 2:21:10 PM Daniel Schaffer Garry hey, why do you have all that stuff in main() ?
11/17/2007 2:21:21 PM Garry Daniel Schaffer where?
11/17/2007 2:21:27 PM Garry Daniel Schaffer in the form?
11/17/2007 2:21:45 PM Daniel Schaffer Garry public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); int X = 0; //character Soldier = new character(1000,40,20,40,'k');//instance a Soldier //character Creature = new character(1000,40,20,40,'d');//instance a Creature //character(int hp, int stren, int dex, int armor,char cType) character Soldier = new character (400, 50, 50, 15, 's'); character Creature = new character (700, 75, 75, 100, 'c'); int turn = 0; int attack = 0; int defend = 0; int damage = 0; Random myRandom = new Random(); //make a random num gen
11/17/2007 2:21:46 PM Daniel Schaffer Garry etc
11/17/2007 2:22:00 PM Garry Daniel Schaffer I needed to put it somewhere :|
11/17/2007 2:22:02 PM Daniel Schaffer Garry main should just be used to launch the application
11/17/2007 2:22:09 PM Garry Daniel Schaffer yea, I know
11/17/2007 2:22:12 PM Daniel Schaffer Garry as it is, that code will only run once
11/17/2007 2:22:16 PM Garry Daniel Schaffer well, I DIDN'T know
11/17/2007 2:22:19 PM Daniel Schaffer Garry heh :D
11/17/2007 2:23:16 PM Garry Daniel Schaffer where should I put it now?
11/17/2007 2:23:30 PM Daniel Schaffer Garry well, are you going to have the player click a button between each round, or will it just go until someone's dead?
11/17/2007 2:24:14 PM Daniel Schaffer Garry i'd use a button ... otherwise you have to use a loop, and then worry about accidentally creating an infinite loop, etc
11/17/2007 2:24:54 PM Garry Daniel Schaffer hmm
11/17/2007 2:25:01 PM Daniel Schaffer Garry well
11/17/2007 2:25:03 PM Garry Daniel Schaffer the way it's set up now, it just creates a loop
11/17/2007 2:25:09 PM Garry Daniel Schaffer and goes untl someone dies
11/17/2007 2:25:16 PM Garry Daniel Schaffer but I'm tempted to change that, and I probably will use the button
11/17/2007 2:25:24 PM Daniel Schaffer Garry you could just do while(Solder.getHp() > 0 && Creature.getHp() > 0)
11/17/2007 2:25:41 PM Garry Daniel Schaffer I do, I think
11/17/2007 2:26:00 PM Daniel Schaffer Garry i'm not seeing how it loops
11/17/2007 2:26:10 PM Garry Daniel Schaffer while (Soldier.getHp() > 0 && Soldier.getHp() > 0){
11/17/2007 2:26:23 PM Garry Daniel Schaffer did you copy that from my code?
11/17/2007 2:26:25 PM Daniel Schaffer Garry oh
11/17/2007 2:26:27 PM Daniel Schaffer Garry no lol
11/17/2007 2:26:29 PM Daniel Schaffer Garry i see it now
11/17/2007 2:26:31 PM Garry Daniel Schaffer haha that's exactly what I have
11/17/2007 2:26:34 PM Daniel Schaffer Garry haha
11/17/2007 2:27:01 PM Daniel Schaffer Garry well i think that runs before the user ever does anything in the form though
11/17/2007 2:27:10 PM Garry Daniel Schaffer oh it does, and I will have to change that
11/17/2007 2:27:16 PM Daniel Schaffer Garry alright
11/17/2007 2:27:23 PM Garry Daniel Schaffer But first of all...I need to get the armor variable passed from the form
11/17/2007 2:27:29 PM Garry Daniel Schaffer which is how all this mess started
11/17/2007 2:27:34 PM Daniel Schaffer Garry haha right
11/17/2007 2:27:35 PM Daniel Schaffer Garry okay
11/17/2007 2:27:37 PM Garry Daniel Schaffer and weapon variable too, I suppose
11/17/2007 2:27:57 PM Daniel Schaffer Garry okay, so back to your event handler
11/17/2007 2:28:09 PM Daniel Schaffer Garry its in the NewJFrame class now right?
11/17/2007 2:29:13 PM Garry Daniel Schaffer yes
11/17/2007 2:29:17 PM Daniel Schaffer Garry good
11/17/2007 2:29:32 PM Daniel Schaffer Garry ok hold that though
11/17/2007 2:29:33 PM Daniel Schaffer Garry t
11/17/2007 2:30:21 PM Daniel Schaffer Garry before we do this, you'll need to move all that code in main() to a different method in NewJFrame
11/17/2007 2:30:38 PM Daniel Schaffer Garry you can call it something like DoCombat() or ExecuteCombat()
11/17/2007 2:30:47 PM Garry Daniel Schaffer ok cool
11/17/2007 2:30:50 PM Daniel Schaffer Garry also
11/17/2007 2:31:35 PM Daniel Schaffer Garry instead of declaring and instantiating the Soldier and Creature variables in the method, they should be members of NewJFrame
11/17/2007 2:32:17 PM Garry Daniel Schaffer oh yea, ok
11/17/2007 2:32:22 PM Garry Daniel Schaffer so I would do like
11/17/2007 2:32:26 PM Garry Daniel Schaffer Character Soldier = new Character (400, 50, 50, 15, 's'); Character Creature = new Character (700, 75, 75, 100, 'c');
11/17/2007 2:32:32 PM Garry Daniel Schaffer inside of....the main method?
11/17/2007 2:32:35 PM Daniel Schaffer Garry well don't instantiate them yet
11/17/2007 2:32:43 PM Daniel Schaffer Garry and no, not in the main method
11/17/2007 2:33:35 PM Daniel Schaffer Garry you should no longer be doing ANYTHING in the main method, except this: java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); int X = 0; // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JComboBox jComboBox1; private javax.swing.JComboBox jComboBox2; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTextArea1; private java.awt.Label label1; // End of variables declaration
11/17/2007 2:33:46 PM Daniel Schaffer Garry i'm guessing all that code was autogenerated by your IDE?
11/17/2007 2:33:54 PM Garry Daniel Schaffer yea
 
Four!

11/17/2007 2:34:08 PM Daniel Schaffer Garry ok... that all needs to stay, everything else should go into your new combat method
11/17/2007 2:35:33 PM Garry Daniel Schaffer it's asking for a return type....so I would return the results of the battle, yes?
11/17/2007 2:35:51 PM Daniel Schaffer Garry well no... it'll be void
11/17/2007 2:35:59 PM Daniel Schaffer Garry at least the way you have things set up now
11/17/2007 2:36:07 PM Garry Daniel Schaffer ah, genius
11/17/2007 2:36:12 PM Garry Daniel Schaffer yes, that makes sense
11/17/2007 2:36:33 PM Garry Daniel Schaffer so where do I instantiate the characters?
11/17/2007 2:37:02 PM Daniel Schaffer Garry i would do it in the handler for the "enter battle" button
11/17/2007 2:37:14 PM Garry Daniel Schaffer ah, good idea
11/17/2007 2:37:20 PM Daniel Schaffer Garry because at that point, the user will have selected both the armor and weapon types
11/17/2007 2:37:23 PM Garry Daniel Schaffer let me set up that form
11/17/2007 2:37:50 PM Daniel Schaffer Garry i've actually got to go now :( wife is yelling at me to help clean the house
11/17/2007 2:38:49 PM Daniel Schaffer Garry as far as passing the armor and weapon types, i'd add two more parameters to your Character class instantiator
11/17/2007 2:39:25 PM Daniel Schaffer Garry char aType, char wType
11/17/2007 2:39:44 PM Daniel Schaffer Garry then you can pass those off to your Armor and Weapon classes
11/17/2007 2:40:31 PM Daniel Schaffer Garry good luck... i should be on later today if you've got more questions :)
11/17/2007 2:40:40 PM Garry Daniel Schaffer ok, thanks so much!
11/17/2007 2:40:48 PM Daniel Schaffer Garry np, my pleasure :)
11/17/2007 10:36:48 PM Garry Daniel Schaffer hey, you gonna be around tomorrow?
11/18/2007 12:21:27 AM Daniel Schaffer Garry yeah
11/18/2007 1:11:43 PM Garry Daniel Schaffer hey!
11/18/2007 1:11:45 PM Daniel Schaffer Garry I am currently away from the computer.
11/18/2007 1:45:36 PM Daniel Schaffer Garry hey, what's up?
11/18/2007 1:45:47 PM Garry Daniel Schaffer issues
11/18/2007 1:45:51 PM Garry Daniel Schaffer several of them
11/18/2007 1:46:10 PM Garry Daniel Schaffer I have them typed up actually
11/18/2007 1:46:35 PM Garry Daniel Schaffer well actually, the main one I have an issue with isn't much - I just can't pass a variable from the form
11/18/2007 1:46:44 PM Garry Daniel Schaffer still...
11/18/2007 1:47:12 PM Daniel Schaffer Garry can't pass a variable from the form? what code are you using and what error?
11/18/2007 1:48:27 PM Garry Daniel Schaffer well, I tried adding "setSelArmor, setSelWeapon" to the Character instantiation like you suggested, but that didn't work
11/18/2007 1:49:09 PM Garry Daniel Schaffer I'm tring to retrieve the variable from getSelectedIndex
11/18/2007 1:49:11 PM Garry Daniel Schaffer on the form
11/18/2007 1:49:33 PM Daniel Schaffer Garry well those are set accessors
11/18/2007 1:52:02 PM Garry Daniel Schaffer I just don't know how I can get a variable from something someone does on the form
11/18/2007 1:52:35 PM Daniel Schaffer Garry hm, and that doesn't work?
11/18/2007 1:52:57 PM Garry Daniel Schaffer well I mean, it does within the context of the ActionPerformed event or whatever
11/18/2007 1:53:01 PM Daniel Schaffer Garry okay
11/18/2007 1:53:02 PM Garry Daniel Schaffer But I can't get that outside of it
11/18/2007 1:53:15 PM Garry Daniel Schaffer To another class or even outside of that event at all, even in the same class
11/18/2007 1:53:32 PM Daniel Schaffer Garry okay
11/18/2007 1:53:52 PM Daniel Schaffer Garry does the ActionEvent object have a "sender" property or something like that?
11/18/2007 1:54:05 PM Daniel Schaffer Garry something that would give you a reference to the object that caused the event?
11/18/2007 1:54:29 PM Garry Daniel Schaffer private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {
11/18/2007 1:54:30 PM Daniel Schaffer Garry i guess this is where java differs from C# and WinForms a bit
11/18/2007 1:54:43 PM Daniel Schaffer Garry yeah, what properties does the ActionEvent class have'
11/18/2007 1:55:27 PM Garry Daniel Schaffer jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select Weapon...", "Photon Pistol", "Freeze Ray", "Plasma Grenade Launcher" })); jComboBox1.setEnabled(false); jComboBox1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox1ActionPerformed(evt);
11/18/2007 1:55:28 PM Garry Daniel Schaffer ?
11/18/2007 1:55:31 PM Garry Daniel Schaffer O
11/18/2007 1:55:32 PM Garry Daniel Schaffer :-O
11/18/2007 1:55:53 PM Daniel Schaffer Garry that's not what i mean
11/18/2007 1:56:13 PM Daniel Schaffer Garry your event handler method takes an ActionClass object as a parameter
11/18/2007 1:56:17 PM Daniel Schaffer Garry what data is in that object?
11/18/2007 1:57:23 PM Garry Daniel Schaffer Not quite sure what you mean
11/18/2007 1:57:31 PM Daniel Schaffer Garry oh ok
11/18/2007 1:57:52 PM Daniel Schaffer Garry you can get the object that fired the event from the getSource() method
11/18/2007 1:58:21 PM Daniel Schaffer Garry so try adding this in your even handler:
11/18/2007 1:58:37 PM Daniel Schaffer Garry javax.swing.JComboBox sender = (javax.swing.JComboBox)evt.getSource();
11/18/2007 1:59:10 PM Garry Daniel Schaffer no errors...that's good
11/18/2007 1:59:19 PM Daniel Schaffer Garry okay, that should give you a good reference to the combo box that fired the event
11/18/2007 2:00:07 PM Garry Daniel Schaffer so how do I reference that?
11/18/2007 2:00:28 PM Daniel Schaffer Garry okay, do you understand what i did in the code i sent you?
11/18/2007 2:00:34 PM Garry Daniel Schaffer not 100%
11/18/2007 2:00:42 PM Daniel Schaffer Garry alright
11/18/2007 2:00:47 PM Daniel Schaffer Garry what don't you get?
11/18/2007 2:01:04 PM Garry Daniel Schaffer what is sender? And what does getSource() do?
11/18/2007 2:01:31 PM Daniel Schaffer Garry okay, when your combobox fires an event, there is data that does along with the event
11/18/2007 2:01:51 PM Daniel Schaffer Garry it has a lot of generic information, like what mouse buttons or keyboard keys were sent, as well as information about the state of the control
11/18/2007 2:02:17 PM Daniel Schaffer Garry it also tells you which control fired the event - that's getSource(), with the "source" being the source of the event itself
11/18/2007 2:02:48 PM Daniel Schaffer Garry to keep with the java naming conventions you should actually probably rename "sender" to "source", just to keep things uniform and less confusing
11/18/2007 2:03:18 PM Daniel Schaffer Garry but what you're doing there is giving assigning the reference to the event source to a variable
11/18/2007 2:03:35 PM Daniel Schaffer Garry so you can use it later on in the method
11/18/2007 2:04:59 PM Daniel Schaffer Garry btw, putting (javax.swing.JComboBox) before evt.getSource() is called casting - the getSource() method returns a object of the base type Object, so you have to cast the object to the JComboBox type in order to get access to the JComboBox class members
11/18/2007 2:05:43 PM Garry Daniel Schaffer alright
11/18/2007 2:06:22 PM Garry Daniel Schaffer so this will allow me to get access to a variable within private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {} ?
11/18/2007 2:07:04 PM Daniel Schaffer Garry well its creating a variable that is a reference to the source of the event
11/18/2007 2:07:31 PM Daniel Schaffer Garry so if jComboBox1 is the control that fired the event, the "source" variable will be a reference to that control.
11/18/2007 2:08:01 PM Garry Daniel Schaffer ok!
11/18/2007 2:09:39 PM Garry Daniel Schaffer but if I put the javax.swing.JComboBox source = (javax.swing.JComboBox)evt.getSource(); in the jComboBox1ActionPerformed handler...will that work?
11/18/2007 2:10:06 PM Daniel Schaffer Garry work for what?
11/18/2007 2:10:17 PM Daniel Schaffer Garry we're not done yet
11/18/2007 2:10:18 PM Garry Daniel Schaffer will I have access to the source variable?
11/18/2007 2:10:22 PM Daniel Schaffer Garry yes
11/18/2007 2:10:28 PM Daniel Schaffer Garry just in that method though
11/18/2007 2:10:43 PM Garry Daniel Schaffer ok
11/18/2007 2:16:58 PM Daniel Schaffer Garry okay, so next, you'll need to get a reference to the form itself
11/18/2007 2:17:38 PM Daniel Schaffer Garry because once you get the value from the combobox, you'll need to be able to pass it to your form
11/18/2007 2:17:55 PM Daniel Schaffer Garry see if you can figure out how
 
Five....

11/18/2007 2:18:35 PM Garry Daniel Schaffer it's funny, I like to think I'm good with google, I can usually find things on my own a lot of the time if I need to, but I never can with this stuff
11/18/2007 2:18:41 PM Daniel Schaffer Garry btw, its helpful to use the java reference. here's the page for the JComboBox control: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JComboBox.html
11/18/2007 2:23:33 PM Daniel Schaffer Garry here's a hint ... check out the methods that it inherits from the Component class
11/18/2007 2:23:49 PM Garry Daniel Schaffer I was about to say
11/18/2007 2:23:52 PM Garry Daniel Schaffer accessibleContext
11/18/2007 2:24:04 PM Garry Daniel Schaffer am I right?
11/18/2007 2:24:15 PM Daniel Schaffer Garry i'm actually not sure what that does, haha
11/18/2007 2:24:36 PM Garry Daniel Schaffer eventListener? :|
11/18/2007 2:25:21 PM Daniel Schaffer Garry you're trying to get a reference to your form
11/18/2007 2:25:26 PM Garry Daniel Schaffer ok
11/18/2007 2:25:42 PM Garry Daniel Schaffer ohhhh, wait
11/18/2007 2:25:43 PM Daniel Schaffer Garry the combobox is a child of the form, correct?
11/18/2007 2:25:45 PM Garry Daniel Schaffer I was looking at the wrong thing
11/18/2007 2:25:49 PM Garry Daniel Schaffer yea, I think so
11/18/2007 2:26:18 PM Daniel Schaffer Garry okay, so have a reference to the combobox... how might you get from the combobox to the form?
11/18/2007 2:26:39 PM Daniel Schaffer Garry if the combobox is a *child* of the form
11/18/2007 2:27:47 PM Garry Daniel Schaffer I see a "getParent" method...
11/18/2007 2:27:58 PM Daniel Schaffer Garry ok
11/18/2007 2:28:01 PM Garry Daniel Schaffer but that seems like the opposite of what I need
11/18/2007 2:28:05 PM Daniel Schaffer Garry why?
11/18/2007 2:28:19 PM Garry Daniel Schaffer well, maybe not
11/18/2007 2:29:07 PM Daniel Schaffer Garry so how might you use getParent() to get a reference to your form then
11/18/2007 2:29:31 PM Daniel Schaffer Garry note the type that getParent() returns
11/18/2007 2:31:55 PM Garry Daniel Schaffer that's a good question
11/18/2007 2:32:04 PM Garry Daniel Schaffer But I have no idea
11/18/2007 2:32:20 PM Garry Daniel Schaffer it says it returns what the parent is of the component
11/18/2007 2:32:26 PM Garry Daniel Schaffer I don't quite see how it helps
11/18/2007 2:33:34 PM Daniel Schaffer Garry okay, do you understand what the Component class is?
11/18/2007 2:37:52 PM Garry Daniel Schaffer not quite, no
11/18/2007 2:38:00 PM Garry Daniel Schaffer I mean, I'm reading about it now
11/18/2007 2:38:17 PM Daniel Schaffer Garry okay, well the Component class is a superclass of ComboBox
11/18/2007 2:38:25 PM Garry Daniel Schaffer ok
11/18/2007 2:39:26 PM Daniel Schaffer Garry its also the the superclass of lots of other controls... including JFrame
11/18/2007 2:39:37 PM Garry Daniel Schaffer right on
11/18/2007 2:40:18 PM Daniel Schaffer Garry so all of these control classes will have all of Component's members... including the getParent() method, which returns a Component object
11/18/2007 2:41:16 PM Daniel Schaffer Garry however, just because it returns a Component object doesn't mean that the object is JUST a Component... it could be any number of the other classes that extend Component.
11/18/2007 2:41:48 PM Daniel Schaffer Garry but since Component is effectively the lower common denominator, that's what it returns
11/18/2007 2:42:12 PM Garry Daniel Schaffer alright
11/18/2007 2:42:37 PM Daniel Schaffer Garry make sense?
11/18/2007 2:42:55 PM Garry Daniel Schaffer as much as it is going to right now at least
11/18/2007 2:43:21 PM Daniel Schaffer Garry alright well lets see if I can make it a little clearer
11/18/2007 2:43:47 PM Daniel Schaffer Garry because its is a fairly big OOP principle that will help you understand a lot of other things
11/18/2007 2:44:01 PM Daniel Schaffer Garry take a look at te hierarchy at the top of this page: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFrame.html
11/18/2007 2:44:29 PM Garry Daniel Schaffer so, java.lang.object is....
11/18/2007 2:44:36 PM Garry Daniel Schaffer the biggest of all superclasses?
11/18/2007 2:44:44 PM Daniel Schaffer Garry well i wouldn't say biggest
11/18/2007 2:44:51 PM Garry Daniel Schaffer oh, but it is one
11/18/2007 2:44:56 PM Daniel Schaffer Garry but EVERY class automatically inherits from Object
11/18/2007 2:45:03 PM Garry Daniel Schaffer neat
11/18/2007 2:45:14 PM Daniel Schaffer Garry even if you have a class that doesn't inherit from anything, it intrinsicly inherits from Object
11/18/2007 2:45:32 PM Daniel Schaffer Garry i should be saying "extends", not "inherits", sorry
11/18/2007 2:45:38 PM Garry Daniel Schaffer ah
11/18/2007 2:45:53 PM Daniel Schaffer Garry they mean the same thing, but "extends" in the Java word for it
11/18/2007 2:45:57 PM Daniel Schaffer Garry is **
11/18/2007 2:46:06 PM Garry Daniel Schaffer ok
11/18/2007 2:46:10 PM Daniel Schaffer Garry Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.
11/18/2007 2:46:18 PM Daniel Schaffer Garry ^^ from the documentation on the Object class
11/18/2007 2:47:47 PM Daniel Schaffer Garry so the hierarchy goes from less specific to more specific
11/18/2007 2:48:41 PM Garry Daniel Schaffer yea
11/18/2007 2:58:13 PM Daniel Schaffer Garry ok sorry, phone
11/18/2007 2:58:19 PM Garry Daniel Schaffer no problem
11/18/2007 2:58:44 PM Daniel Schaffer Garry alright, so each class that extends another class gets all the members of the superclass
11/18/2007 2:58:51 PM Garry Daniel Schaffer I think I might be done with this anyways. It's a group project my group is meeting up tonight to work on it and we'll decide what we're going to do
11/18/2007 2:59:23 PM Daniel Schaffer Garry so for example, the Component class has all the methods and properties of the Object class, the Container class has all the methods and properties of the Component class, and so on
11/18/2007 2:59:31 PM Daniel Schaffer Garry ah, alright
11/18/2007 2:59:37 PM Garry Daniel Schaffer Thanks so much for your help
11/18/2007 2:59:39 PM Daniel Schaffer Garry np
11/18/2007 2:59:42 PM Garry Daniel Schaffer I've definitely learned a lot these two days
11/18/2007 2:59:49 PM Garry Daniel Schaffer It'll come eventually!
11/18/2007 3:00:00 PM Garry Daniel Schaffer I'm just very young and in-experienced
11/18/2007 3:00:11 PM Daniel Schaffer Garry it will :) though I'm surpised your teacher isn't giving you more theory before throwing these projects at you
11/18/2007 3:00:27 PM Garry Daniel Schaffer he kind of is I guess, but I'm not sure he's teaching it the right way
11/18/2007 3:00:29 PM Daniel Schaffer Garry it seems like there's a lot he's not teaching you
11/18/2007 3:00:45 PM Garry Daniel Schaffer very likely
11/18/2007 3:01:06 PM Garry Daniel Schaffer and unfortunately I'm near the top of the class :|
11/18/2007 3:01:12 PM Daniel Schaffer Garry alright... well i'm glad I could help... i'm never sure if the way I explain things helps people
11/18/2007 3:01:32 PM Garry Daniel Schaffer oh it did, definitely
11/18/2007 3:01:36 PM Daniel Schaffer Garry well awesome
11/18/2007 3:01:49 PM Garry Daniel Schaffer well, I'm off...busy day today. Have a good one! Talk to you again sometime
11/18/2007 3:01:55 PM Daniel Schaffer Garry k, see ya

phew
 
oh god...i have a java final lab exam...i will definately have some questions so keep this thread open lol :D
 
wow we talked for a long ass time. D: no wonder my wife was getting pissed
 
Wow, why didn't i think about asking the internets about my Java problems last semester? I could of spent less time learning Java and more time having somebody tell me the answer.. :)

btw, cool project. In my class we had to write some boring robot simulation to navigate a maze.
 
Well, I tried to help him understand and not just throw code at him... I'm assuming that people take Java classes because they want to be some sort of computer programmer when they graduate, so it'd be in your best interest to learn the language. Maybe I should get a webcam or something and start giving online OOP classes or something:dork:
 
are you at MAC by any chance?

Nope, I was going to go to MAC for computer science but you needed to take 1 year of general sciences which is a huge waste of time and money, so I went to Brock for Computing and Network communications
 
Back
Top