Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 446
  • Last Modified:

ActionListener & Integer.parseInt Question

Hello experts,

I made a simple applet just to learn but ran into a problem. I commented out the parts that fails the action listener for the textboxes. What I had in mind was for a person to enter in values and then set two variables equal them and then once they click the buton it would repaint the images in a new location. Also the compiler complained about the use of :
x = Integer.parseInt( textField1.getText() );
Why? Thanks for the help


import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class CarButton extends Applet
{
     //use variable to move car around
     int x = 0;
     int y = 0;

     String BUTTON_STRING = "Move Car";
     private Button button = new Button(BUTTON_STRING);
     private int count = 0;

     public void init()
     {
          //Create user input boxes
          Label labelOne = new Label( "Enter x value" );
          TextField textField1 = new TextField(2);
          textField1.setText( "" );
          //textField1.addActionListener( new TextHandler1() );

          Label labelTwo = new Label( "Enter Y value" );
          TextField textField2 = new TextField(2);
          textField2.setText( "" );
          //textField2.addActionListener( new TextHandler2() );

          //Create button
          button.addActionListener( new ButtonHandler() );

          add( labelOne );
          add( textField1);
          add( labelTwo );
          add( textField2);
          add( button );
     }

     //public class TextHandler1 implements ActionListener
     //{
          //x = Integer.parseInt( textField1.getText() );
     //}
         
     //public class TextHandler2 implements ActionListener
     //{
          //y = Integer.parseInt( textField2.getText() );    
     //}

     public class ButtonHandler implements ActionListener
     {
          public void actionPerformed( ActionEvent e )
          {
               repaint();
          }
     }

     public void paint (Graphics g)
     {
          //Begin to draw car image.
          g.setColor(Color.black);
          //Strait verticle lines top down
          g.drawLine(60+x,70+y, 120+x,70+y);
          g.drawLine(65+x,75+y, 120+x,75+y);
          g.drawLine(10+x,100+y, 45+x,100+y);
          g.drawLine(50+x,100+y, 135+x,100+y);
          g.drawLine(5+x,130+y, 20+x,130+y);
          g.drawLine(50+x,130+y, 150+x,130+y);
          g.drawLine(180+x,130+y, 190+x,130+y);

          //Horizontal lines
          g.drawLine(90+x,70+y, 90+x,130+y);
          g.drawLine(140+x,100+y, 140+x,130+y);

          //Slanted lines
          g.drawLine(10+x,100+y, 5+x,130+y);
          g.drawLine(60+x,70+y, 45+x,100+y);
          g.drawLine(65+x,75+y, 50+x,100+y);
          g.drawLine(120+x,75+y, 135+x,100+y);
          g.drawLine(120+x,70+y, 140+x,100+y);
          g.drawLine(140+x,100+y, 180+x,105+y);
          g.drawLine(180+x,105+y, 190+x,130+y);
         
          //Circles for the car wheels
          g.drawOval(20+x,115+y, 30,30);
          g.drawOval(150+x,115+y, 30,30);
     }
}
0
MrError
Asked:
MrError
  • 5
  • 4
  • 2
  • +1
1 Solution
 
kanthonymCommented:
You may want to beging by doing a Trim() in the input from the text box. This will reduce the chance of bad data going to the ParseInt method which will throw and exception.  This is not likley to be the solution to your question but could help avoid another issue.

textField1.getText().trim()

BTW -  Are you including the java.lang.Integer although the parseint is a static method and it should not be required.
0
 
pellepCommented:
parseInt() throws NumberFormatException. You need to enclose it in a try/catch block.
0
 
pellepCommented:
parseInt() throws NumberFormatException. You need to enclose it in a try/catch block.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
MrErrorAuthor Commented:
OK pellep & kanthonym

I copied part of the code to show you what I tried...
Here is the errors I get when compiling:

C:\Inetpub\wwwroot\ObjClass\assignment1>javac CarButton.java
CarButton.java:45: illegal start of type
                try
                ^
CarButton.java:52: <identifier> expected
                }
                 ^
CarButton.java:43: CarButton.TextHandler1 should be declared abstract; it does n
ot define actionPerformed(java.awt.event.ActionEvent) in CarButton.TextHandler1
        public class TextHandler1 implements ActionListener
               ^
3 errors

Here is the code that I modified:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class CarButton extends Applet
{
     //use variable to move car around
     int x = 0;
     int y = 0;

     String BUTTON_STRING = "Move Car";
     private Button button = new Button(BUTTON_STRING);
     private int count = 0;

     public void init()
     {
          //Create user input boxes
          Label labelOne = new Label( "Enter x value" );
          TextField textField1 = new TextField(2);
          textField1.setText( "" );
     textField1.addActionListener( new TextHandler1() );

          Label labelTwo = new Label( "Enter Y value" );
          TextField textField2 = new TextField(2);
          textField2.setText( "" );
          //textField2.addActionListener( new TextHandler2() );

          //Create button
          button.addActionListener( new ButtonHandler() );
          add( labelOne );
          add( textField1);
          add( labelTwo );
          add( textField2);
          add( button );
     }

     public class TextHandler1 implements ActionListener
     {
          try
          {
               x = Integer.parseInt( textField1.getText().trim() );
          }
          catch (NumberFormatException e)
          {
               e.printStackTrace();
          }
     }
0
 
MrErrorAuthor Commented:
Well OK I got this to compile now but it does not seem to move my Car image... Any Ideas?

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class CarButton extends Applet implements ActionListener
{
     //use variable to move car around
     int x = 0;
     int y = 0;

     //Create label and text boxes
     TextField textField1 = new TextField(2);
     TextField textField2 = new TextField(2);
     Label labelOne = new Label( "Enter x value" );
     Label labelTwo = new Label( "Enter Y value" );
     String BUTTON_STRING = "Move Car";
     private Button button = new Button(BUTTON_STRING);

     public void init()
     {
          //Create user input boxes
          textField1.setText( "" );
          textField1.addActionListener( this );

          textField2.setText( "" );
          textField2.addActionListener( this );

          //Create button
          button.addActionListener( new ButtonHandler() );

          add( labelOne );
          add( textField1);
          add( labelTwo );
          add( textField2);
          add( button );
     }

     public void actionPerformed (ActionEvent event)
     {
          x = Integer.parseInt( textField1.getText() );
          y = Integer.parseInt( textField2.getText() );
     }

     public class ButtonHandler implements ActionListener
     {
          public void actionPerformed( ActionEvent e )
          {
               repaint();
          }
     }

     public void paint (Graphics g)
     {
          //Begin to draw car image.
          g.setColor(Color.red);
          //Strait verticle lines top down
          g.drawLine(60+x,70+y, 120+x,70+y);
          g.drawLine(65+x,75+y, 120+x,75+y);
          g.drawLine(10+x,100+y, 45+x,100+y);
          g.drawLine(50+x,100+y, 135+x,100+y);
          g.drawLine(5+x,130+y, 20+x,130+y);
          g.drawLine(50+x,130+y, 150+x,130+y);
          g.drawLine(180+x,130+y, 190+x,130+y);
          //Horizontal lines
          g.drawLine(90+x,70+y, 90+x,130+y);
          g.drawLine(140+x,100+y, 140+x,130+y);
          //Slanted lines
          g.drawLine(10+x,100+y, 5+x,130+y);
          g.drawLine(60+x,70+y, 45+x,100+y);
          g.drawLine(65+x,75+y, 50+x,100+y);
          g.drawLine(120+x,75+y, 135+x,100+y);
          g.drawLine(120+x,70+y, 140+x,100+y);
          g.drawLine(140+x,100+y, 180+x,105+y);
          g.drawLine(180+x,105+y, 190+x,130+y);
         
          //Circles for the car wheels
          g.drawOval(20+x,115+y, 30,30);
          g.drawOval(150+x,115+y, 30,30);
     }
}
0
 
objectsCommented:
The action listener on your text field is only invoked when you hit <enter> after entering the value.
If you don't hit enter it is never called.
In addition the way you've currently got it the button is not really required you could just have the label action listeners call repaint:

    public void actionPerformed (ActionEvent event)
    {
         x = Integer.parseInt( textField1.getText() );
         y = Integer.parseInt( textField2.getText() );
         repaint();
    }

This sill requires <enter> to be pressed in your text field.

Alternatively, you could remove the action listeners from your text fields and instead use the above actionPerformed method as your button listener.
This no longer requires <enter> to be pressed in the text field, but instead requires the button to be pressed to move the image.
0
 
MrErrorAuthor Commented:
Objects...

Points for you, Can you help me with one last idea. I want to set the color value using a text box. So I have these elements to deal with:

Label labelThree = new Label( "Enter Color" );
TextField textField3 = new TextField(5);

z = textField3.getString(); <--- how to get the value?
g.setColor(Color.z);  <--- I know that is wrong.


Thanks everyone!!!
0
 
objectsCommented:
The easiest way is to have them enter the colour as a six digit hex value (the same as done in html). Where the value represents rrggbb.

Label labelThree = new Label( "Enter Color" );
TextField textField3 = new TextField(5);

z = textField3.getString(); <--- rrggbb value
int hex = Integer.parseInt(z, 16);
g.setColor(new Color(hex));  <--- I know this works :)

Alternatively if you want to use colour names, then you'll need to define your own lut of colour names, and corresponing colout.


0
 
MrErrorAuthor Commented:
Hex values sound fine to me...

C:\Inetpub\wwwroot\ObjClass\assignment1>javac CarButton.java
CarButton.java:65: cannot resolve symbol
symbol  : method getString  ()
location: class java.awt.TextField
                        z = textField3.getString();
                                      ^
CarButton.java:66: cannot resolve symbol
symbol  : method parseInt  (int,int)
location: class java.lang.Integer
                        hex = Integer.parseInt(z, 16);
                                     ^
2 errors


int z = 0;
int hex = 0;


     public class ButtonHandler implements ActionListener
     {
          public void actionPerformed( ActionEvent e )
          {
               x = Integer.parseInt( textField1.getText() );
                  y = Integer.parseInt( textField2.getText() );
               z = textField3.getString();
               hex = Integer.parseInt(z, 16);
                  repaint();
          }
     }

     public void paint (Graphics g)
     {
         
          g.setColor(Color.green);

          if ( hex != 0 )
          {
               g.setColor(new Color(hex));  
          }
0
 
objectsCommented:
Sorry, I just cut&pasted your code:

String rrggbb = textField3.getText(); <--- rrggbb value
int hex = Integer.parseInt(rrggbb, 16);


0
 
MrErrorAuthor Commented:
Thanks...

You helped a lot.
0
 
objectsCommented:
no worries.
thanks for the points :)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now