Drawing the graph of sin(x) in java

我们两清 提交于 2019-12-08 09:35:28

问题


I am currently trying to draw the graph of sin(x) in java. I am required by the instructions of my assignment to only use drawLine() as the method of drawing the graph. I can't seem to figure out how to properly set my y value though. Right now what I have is a while loop used in order to draw the line pixel by pixel, but cannot manage to get the y value to be correct. Here is what I have so far.

public class GraphJComponent extends JComponent {
public void paintComponent (Graphics g){
    Color axis = new Color(128, 128, 128);
    g.setColor(axis);

    int xShift = getWidth() / 50;
    int xShift2 = getWidth() / 100;
    int yShift = getHeight() / 10;
    int yShift2 = getHeight() / 17;

    g.drawLine(xShift,yShift,xShift,getHeight() - yShift);
    g.drawLine(xShift, getHeight() / 2, getWidth() - xShift, getHeight() / 2);

    g.drawString("0", xShift + xShift2, getHeight() / 2 + yShift2);
    g.drawString("1", xShift + xShift2, yShift - (yShift2 / 4));
    g.drawString("-1", xShift + xShift2, getHeight() - yShift + yShift2);
    g.drawString("\u03c0", getWidth() / 2, getHeight() / 2 + yShift2);
    g.drawString("2" + "\u03c0", getWidth() - (2 * xShift), getHeight() / 2 + yShift2);

    Color line = new Color (255, 0, 0);
    g.setColor(line);

    int x = xShift;
    int y = getHeight() / 2;




    while (x < getWidth() - xShift){
        x += 1;

        y = ??;


        g.drawLine(x, y, x, y);
    }










}

}

Yes I know there are numerous things that I could tidy up or simplify, this is just a rough draft, per-se, that I will clean up once I have everything working. I have tried multiple methods of getting the y value to properly be set, the closest I got ended up drawing a straight diagonal lines, it wasn't curved like it was supposed to be. What can I do to properly set the y value so that the sin graph from [0, 2pi] will be properly drawn?

To try to clarify the issue: The problem lies in changing the y value of my drawLine function. Basically, what I had working wouldn't draw the sin function properly because I only could figure out to lineraly increment it. It looked something like this:

 /\
/  \  /
    \/

The ultimate question I have is: How can I make it so my y coordinate will scale with the proper y coordinate of the sin(x) graph? In other words, how can I make it "not-straight" and properly curved like the sin(x) graph should.

Another way to look at the issue is how could I properly "scale" the y value of sin(x) with the pixels of my JFrame? Thank you for your time I appreciate any help.


回答1:


Well, David Wallace mentioned in comments that you need to reduce your x increment, but I think you are trying to get 2*PI over the entire width interval.

Use y = Math.sin(x*Math.PI*2/getWidth()); which will scale your x values before plugging them into your sin function.




回答2:


Well here's the code,not only it can draw the graph of sin(x) but also it can draw any graph using an external parser.

      import javax.swing.*;
      import java.awt.*;
      import java.util.Scanner;
      import net.objecthunter.exp4j.*;

     class math extends JFrame
     {
        public static void main(String args[])
      {
    math m=new math();
    m.setVisible(true);
    m.setLocationRelativeTo(null);


}

public void paintallies(Graphics G1,double sf)
{int i;
 Graphics2D g21=(Graphics2D) G1;
 g21.setColor(Color.GREEN);
    for(i=0;i<=600;i=(int) (i+sf))
    {
        g21.drawLine(i,0,i,600);
        g21.drawLine(0,i,600,i);

    }

}
public void paintaxes(Graphics G1)
{
    Graphics2D g21=(Graphics2D) G1;
    g21.setColor(Color.BLACK);
    g21.drawLine(300,0,300,600);//y axis

    g21.drawLine(0,300,600,300); //x axis

}


public void paint(Graphics G)
{
    int i;
    double j,k;

    Scanner s=new Scanner(System.in);
    System.out.println("Enter input");
    String input=s.nextLine();
    System.out.println("Enter scale factor");
    double sf=s.nextDouble();
    double sff=300/sf;
    double kf=sff;
    double count=0;






    Graphics g2=(Graphics) G;
    paintallies(G,sf);
    paintaxes(G);

    g2.translate(300,300);
    do
    {
        kf=kf-(1/sf);
        count++;

    }while(kf>=0);
    double counts=2*count;


    Color c=Color.RED;
    g2.setColor(c.darker());

    double yarr[]=new double[(int)counts];
    double xarr[]=new double[(int)counts];



    Expression E=new ExpressionBuilder(input).variables("x").build();


     j=-sff; k=-sff;
    for(i=0;i<counts;i++)
    {

        xarr[i]=j;
        j=j+(1/sf);


        E.setVariable("x",k);
        yarr[i]=E.evaluate();
        k=k+(1/sf);

        xarr[i]=sf*xarr[i];
        yarr[i]=-sf*yarr[i];

    }

    for(i=0;i<counts;i++)
    {
        if(i==counts-1)
        {
            break;
        }
        else
        {
        g2.drawLine((int)xarr[i],(int)yarr[i],(int)xarr[i+1],(int)yarr[i+1]);

        }
    }


}






math()
{
    super("Grapher");
    setSize(600,600);
    setResizable(true);



}

}



来源:https://stackoverflow.com/questions/28757480/drawing-the-graph-of-sinx-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!