Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8005595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:16:25+00:00 2026-06-04T17:16:25+00:00

I am trying to plot a two variable equation to a Cartesian plane. Here

  • 0

I am trying to plot a two variable equation to a Cartesian plane. Here is my problem, on the code below if I comment out one for loop and let another run, the plot of the graph (the negative or positive sides of x) is correct, but if I run both for loops they seem to alter each other and the graph comes out wrong. I’ve tried every combination of for loops, looping frontward, backward, nothing seems to solve it.

I commented out the where these loops are at the end of the class “public class DrawingComponent extends JComponent” where it says “// This two loops work fine if each one is run independently”

public class GraphingCalc extends JFrame {

    private static final int FRAME_WIDTH = 450;
    private static final int FRAME_HEIGHT = 400;


    private static final double INITIAL_BALANCE = 1000;

    private JLabel rateLabel;
    private JTextField rateField;
    public JButton button ;
    private JLabel resultLabel;

    private JPanel basePanel;
    private JPanel topPanel; // will contain rateLabel, rateField, and buton
    private JPanel bottomPanel; // will contain drawingComponent 


    JComponent drawingComponent;

    Rectangle box;
    //////////////////////////////
    Graph graphObj;
    double xCoord; 
    int yCoord_size;
    double xRate;
    double[] yCoord;
    //////////////////////////////

    public GraphingCalc() {


        createTextField();


        drawingComponent = new DrawingComponent();      
        box = new Rectangle(100, 100, 20, 30);
        graphObj = new Graph();
        xCoord = 0;

        yCoord_size = 400;
        xRate = 0.101;

        yCoord = new double[yCoord_size];

        createButton();

        createPanel();

        setSize(FRAME_WIDTH, FRAME_HEIGHT);
    }

    private void createTextField() {

        rateLabel = new JLabel("Equation: ");

        final int FIELD_WIDTH = 10;
        rateField = new JTextField(FIELD_WIDTH);

    }



public void createButton() {

        button = new JButton("Plot");

         class AddInterestListener implements ActionListener {

            public void actionPerformed(ActionEvent event) {

                if(event.getSource() == button) {

                    //System.out.println("Boo");
                    graphObj.setEq(rateField.getText());

                }
                else {

                }

                //yCoord = Arrays.copyOf(graphObj.setY(), 10);
                yCoord = graphObj.getY(drawingComponent.getSize().width);

                //for(int i=0; i<10; i++)
                    //System.out.println("yCoord: " + yCoord[i] +"\n");

                revalidate();
                repaint();


            }
        }
        ActionListener listener = new AddInterestListener();
        button.addActionListener(listener);
    } 

public class DrawingComponent extends JComponent {

    public void paintComponent(Graphics g) {

            //////////////////////////
            // int scale 
            // multiply coord values by this
            // scale
            /////////////////////////
            Graphics2D g2 = (Graphics2D) g;

            g2.setStroke(new BasicStroke(1f));
            //g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            int drawArea_width = getSize().width;
            int drawArea_height = getSize().height;

            /////////////////////
            // x axis
            g2.draw(new Line2D.Double( 0, drawArea_height/2, drawArea_width ,drawArea_height/2 ));
            Font font = new Font("Times New Roman", Font.PLAIN, 10);
            g2.setFont(font);

            //positive x axis values

            for(int i = 1; i < 20; i++) {

                String num = Integer.toString(i);
                if(i >= 10) {
                    g2.draw(new Line2D.Double((drawArea_width/2 -19 ) + i*12 , (drawArea_height/2) -2 , (drawArea_width/2 -19 ) + i*12 , (drawArea_height/2) + 2 ));
                    g2.drawString(num, (drawArea_width/2 -22 ) + i*12  , (drawArea_height/2) + 15 );
                }

                else {
                    g2.draw(new Line2D.Double((drawArea_width/2) + i*10 , (drawArea_height/2) -2 , (drawArea_width/2) + i*10 , (drawArea_height/2) + 2 ));
                    g2.drawString(num, (drawArea_width/2 -1) + i*10 , (drawArea_height/2) + 15 );
                }
            }
            //


            //negative x axis values
            for(int i = 20; i > 0; i--) {

                String num = Integer.toString(i);
                if(i <= 9) {

                    g2.draw(new Line2D.Double((drawArea_width/2 ) - i*13 , (drawArea_height/2) -2 , (drawArea_width/2 ) - i*13 , (drawArea_height/2) + 2 ));
                    g2.drawString("-" + num, (drawArea_width/2 - 4) - i*15 , (drawArea_height/2) + 15 );
                }

                else {

                    g2.draw(new Line2D.Double((drawArea_width/2 + 16 ) - i*15 , (drawArea_height/2) -2 , (drawArea_width/2 + 16 ) - i*15 , (drawArea_height/2) + 2 ));
                    g2.drawString("-" + num, (drawArea_width/2 + 16 ) - i*17  , (drawArea_height/2) + 15 );
                }
            }
            //


            //////////////////////

            //y axis
            g2.draw(new Line2D.Double( drawArea_width/2, 0, drawArea_width/2, drawArea_height ));


            g2.setStroke(new BasicStroke(1.20f));



            // This two loops work fine if each one is run independently

            xCoord = 0;
            for(int i = 0; i < yCoord_size/2 ; i++) {

                //g2.draw(new Line2D.Double((drawArea_width/2 ) + xCoord , yCoord[i] + (drawArea_height/2), (drawArea_width/2 ) + xCoord , yCoord[i] + (drawArea_height/2) ) );
                g2.draw(new Ellipse2D.Double((drawArea_width/2 ) + xCoord , yCoord[i] + (drawArea_height/2), 1, 1));
                xCoord-= xRate;
            } 

            /*
            for(int i = yCoord_size/2; i < yCoord_size ; i++) {

                //g2.draw(new Line2D.Double((drawArea_width/2 ) + xCoord , yCoord[i] + (drawArea_height/2), (drawArea_width/2 ) + xCoord , yCoord[i] + (drawArea_height/2) ) );
                g2.draw(new Ellipse2D.Double((drawArea_width/2 ) + xCoord , yCoord[i] + (drawArea_height/2), 1, 1));
                xCoord+= xRate;
            }
            */

        }       
}


    private void createPanel() {

        basePanel = new JPanel();
        topPanel = new JPanel();

        basePanel.setLayout(new BorderLayout());

        topPanel.add(rateLabel);
        topPanel.add(rateField);
        topPanel.add(button);


        basePanel.add(topPanel, BorderLayout.NORTH);
        basePanel.add(drawingComponent, BorderLayout.CENTER);

        add(basePanel);     
    }
}




public class Graph {

    int yCoord_size;
    double xRate;
    String equation;
    double[] yCoord;
    static double num;

    public Graph() {

        yCoord_size = 400;
        xRate = 0.101;

        equation = "";
        num = 0;
        yCoord = new double[yCoord_size];

        for(int i=0; i <10; i++)
            yCoord[i] = 0;
    }

    public void setEq(String eq) {

        equation = eq;

        //System.out.println("eq is: " + eq);


    }

    public double equationStrToVal(String eq, double inX){  

        double total = 0.0;
        double x = inX;
        String[] tokens;
        tokens = eq.split(" ");

        for(int i=0; i<tokens.length; i++){

            if(tokens[i].equalsIgnoreCase("x")){

                total+= x;
            }

            //addition
            else if(tokens[i].equals("+")){
                if(tokens[i+1].equalsIgnoreCase("x"))
                    total+= x;
                else
                    total+= stringToDouble( tokens[i+1]);
                i++;
            }

            //multiplication
            else if(tokens[i].equals("*")){
                if(tokens[i+1].equalsIgnoreCase("x"))
                    total*= x;
                else
                    total*=stringToDouble( tokens[i+1]);
                i++;
            }

            //raised to a power

        }

        return total;
        //System.out.println("The result is: " + total);
    }

    public double[] getY(int screen_width) {

        double j = 0.0;

        //for(int i=0 ; i < screen_width/2; i++, j-=0.05) 
        for(int i=0 ; i < yCoord_size/2; i++, j-=xRate) 
            yCoord[i] =  -1 * equationStrToVal(equation, j);

        j = 0;

        //for(int i=screen_width/2 ; i<screen_width ; i++, j+=0.05)
        for(int i= yCoord_size/2 ; i< yCoord_size ; i++, j+=xRate)
            yCoord[i] = -1 * equationStrToVal(equation, j);     


        return yCoord;
    }

    static double stringToDouble(String num){


        return ( Double.valueOf(num.trim()).doubleValue());

    }

}

public class GraphingCalcViewer {

    public static void main(String[] args) {

        JFrame frame = new GraphingCalc();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:16:27+00:00Added an answer on June 4, 2026 at 5:16 pm

    if you comment out one loop the other loop always start with xCoord = 0;

    if you run both loops the second starts with the current xCoord.value

    it looks like you have to set “xCoord = 0;” before the second loop;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one y variable, which I am trying to plot against two related
what i am trying to do is plot two rows out of a file
I am trying to plot a matrix where each element is in one out
I am trying to plot two variables where N=700K. The problem is that there
I am trying to plot two columns from a text file using python matplotlib
I'm very new to python. two days. trying to get a plot working with
I'm trying to plot two sets of data wrt to time on the same
I'm trying to plot lines between two or more addresses in Google Maps. The
I am running into trouble trying to compare and plot two files of different
Trying to work out distance between two points (lat & lng) I have an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.