绘制方程直角平面 [英] Plotting equation to cartesian plane

查看:120
本文介绍了绘制方程直角平面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制两个变量方程笛卡尔平面。这里是我的问题,在code以下,如果我注释掉一个用于循环,让另一个运行,图形的曲线(x的消极或积极的一面)是正确的,但如果我同时运行for循环,他们似乎改变对方和图形出来是错误的。我试过的for循环每一个组合,循环向前,向后,似乎没有任何解决它。

我注释掉其中这些循环是在类的公共类DrawingComponent扩展JComponent的它说://这两个循环正常工作,如果每一个都是独立运行的结尾

 公共类GraphingCalc扩展的JFrame {

    私有静态最终诠释FRAME_WIDTH = 450;
    私有静态最终诠释FRAME_HEIGHT = 400;


    私有静态最后双INITIAL_BALANCE = 1000;

    私人的JLabel rateLabel;
    私人的JTextField rateField;
    公共JButton的按钮;
    私人的JLabel resultLabel;

    私人的JPanel basePanel;
    私人的JPanel topPanel; //将包含rateLabel,rateField和布通
    私人的JPanel bottomPanel; //将包含drawingComponent


    JComponent的drawingComponent;

    矩形框;
    //////////////////////////////
    图graphObj;
    双XCOORD;
    INT yCoord_size;
    双xRate;
    双[] YCOORD;
    //////////////////////////////

    公共GraphingCalc(){


        通过createTextField();


        drawingComponent =新DrawingComponent();
        箱=新的Rectangle(100,100,20,30);
        graphObj =新图();
        XCOORD = 0;

        yCoord_size = 400;
        xRate = 0.101;

        YCOORD =新的双[yCoord_size]

        createButton();

        createPanel();

        的setSize(FRAME_WIDTH,FRAME_HEIGHT);
    }

    私人无效通过createTextField(){

        rateLabel =新的JLabel(公式:);

        最终诠释FIELD_WIDTH = 10;
        rateField =新的JTextField(FIELD_WIDTH);

    }



公共无效createButton(){

        按钮=新的JButton(暗算);

         类AddInterestListener实现的ActionListener {

            公共无效的actionPerformed(ActionEvent的事件){

                如果(event.getSource()==按钮){

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

                }
                其他 {

                }

                // YCOORD = Arrays.copyOf(graphObj.setY(),10);
                YCOORD = graphObj.getY(drawingComponent.getSize()的宽度。);

                //的for(int i = 0;我小于10;我++)
                    //System.out.println("yCoord:+ YCOORD [我] +\ N);

                重新验证();
                重画();


            }
        }
        的ActionListener监听器=新AddInterestListener();
        button.addActionListener(听众);
    }

公共类DrawingComponent扩展JComponent的{

    公共无效的paintComponent(图形G){

            //////////////////////////
            // int标
            //乘以坐标值这个
            // 规模
            /////////////////////////
            Graphics2D的G2 =(Graphics2D的)克;

            g2.setStroke(新的BasicStroke(1F));
            //g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

            INT drawArea_width =的getSize()宽。
            INT drawArea_height =的getSize()的高度。

            /////////////////////
            // X轴
            g2.draw(新Line2D.Double(0,drawArea_height / 2,drawArea_width,drawArea_height / 2));
            字体的字体=新的字体(宋体,Font.PLAIN,10);
            g2.setFont(字体);

            //正x轴的值

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

                串NUM = Integer.toString(ⅰ);
                如果(ⅰ> = 10){
                    g2.draw(新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);
                }

                其他 {
                    g2.draw(新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);
                }
            }
            //


            //负x轴的值
            的for(int i = 20; I> 0;我 - ){

                串NUM = Integer.toString(ⅰ);
                如果(ⅰ&其中; = 9){

                    g2.draw(新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);
                }

                其他 {

                    g2.draw(新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轴上
            g2.draw(新Line2D.Double(drawArea_width / 2,0,drawArea_width / 2,drawArea_height));


            g2.setStroke(新的BasicStroke(1.20f));



            //这两个循环正常工作,如果每一个都是独立运行

            XCOORD = 0;
            的for(int i = 0; I< yCoord_size / 2;我++){

                //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(新Ellipse2D.Double((drawArea_width / 2)+ XCOORD,YCOORD [I] +(drawArea_height / 2),1,1));
                xCoord- = xRate;
            }

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

                //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(新Ellipse2D.Double((drawArea_width / 2)+ XCOORD,YCOORD [I] +(drawArea_height / 2),1,1));
                XCOORD + = xRate;
            }
            * /

        }
}


    私人无效createPanel(){

        basePanel =新的JPanel();
        topPanel =新的JPanel();

        basePanel.setLayout(新的BorderLayout());

        topPanel.add(rateLabel);
        topPanel.add(rateField);
        topPanel.add(按钮);


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

        加(basePanel);
    }
}




公共类图{

    INT yCoord_size;
    双xRate;
    字符串方程;
    双[] YCOORD;
    静态双NUM;

    公共格拉夫(){

        yCoord_size = 400;
        xRate = 0.101;

        公式=;
        NUM = 0;
        YCOORD =新的双[yCoord_size]

        的for(int i = 0;我小于10;我++)
            YCOORD [I] = 0;
    }

    公共无效setEq(字符串当量){

        公式=当量;

        //System.out.println("eq是:+当量);


    }

    公共双equationStrToVal(字符串均衡器,双INX){

        双总= 0.0;
        双X = INX;
        的String []标记;
        令牌= eq.split();

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

            如果(令牌[I] .equalsIgnoreCase(×)){

                共有+ = X;
            }

            //加成
            否则如果(令牌[I] .equals(+)){
                如果(令牌[I + 1] .equalsIgnoreCase(×))
                    共有+ = X;
                其他
                    共有+ =字符串到(令牌[I + 1]);
                我++;
            }

            //乘法
            否则如果(令牌[I] .equals(*)){
                如果(令牌[I + 1] .equalsIgnoreCase(×))
                    总* = X;
                其他
                    总* =字符串到(令牌[I + 1]);
                我++;
            }

            //幂

        }

        总回报;
        //System.out.println("The结果是:+总);
    }

    公共双[]的getY(INT SCREEN_WIDTH){

        双J = 0.0;

        //的for(int i = 0; I< SCREEN_WIDTH / 2;我++,J- = 0.05)
        的for(int i = 0; I< yCoord_size / 2;我++,J- = xRate)
            YCOORD [I] = -1 * equationStrToVal(方程,j)的;

        J = 0;

        //为(INT I = SCREEN_WIDTH / 2; I< SCREEN_WIDTH;我++,J + = 0.05)
        的for(int i = yCoord_size / 2; I< yCoord_size;我++,J + = xRate)
            YCOORD [I] = -1 * equationStrToVal(方程,j)的;


        返回YCOORD;
    }

    静态双字符串到(字符串NUM){


        返程(Double.valueOf(num.trim())的doubleValue());

    }

}

公共类GraphingCalcViewer {

    公共静态无效的主要(字串[] args){

        JFrame的框架=新GraphingCalc();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(真正的);
    }

}
 

解决方案

如果您注释掉一个循环的另一个循环总是先从XCOORD = 0;

如果您同时运行循环的第二开始与当前xCoord.value

看起来你必须设置XCOORD = 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);
    }

}

解决方案

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;

这篇关于绘制方程直角平面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆