Results 1 to 16 of 16
- 11-25-2011, 04:03 PM #1
Member
- Join Date
- Nov 2011
- Location
- New Hampshire
- Posts
- 29
- Rep Power
- 0
Java word scramble -Trouble reading input
Hey guys, I've been working on this word scramble game for days. I've read the entire book the professor assigned (Object Oriented Programming in Java) and searched the internet for a while. This is actually the third java forum that I've joined!
Alright here it is. I need to read from a text box and depending on what the person types in do different actions. However I cannot get my text box input to register.
The textinputbox i'm using extends inputbox and the only method it has is getValue which is a string. I don't have any of the key events listed yet because I don't have any variables to work with. I've tried assigned getValue to a string and scanning the string, but that doesn't seem to work. I've tried switching over to Jframe and using all of the Jtexfields and all that, but for some reason Vectors won't work. There may be some conflict in one of the packages but I can't find it.
The file it's reading is a huge list of words and hints seperated by a colon and randomly shuffled through another class. If you would like that class as well just let me know.
Here is my code:
Thanks in advanced guys!Java Code:import wheelsunh.users.*; import java.awt.event.*; import java.util.*; import java.io.*; public class WordApp extends Frame implements KeyListener, Animator { private AnimationTimer timer; private int seconds = 120; private String scrambled, current, word, hint; private TextBox textBox, hintBox; private int score = 0; private TextInputBox inputRow; private Vector <String> v_words, v_hints, phrases; public WordApp() { inputRow = new TextInputBox(); inputRow.setLocation(200,100); phrases = new Vector <String>(FileUtilities.getStrings()); v_hints = new Vector<String>(); v_words = new Vector<String>(); categorizeStrings(); scrambledWord(word); textBox = new TextBox(200,100); textBox.setLocation(200,200); textBox.setText(scrambled + "\n\n Time Left: \t" + seconds + "\n Score: \t" + score); hintBox = new TextBox(200,200); hintBox.setLocation(300,400); hintBox.setText("Hint: " + hint); hintBox.hide(); } public void categorizeStrings() { Iterator <String> categorize = phrases.iterator(); while(categorize.hasNext()) { String element = categorize.next(); int index = element.indexOf(":"); v_words.add(element.substring(0,index)); v_hints.add(element.substring(index +1)); } Iterator <String> words = v_words.iterator(); word = words.next(); Iterator <String> hints = v_hints.iterator(); hint = hints.next(); } public String scrambledWord(String something) { java.util.Random r; scrambled = ""; for(int i = 0; i<word.length(); i++) { r = new java.util.Random(); if(r.nextBoolean()) scrambled = scrambled + word.charAt(i); else scrambled = word.charAt(i) + scrambled; } return scrambled; } public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { } public void keyReleased(KeyEvent e) { } public void animate() { seconds -=10; } public static void main (String[] args) { WordApp app = new WordApp(); } }Last edited by Fubarable; 11-25-2011 at 06:55 PM. Reason: code tags added
- 11-25-2011, 07:42 PM #2
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
Re: Java word scramble -Trouble reading input
We don't have enough with this code I think. What is for example a TextBox and a TextInputBox. Why don't you use SWING instead of AWT, with a JTextField?
- 11-25-2011, 08:18 PM #3
Re: Java word scramble -Trouble reading input
You are using third party classes that not everyone has. If you were to use the java SE classes more people could help you with your code.import wheelsunh.users.*;
- 11-25-2011, 08:22 PM #4
Member
- Join Date
- Nov 2011
- Location
- New Hampshire
- Posts
- 29
- Rep Power
- 0
Re: Java word scramble -Trouble reading input
Thanks for responding.
Like I said, I tried using swing instead of awt but for some reason I get errors with my vectors. And I don't know how to expand the height of the text fields. If I could correct the error with the vectors and find a way to change the height of the text fields I would go that route.
As for the text boxes, they are extensions of rectangle that have default border width and default sizes as well as a protected text field. They have a few accessor and mutator methods related to those fields as well.
At any rate, I don't need to use a text input box if there is a simpler way to go about being able to type information to the frame and have those characters be visible and be able to write some loops regarding what can go in and what happens when certain characters are typed.
This all just needs to happen in a frame, it doesn't need to look fancy or anything it just needs to work.
Thanks again for responding so quickly!
- 11-25-2011, 08:26 PM #5
Re: Java word scramble -Trouble reading input
Please post the full text of the error messages here.I get errors with my vectors
Why do you need to change the height of a text field?change the height of the text fields I would go that route.
Can you post code that compiles with java SE classes that shows your problem?
- 11-25-2011, 08:34 PM #6
Member
- Join Date
- Nov 2011
- Location
- New Hampshire
- Posts
- 29
- Rep Power
- 0
Re: Java word scramble -Trouble reading input
Well I've been working on two versions of the same program, one that extends frame and one that extends jframe. I have changed the code around on both of them so much that I can't really put the original error message down for the vector thing. I can't even get it to come up again because I've created a gui that handles half of the stuff and a few classes that handle the other half.
Any way, to answer your question about the text fields. The book I'm reading created a LabeledTexBox class that I've been using on the Jframe program. It extends Jpanel but it's very thin and doesn't extend depending on the information it has. I have tried using other objects like textfields but I can't get them to become larger. The only option is to make the character size different. I can e-mail you what I have so far if anyone wants to look at the entirety of the program and run it themselves.
- 11-25-2011, 08:41 PM #7
Re: Java word scramble -Trouble reading input
No need to email the program. Can you make a very small simple version with code that compiles, executes and demonstrates your problem and post that?
- 11-25-2011, 09:37 PM #8
Member
- Join Date
- Nov 2011
- Location
- New Hampshire
- Posts
- 29
- Rep Power
- 0
Re: Java word scramble -Trouble reading input
Sorry, I've been trying to make a small version but I can't seem to do it. I feel like it needs all the information I have. It's like I'm a mother hen and can't give up any eggs.
Could you post a sample code of a textfield that is editable and has actions dependent on certain characters?
- 11-25-2011, 09:41 PM #9
Re: Java word scramble -Trouble reading input
No hurry, take your time.
What do the actions have to do with the height of the textfield?Last edited by Norm; 11-25-2011 at 09:44 PM.
- 11-25-2011, 09:43 PM #10
Member
- Join Date
- Nov 2011
- Location
- New Hampshire
- Posts
- 29
- Rep Power
- 0
Re: Java word scramble -Trouble reading input
Ok nevermind, I've decided to take out everything and just show the exact problem I have.
From this you can see that I need to set the height of the textfield so that the entire string can be displayed. Otherwise you need to scroll through it yourself.Java Code:import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class Main { public static void main(String[] args) { JTextField tf = new JTextField("The approximate value of pi is: 3.1415695"); tf.setPreferredSize(tf.getPreferredSize()); JPanel pHacked = new JPanel(); pHacked.add(tf); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new java.awt.GridLayout(5, 5)); frame.add(pHacked); frame.setSize(200,200); frame.setVisible(true); tf.requestFocus(); } }Last edited by Norm; 11-25-2011 at 09:45 PM. Reason: added code tags
- 11-25-2011, 09:47 PM #11
Re: Java word scramble -Trouble reading input
The text field is of finite length. You can always add a String too long.
- 11-25-2011, 09:59 PM #12
Member
- Join Date
- Nov 2011
- Location
- New Hampshire
- Posts
- 29
- Rep Power
- 0
Re: Java word scramble -Trouble reading input
So is there any way to wrap the text to increase the height? I just want it to be like the text box we are typing in here. Something that you can see the entire
- 11-25-2011, 10:06 PM #13
Re: Java word scramble -Trouble reading input
Use a JTextArea in a JScrollPane.any way to wrap the text
If you want the text to wrap to a new line, you could add a newline character where you want it to go to the next line.
Or you could use a JTextPane which will accept some HTML tags and will wrap the text.
- 11-26-2011, 04:45 PM #14
Member
- Join Date
- Nov 2011
- Location
- New Hampshire
- Posts
- 29
- Rep Power
- 0
Re: Java word scramble -Trouble reading input
Thanks for the help Norm.
One last quick question. I'm trying to have the "enter" button register to the keytyped event. How would I put that in?
The end result I'm looking for is the word I type in needs to be cleared when the enter button is hit. Also, is there a way for the "backspace" button or "delete" button to register as well?
Thanks again for the help.
- 11-26-2011, 04:50 PM #15
Re: Java word scramble -Trouble reading input
Have you tried printing out the events received by the keyPressed listener to see what events that method receives?have the "enter" button register to the keytyped event.
- 11-26-2011, 07:09 PM #16
Member
- Join Date
- Nov 2011
- Location
- New Hampshire
- Posts
- 29
- Rep Power
- 0
Similar Threads
-
String builder scramble word game
By moncur in forum New To JavaReplies: 4Last Post: 10-22-2010, 03:14 AM -
Scramble Java Game
By equinox55 in forum New To JavaReplies: 3Last Post: 10-21-2010, 11:30 PM -
reading tables word document
By ashik03 in forum Advanced JavaReplies: 1Last Post: 02-06-2010, 01:01 AM -
Reading Microsoft Word Document in JAVA
By satheeshtech in forum Advanced JavaReplies: 0Last Post: 07-18-2009, 08:21 AM -
Word Scramble
By lk9865 in forum New To JavaReplies: 5Last Post: 11-17-2007, 02:22 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks