Issues with JApplet and Text Fields
Hi, I'm trying to create a game in which a random song title comes up and the user will have to enter the correct singer. My issue right now is I'm having difficulty making it so that the user's input is checked and then the text field is reset. I'm not even sure if this is possible, because no matter how hard I've looked I haven't found anything. Thanks for any help you can give me! Here's my code so far:
Code :
package final_project;
import java.applet.Applet;
import java.awt.*; // import the java.awt package
import java.awt.event.*; // import the java.awt.event package
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JTextArea;
public class NameThatBand extends Applet implements ActionListener, MouseListener{
String song;
String[] songArray={"Let It Be","Where Is My Mind?", "Pet Sounds"};
JTextArea textArea;
TextField input;
Label prompt;
JButton button;
//The x and y coordinates of the last click:
int xpos;
int ypos;
//The coordinates of the rectangle that will need to be clicked:
int rect1xco,rect1yco,rect1width,rect1height;
//Test whether the click is within the applet area:
boolean mouseEntered;
//Test whether the click is within rect1:
boolean rect1Clicked;
int randomNum;
int score;
public void init(){
setSize(500,500);
prompt = new Label("Who Sings It?");
input = new TextField(5);
add(prompt); // put prompt on applet
add(input); // put input TextField on applet
input.addActionListener( this ); // "this" applet handles action events for TextField input
//Rectangle coordinates:
rect1xco = 0;
rect1yco = 0;
rect1width=1000;
rect1height=5000;
addMouseListener(this);
}
public void actionPerformed(ActionEvent e) {
input.equals((e.getActionCommand()));
String text = input.getText();
while(songArray[randomNum].equalsIgnoreCase("Where is my mind?")){
if (text.equalsIgnoreCase("The Pixies")){
score++;
input.setText("");
repaint();
actionPerformed(e);
}
else{
input.setText("");
repaint();
actionPerformed(e);
}
}
while(songArray[randomNum].equalsIgnoreCase("Pet Sounds")){
if (text.equalsIgnoreCase("The Beach Boys")){
score++;
input.setText("");
repaint();
actionPerformed(e);
}
else{
input.setText("");
repaint();
actionPerformed(e);
}
}
while(songArray[randomNum].equalsIgnoreCase("Let It Be")){
if (text.equalsIgnoreCase("The Beatles")){
score++;
input.setText("");
repaint();
actionPerformed(e);
}
else{
input.setText("");
repaint();
actionPerformed(e);
}
}
}
public void paint(Graphics g){
g.setColor(Color.white);
g.fillRect(rect1xco, rect1yco, rect1width, rect1height);
g.setColor(Color.black);
int minimum = 0;
int maximum = 3;
int randomNum = minimum +(int)(Math.random()*maximum);
if(rect1Clicked){
g.drawString(songArray[randomNum],20,140);
}
else {
g.drawString("To play, try to correctly guess the artist who sings the given song.", 20, 120);
g.drawString("Click anywhere to begin.", 20, 140);
}
if(mouseEntered){
repaint();
}
if(score==10){
g.drawString("Your Score = " + score, 20, 140);
}
}
public void mouseClicked(MouseEvent f){
xpos = f.getX();
ypos = f.getY();
if (xpos>rect1xco && xpos < rect1xco+rect1width && ypos >rect1yco &&
ypos<rect1yco+rect1height) rect1Clicked = true;
else
rect1Clicked=false;
repaint();
}
@Override
public void mouseEntered(MouseEvent e) { }
@Override
public void mouseExited(MouseEvent e) { }
@Override
public void mousePressed(MouseEvent e) { }
@Override
public void mouseReleased(MouseEvent e) { }
public static void main(String[] args){
}
}
Re: Issues with JApplet and Text Fields
Hi Do u Know U Did A Simple Mistake input.setText(""); so Always It Will Set To Empty I think So Just Try to give Some Values
Re: Issues with JApplet and Text Fields
Quote:
My issue right now is I'm having difficulty making it so that the user's input is checked and then the text field is reset.
I recommend boiling your code down to only the parts which involve your problem, post it as an SSCCE, and clearly define the behavior you want the behavior you have. Right now, at least to me, the problem/question is not clearly defined (define 'input is checked', define 'field is reset')
Re: Issues with JApplet and Text Fields
Quote:
Originally Posted by
copeg
I recommend boiling your code down to only the parts which involve your problem, post it as an SSCCE, and clearly define the behavior you want the behavior you have. Right now, at least to me, the problem/question is not clearly defined (define 'input is checked', define 'field is reset')
Okay, the main problem is within my actionPerformed() method. Ideally, I'd like to make it so that when the user enters text into the TextField, which I call input, and hits enter, the text entered will be compared to a certain value based upon the random location in the array. So, if the random location in the array is the String "Pet Sounds", the user will need to enter "Beach Boys" in order to get a point. After that, I'd like the TextField to reset while another section of the array is called upon. So here's the code for my actionPerformed method:
Code :
public void actionPerformed(ActionEvent e) {
input.equals((e.getActionCommand()));
String text = input.getText();
while(songArray[randomNum].equalsIgnoreCase("Where is my mind?")){
if (text.equalsIgnoreCase("The Pixies")){
score++;
input.setText("");
repaint();
actionPerformed(e);
}
else{
input.setText("");
repaint();
actionPerformed(e);
}
}
while(songArray[randomNum].equalsIgnoreCase("Pet Sounds")){
if (text.equalsIgnoreCase("The Beach Boys")){
score++;
input.setText("");
repaint();
actionPerformed(e);
}
else{
input.setText("");
repaint();
actionPerformed(e);
}
}
while(songArray[randomNum].equalsIgnoreCase("Let It Be")){
if (text.equalsIgnoreCase("The Beatles")){
score++;
input.setText("");
repaint();
actionPerformed(e);
}
else{
input.setText("");
repaint();
actionPerformed(e);
}
}
}
Thanks for any help you can provide, and let me know if you have any questions!
Re: Issues with JApplet and Text Fields
A few comments:
1) That is not an SSCCE, but suffices for the following comments
2) The first line in which you evaluate the action command does nothing
3) For each call to actionPerformed, you call the method again. Any reason why?
4) Why are you calling repaint for every evaluation?
5) Think about those while loops...are there cases in which their evaluation statements never evaluate to false (and hence be infinite)?
Re: Issues with JApplet and Text Fields
First off, thanks for your comments!
1) Yeah, truthfully I'm not really sure what I was supposed to give you, so I just tried making my problem a little more specific.
2) Hmm. So would that line be something I should just delete or is there something I'm missing. Do I need something there to make it so that hitting the ENTER key does something?
3) This was an attempt to reset the program so it would run again after one value was input, but obviously it doesn't work that way. I just didn't take it out like I should have.
4) Same as number 3.
5) Now that you mention it, it seems like one of my problems is that the randomNum does not change after the user inputs a String. So if the String in the array is "Pet Sounds", there's nothing that is changing that value. So I guess I need to make it so that when a user enters a String, the String is checked and then everything starts over with a new randomNum.
If you can't tell I'm having a little trouble working with the applet. It's something somewhat new to me so I've been toying with it a lot and putting in things that probably make no sense. So I really appreciate your help.
EDIT: I just changed the while loops to if elses. So now, the text box "resets" as in the user can enter a String into the TextField and when he/she hits ENTER, the text goes away and they have the ability to enter another String. So I think I made some progress here.
Re: Issues with JApplet and Text Fields
Quote:
Originally Posted by
schiefer740
So I think I made some progress here.
It sounds that way to me...glad to hear you are making progress. My biggest pieces of advice are to take things one step at a time and break the problem down to steps, even writing them on paper if you must to help you think more clearly and logically about a more complex problem; use System.out.println to help you understand the flow of the program (and debug); and read the tutorials at Oracle (for instance How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners) ). If you are still struggling, the post the updated code (see the link in my signature for how to make an SSCCE) with another question