0

im new programming, im trying to get some values from jtextfield and then convert them into int to use them as a paramter to draw a polygon, but im having trouble converting the strings into int, please help me, what is the best way t do this this is my code:

this is where im getting the data from textfield

import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;


public class Transformacion extends JFrame {


    Transformacion_R2 grafico= new Transformacion_R2();


    public Transformacion()
       {  
           // Use helper methods
        createpuntosx();
        createpuntosy();
        createButton();

          xa=puntoFieldx1.getText();
          xb=puntoFieldx2.getText();
          xc=puntoFieldx3.getText(); 
          xd=puntoFieldx4.getText();


          createPanel();

          setSize(FRAME_WIDTH, FRAME_HEIGHT);
       }

    public void createButton()
       {  
          graficar = new JButton("Grafica");

          class PoligonoListener implements ActionListener
          {
             public void actionPerformed(ActionEvent event)
             {
                  JFrame frame = new JFrame(Transformacion_R2.TITLE);
                  frame.setContentPane(new Transformacion_R2());
                  frame.pack();
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  frame.setLocationRelativeTo(null);
                  frame.setVisible(true);
             }            
          }

          ActionListener listener = new PoligonoListener();
          graficar.addActionListener(listener);
       }


    public void createpuntosx()
       {
          puntosx = new JLabel("Puntos x: ");
          final int FIELD_WIDTH = 4;
          puntoFieldx1 = new JTextField(FIELD_WIDTH);
          puntoFieldx2 = new JTextField(FIELD_WIDTH);
          puntoFieldx3 = new JTextField(FIELD_WIDTH);
          puntoFieldx4 = new JTextField(FIELD_WIDTH);
       }

       public void createpuntosy()
       {
          puntosy = new JLabel("Puntos y: ");
          final int FIELD_WIDTH = 4;
          puntoFieldy1 = new JTextField(FIELD_WIDTH);
          puntoFieldy2 = new JTextField(FIELD_WIDTH);
          puntoFieldy3 = new JTextField(FIELD_WIDTH);
          puntoFieldy4 = new JTextField(FIELD_WIDTH);
       }


       public void createPanel()
       {
          JPanel northPanel = new JPanel();
          northPanel.add(puntosx);
          northPanel.add(puntoFieldx1);       
          northPanel.add(puntoFieldx2);
          northPanel.add(puntoFieldx3);
          northPanel.add(puntoFieldx4);
          northPanel.add(puntosy);
          northPanel.add(puntoFieldy1);
          northPanel.add(puntoFieldy2);
          northPanel.add(puntoFieldy3);
          northPanel.add(puntoFieldy4);

          add(northPanel,BorderLayout.NORTH);

          JPanel centerPanel = new JPanel();
          add(centerPanel,BorderLayout.CENTER);


          JPanel southPanel = new JPanel();
          southPanel.add(graficar);
          add(southPanel,BorderLayout.SOUTH);




       }


       private JLabel puntosx;
       private JTextField puntoFieldx1;
       private JTextField puntoFieldx2;
       private JTextField puntoFieldx3;
       private JTextField puntoFieldx4;
       private JLabel puntosy;
       private JTextField puntoFieldy1;
       private JTextField puntoFieldy2;
       private JTextField puntoFieldy3;
       private JTextField puntoFieldy4;



       private JButton graficar;
       public static String xa;
       public static String xb;
       public static String xc;
       public static String xd;
       public static String ya;
       public static String yb;
       public static String yc;
       public static String yd;
       private static final int FRAME_WIDTH = 800;
       private static final int FRAME_HEIGHT = 200;

and this is where i'm converting them into string

import java.awt.*;
import java.awt.geom.AffineTransform;
import javax.swing.*;

/** Test applying affine transform on vector graphics */
@SuppressWarnings("serial")



public class Transformacion_R2 extends JPanel{

   public static final int CANVAS_WIDTH = 1000;
   public static final int CANVAS_HEIGHT = 1200;
   public static final String TITLE = "Tranformacion Lineal R2";

//DECLARO PUNTOS DEL POLIGONO
   int a=Integer.parseInt (Transformacion.xa);
   int b=Integer.parseInt (Transformacion.xb);
   int c=Integer.parseInt (Transformacion.xc);
   int d=Integer.parseInt (Transformacion.xd);
   int[] polygonXs = { a, b, c, d};
   int[] polygonYs = { 80, 70, 80, 40};
   Shape shape = new Polygon(polygonXs, polygonYs, polygonXs.length);


   /** Constructor to set up the GUI components */

   public Transformacion_R2() {
      setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
   }

   /** Custom painting codes on this JPanel */
   @Override
   public void paintComponent(Graphics g) {
      super.paintComponent(g);    
      setBackground(Color.BLACK);


      Graphics2D g2d = (Graphics2D)g;

      AffineTransform saveTransform = g2d.getTransform();

      AffineTransform identity = new AffineTransform();
      g2d.setTransform(identity);

      g2d.setColor(Color.GREEN);
      g2d.fill(shape);

      //TRANSFORMACIONES
      double x = 200.0, y = -500.0;  
      g2d.translate(x, y);
      g2d.rotate(Math.toRadians(30.0));
      g2d.scale(10, 9);



      g2d.setColor(Color.RED);
      g2d.fill(shape);

      g2d.setTransform(saveTransform);
   }

   /** The Entry main method */
   public static void main(String[] args) {

      SwingUtilities.invokeLater(new Runnable() {
         @Override
         public void run() {


            JFrame frame = new JFrame(TITLE);
            frame.setContentPane(new Transformacion_R2());
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

         }
      });
   }
}

thank you for your help

Tom
  • 606
  • 7
  • 28
Erick
  • 1
  • 2
  • possible duplicate of [Converting String to int in Java?](http://stackoverflow.com/questions/5585779/converting-string-to-int-in-java) – k_g Feb 20 '15 at 04:43
  • His issue seems to be regarding the lack of error handling (based on his comments below), causing NumberFormatExceptions. The title is misleading. – iFytil Feb 20 '15 at 05:10

2 Answers2

0
int number = Integer.parseInt(jTextField.getText());

The typical method of doing it can be found at How to convert a String to an int in Java?

which you seem to be doing properly. What issues are you running into, more specifically?

If the jtextField is not set, you need to handle the possiblity of error.

int number;
try {
    number = Integer.parseInt(jTextField.getText());
} catch(NumberFormatException e) {
    number = 0; // Handle the error or give a default value
}
Community
  • 1
  • 1
iFytil
  • 459
  • 3
  • 7
  • thank you for your time answering,I really appreciate it, this is the error; Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: null at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at Transformacion_R2.(Transformacion_R2.java:17) at Transformacion_R2$1.run(Transformacion_R2.java:72) – Erick Feb 20 '15 at 04:46
  • Can you tell us what is in jTextField.getText()? It's possible it cannot represent an integer. – iFytil Feb 20 '15 at 04:47
  • Actually i cant run it, so i cant write anything into the jtext field when i try to run the frame it says: Exception in thread "main" java.lang.NumberFormatException: null at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at Transformacion_R2.(Transformacion_R2.java:17) at Transformacion.(Transformacion.java:19) at Transformacion_Tester.main(Transformacion_Tester.java:11) – Erick Feb 20 '15 at 04:49
  • If there is nothing written in the jtextField, of course it will fail if it tries to parse null into an Integer. You should do some error handling and guard against bad cases. Maybe a try-catch block. – iFytil Feb 20 '15 at 04:52
0

Your xa is always going to be null unless the constructor of Transformacion runs. In this code constructor and initializations for Transformacion_R2 run before constructor of Transformacion. So make sure you call the constructor of Transformacion before Transformacion_R2. To remove cyclic initalization remove Transformacion_R2 grafico= new Transformacion_R2(); from Transformacion Eg. code could be

 public static void main(String[] args) {

  SwingUtilities.invokeLater(new Runnable() {
     @Override
     public void run() {


        JFrame frame = new JFrame(TITLE);
        Transformacion trns = new Transformacion();
        frame.setContentPane(new Transformacion_R2());
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

     }
  });

}

Dheerendra Kulkarni
  • 2,728
  • 1
  • 16
  • 18