Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A Multitude of questions

 
Matt Fyffe
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since it's summer and I'm milling away at trying to learn java, I have no where to ask for help but you guys. So lol, sorry about this mess but I've been working on an interface (trying to replicate the character creation screen from the game Fallout) and have encountered stuff I really have no idea how to do. Thus, I have turned here for my variety of questions, hoping that you guys will not have tired of my questions, and be able to answer my new round of questions.
Here I'll run down my list of questions (with the code to my miserable program below).
1. I've made my JSpinners and they change numbers. My changeListener finds out they change numbers and thus changes what the screen says. However, I don't understand how I can see what the number changed to. Reading SunJava's class listing, I saw the method getValue(). However, this returns an object. I tried casting this into an int but it didn't like me doing that. How can I get the new JSpinner value as an integer so I can compare it to its previous values?
1b. While on the topics of JSpinners, when using SpinnerNumberModel, I have 7 cases of the same exact SpinnerNumberModel, but I need to make a different variable for each. I thought I could use only one but when using one, it would change ALL JSpinner values. Is there a way to use only one that will save me several lines of code? Or does each JSpinner need to use its seperate one?
2. I want to add an image in the infoPanel in the bottom right corner of the screen (don't want it to be to boring to look at). However, my book that "teaches" me java doesn't seem to be very friendly when it comes to comprehending how this is done. The image is stored in the same file as the program as a .bmp file (can I use those?). How do I tell the program to take my pretty image and draw it at the desired location?
3. If you can read over my code and it's legible, is the layout I used okay? I wasn't sure if I was doing it right but this was the only way I could use layouts to make everything appear on the screen.
Thanks a lot for any input you can give me. I figure I will use this subject as my location for my questions now so that I don't appear on every forum asking every question that appears in my head. Thanks once more and if you have any problems reading my code, or suggestions outside of the problems I already brought up, feel free to tell me. Apologies for its length... I did not realize its size until after I posted.

[ July 03, 2003: Message edited by: Matt Fyffe ]
[ July 03, 2003: Message edited by: Matt Fyffe ]
 
James Clinton
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer to q1.
Cast the value to a Integer obj, then use getInt();
int i = ((Integer)xx.getValue()).intValue();
// off the top of my head its something like that.
/ James
 
James Clinton
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops typo. getInt() should be intValue().
Basically follow the example code.
 
James Clinton
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
q 1b
You have NOT made 7 cases of the same spinner model.
They may have the same params but they are all different. Dont be fooled by thinking that just because you have created identical objects that they are one. They all have different memory addresses underneath and thats the important thing to understand.
If you want to change them all then you'll need to store the references in a list after you have created them.
vector.addElement(model1);
vector.addElement(model2);
Then have a method called changeAll() and loop thru your list and do xyz.


/ James
 
James Clinton
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code Layout.
Seems fine. However the main method usually lives at the bottom.
 
Matt Fyffe
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James, I used the code
int i = ((Integer)STR.getValue()).getInt();
but I get an error when I compile saying cannot resolve symbol. I also tried the .intValue instead of getInt and that compiled. However, when it ran with that line of code, another window popped up besides my own spitting out information. The window is labeled a Terminal Window and it spits out a bunch of data at the bottom. When I use .intValue though, it does read the integer in (I have a JOptionPane tell me its value).
Also, something odd seems to happen. I tried to run my program through MS-DosPrompt instead of Bluej. When I did I got an error stating "Exception in thread "main" java.lang.NoSuchMethodError: main". Now I'm not an expert programmer... but I dont think thats good :-\
[ July 04, 2003: Message edited by: Matt Fyffe ]
[ July 04, 2003: Message edited by: Matt Fyffe ]
 
peter greaves
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

should be

the JVM looks for a method with that sig.
peter
 
James Chegwidden
Author
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your import statements are wrong:

Event handling methods are found in
java.awt.event.*;
Also, why do you have an init() method with a main(). Is this and applet or an application?
[ July 04, 2003: Message edited by: James Chegwidden ]
 
Matt Fyffe
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Peter, I'm not sure why I left String[] args out... Usually I have it in there. Thanks, it now works .
James, I THINK I'm trying to make an application. We always made applets in school and I was trying to expand and make an application. And the reason I import javax.swing.event.* and not the java.awt one is because ChangeListener is in the swing event library not the awt library.
Alright... in the same vein of problems I'm having converting the object to an int when I read JSpinners value, how can I convert an int into an object to set its value? Lol, I hate these JSpinners
[ July 04, 2003: Message edited by: Matt Fyffe ]
 
Matt Fyffe
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, the entire screen is complete for the most part now, however I have two problems still and one question.
First off, the JSpinners are still given me a lotta hassle. The problem is the same as I posted in a previous post. I can't read the value on the JSpinner, nor can I set the value of the JSpinner.
My question is about images. I made it so that when the player modifies one of his stats, an image and information appears in the bottom right explaining the stat he changed. This works fine but for the location of my image I had to write out the full address icon = new ImageIcon("C:/programming/RPG/charisma.jpg");
That is in the same folder as my program is. When I tried to bring it down to just charisma.jpg it would compile but would not show the picture. Is there a way I can tell the prog it is in the same folder as the code itself? (Note: this isn't a problem for me myself, but if I want to send my friend the prog he won't see the pictures :-\).
Thanks again for all the help you guys have given me... the prog wouldn't have been the same without you .
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Matt Fyffe:
Is there a way I can tell the prog it is in the same folder as the code itself?

Yes. Take a look at ClassLoader#getSystemResource.
 
Matt Fyffe
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hrmm... When you phrase it like that... I think I'll just leave it as the direct file.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Matt Fyffe:
Hrmm... When you phrase it like that... I think I'll just leave it as the direct file.

Huh? Why?
 
Matt Fyffe
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've never heard of ClassLoader before and it's not covered in my book :-\. I figure if I do implement that, I'll save it for the end. I hope you didn't take it as me being an ass lol, just sounded to complicated for me when I looked it up.
 
Matt Fyffe
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My next area of intrigue (besides the ever plaguing JSpinners) is closing a window on command. In the program I made (for better or worse) when you click on some buttons, I have the program bring you to an entirely new window. However, when it brings the user to this entirely new window, the one the person just left from is still up. Is there a way I can tell the class to end itself?
Example: User is at the main menu and clicks new game. This loads up a new window where the player can create his character. However, the main menu screen is still up. How can I make it so when the new window is created, the old one closes?
I tried using setDefaultCloseOperation(EXIT_ON_CLOSE); but that closed all my windows, not just the selected one. I have a feeling this is because that closes the program, not just the one part.
Thanks for any help you can give.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Matt Fyffe:
I've never heard of ClassLoader before and it's not covered in my book :-\. I figure if I do implement that, I'll save it for the end. I hope you didn't take it as me being an ass lol, just sounded to complicated for me when I looked it up.

It's not complicated. Just try
new ImageIcon(ClassLoader.getSystemResource("charisma.jpg"));
and put charisma.jpg into the classpath.
 
Matt Fyffe
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
o lol, I didn't realize it was that simple . Thanks a lot.
How do I add something to the classpath? If you mean put it in the same folder, I guess I've got that done... Using the code it compiles fine, however when I run it tells me nullPointer Exception and that it is currently null?
[ July 09, 2003: Message edited by: Matt Fyffe ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic