1

I have an application that does things with polar coordinates that is programmed in Java. I have to create a GUI for it and I decided to try to use swing components instead of this JAR called BreezyGUI. The issue I have is that I am unable to set the frame options without getting an error. How do I begin to design the interface after creating the frame, labels, and text fields? Any suggestions would really help me out, thanks!

The error I receive in Eclipse is not specific. It just has it highlighted red like it is a straight up miss type.

Here is my application:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;


public class PolarCoords extends JFrame
{

    JFrame frameWin = new JFrame();
    frameWin.setBounds(100, 200, 120, 120);
    frameWin.setTitle("Polar Coordinates");
    frameWin.setVisible(true);
    frameWin.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    private JLabel rLabel;
    private JLabel thetaLabel;
    private JLabel r2Label;
    private JLabel theta2Label;
    private JLabel answerLabel;

    private static String rString = "R Coordinate";
    private static String thetaString = "Theta";
    private static String r2String = "R2 Coordinate";
    private static String theta2String = "Theta2";
    private static String answerString = "Answer";

    private JFormattedTextField rField;
    private JFormattedTextField thetaField;
    private JFormattedTextField r2Field;
    private JFormattedTextField theta2Field;
    private JFormattedTextField answerField;

    public PolarCoords(double rCoord, double theta)
    {
        return;
    }

    /**
     * Returns theta in (r, theta)
     * @param tTheta
     * @return
     */
    public double tTheta(double theta)
    {
        return theta;
    }

    /**
     * Returns r coordinate in (r, theta)
     * @param rCoord
     * @return
     */
    public double rRCoord(double rCoord)
    {
        return rCoord;
    }

    public Object magnitude()
    {   
        double r1 = Double.valueOf(rField.getText());
        double theta1 = Double.valueOf(thetaField.getText());
        double r2 = 0;
        double theta2 = 0;
        double distance = Math.sqrt(Math.pow(r1, 2)) + Math.sqrt(Math.pow(r2, 2) - 2*(r1*r2) * Math.cos(theta1 - theta2));
        return distance;
    }

    public Double angleFromOrigin()
    {
        double theta1 = Double.valueOf(thetaField.getText());
        double theta2 = Double.valueOf(theta2Field.getText());
        double answer = theta2 - theta;
        return answer;
    }

    public Object distanceFromPoint()
    {
        // Distance formula Ã(r1)^2 + (r2)^2 - 2(r1)(r2) cos ( degree1 - degree2 )
        double r1 = Double.valueOf(rField.getText());
        double theta1 = Double.valueOf(thetaField.getText());
        double r2 = Double.valueOf(r2Field.getText());
        double theta2 = Double.valueOf(theta2Field.getText());
        double distance = Math.sqrt(Math.pow(r1, 2)) + Math.sqrt(Math.pow(r2, 2) - 2*(r1*r2) * Math.cos(theta1 - theta2));
        return distance;
    }

    public PolarCoords addPoints()
    {
        double r1 = Double.valueOf(rField.getText());
        double theta1 = Double.valueOf(thetaField.getText());
        double r2 = Double.valueOf(r2Field.getText());
        double theta2 = Double.valueOf(theta2Field.getText());
        double x = r1 * Math.cos(theta1);
        double y = r1 * Math.sin(theta1);
        double x2 = r2 * Math.cos(theta1);
        double y2 = r2 * Math.sin(theta2);

        double xAnswer = x2 - x;
        double yAnswer = y2 - y;
        double answer = Math.atan2(xAnswer, yAnswer);
        PolarCoords polarAnswer = new PolarCoords(xAnswer, yAnswer);
        return polarAnswer;
    }

    public static void main(String[] args)
    {
        Frame f = new Frame();
        System.out.print("Visible = True");
    }


}
BitWar
  • 43
  • 1
  • 2
  • 7
  • 1
    Please post code here in the forum. Please post your full error message too. Also the more specific your question, usually the better the help. Questions like `"I also am clueless as to how to design the interface"` usually are appropriate and simply answered with a link to the tutorials – Hovercraft Full Of Eels Sep 30 '12 at 18:02
  • Okay, sorry about that. I just don't know where to begin or how to design the interface. – BitWar Sep 30 '12 at 18:03
  • See also this [tag:jfreechart][example](http://stackoverflow.com/a/3467341/230513). – trashgod Sep 30 '12 at 18:09

3 Answers3

2

You're trying to place Java statements naked in the class, outside of a constructor or method, and this won't work. Consider placing these statements in your constructor. More importantly go through the intro to Java tutorials as you really should know the basics before trying to do Swing GUI coding.

Also, you're ignoring the JFrame that represents your this object and trying to create a totally unrelated JFrame object. Don't do that. Get rid of the frameWin variable as it will only mislead you.

Again read up on the basics of Java as this will help you immensely.

Please check out The Java Tutorials Really Big Index

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
2
 //JFrame frameWin = new JFrame();
    frameWin.setBounds(100, 200, 120, 120);
    frameWin.setTitle("Polar Coordinates");
    frameWin.setVisible(true);
    frameWin.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE) ;

Should be inside a method. And as you are extending JFrame it has to be

setBounds(100, 200, 120, 120);
 setTitle("Polar Coordinates");
 setVisible(true);
 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE) ;

You can do this:

public class PolarCoords extends JFrame
{
   public PolarCoords()
   {
     setBounds(100, 200, 120, 120);
     setTitle("Polar Coordinates");
     setVisible(true);
     setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE) ;
   }
}

Or

public class PolarCoords extends JFrame
{
   public PolarCoords()
   {
     Init();
   }
   public void Init()
   {
     setBounds(100, 200, 120, 120);
     setTitle("Polar Coordinates");
     setVisible(true);
     setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE) ;
   }
}
Lews Therin
  • 10,907
  • 4
  • 48
  • 72
2

Why is frameWin commented out and you are still using it in subsequent lines??? Did you mean to use this? That section should be in your constructor.

Also, setvisible should be the last thing after adding all your components to the frame.

Try:

public PolarCoords(double rCoord, double theta) //u'r not even using rCoord and theta
{
    this.setBounds(100, 200, 120, 120);
    this.setTitle("Polar Coordinates");
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    this.setVisible(true);
}
davidXYZ
  • 719
  • 1
  • 8
  • 16
  • I accidentally added that for some reason. I still receive the errors when it isn't. – BitWar Sep 30 '12 at 18:10
  • ok. But you dont need it anyway since you are already extending a JFrame. No need to create another instance. – davidXYZ Sep 30 '12 at 18:33