绘制正弦和余弦函数

时间:2013-11-11 19:40:37

标签: java math graph pi cosine

我目前在做作业方面遇到了一些问题。

这是练习:

  

(绘制正弦和余弦函数)编写一个程序,用红色绘制正弦函数,用蓝色绘制余弦函数。

     

提示: Pi的Unicode为 \ u03c0 。要显示-2Pi,请使用g.drawString(“ - 2 \ u03c0”,x,y)。对于像sin(x)这样的三角函数,x是弧度。使用以下循环将点添加到多边形 p

for (int x = -170; x <= 170; x++) {
    p.addPoint(x + 200, 100 - (int)(50 * Math.sin((x / 100.0) * 2 * Math.PI)));
  

-2Pi位于( 100,100 ),轴的中心位于( 200,100 ),2Pi位于( 300, 100 )   使用 Graphics 类中的drawPolyline方法连接点。

好吧,所以我所拥有的罪恶功能与练习中的功能略有不同,但它起作用,所以它应该不是问题。另一方面,余弦函数,我找不到它的代码,所以我的程序中没有。

我还需要做的是将-Pi和Pi放在他们可敬的地方的图表上。

所以,这是代码。

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Polygon;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Exercise13_12 extends JFrame {

public Exercise13_12() {
    setLayout(new BorderLayout());
    add(new DrawSine(), BorderLayout.CENTER);
}

public static void main(String[] args) {
    Exercise13_12 frame = new Exercise13_12();
    frame.setSize(400, 300);
    frame.setTitle("Exercise13_12");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}

class DrawSine extends JPanel {

    double f(double x) {
        return Math.sin(x);
    }

    double g(double y) {
        return Math.cos(y);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.drawLine(10, 100, 380, 100);
        g.drawLine(200, 30, 200, 190);

        g.drawLine(380, 100, 370, 90);
        g.drawLine(380, 100, 370, 110);
        g.drawLine(200, 30, 190, 40);
        g.drawLine(200, 30, 210, 40);

        g.drawString("X", 360, 80);
        g.drawString("Y", 220, 40);

        Polygon p = new Polygon();

        for (int x = -170; x <= 170; x++) {
            p.addPoint(x + 200, 100 - (int) (50 * f((x / 100.0) * 2
                    * Math.PI)));

        }

        g.drawPolyline(p.xpoints, p.ypoints, p.npoints);
        g.drawString("-2\u03c0", 95, 115);
        g.drawString("2\u03c0", 305, 115);
        g.drawString("0", 200, 115);
    }
 }
}

如果有人有时间帮助我,我将非常感激。

4 个答案:

答案 0 :(得分:6)

试试这个:

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Exercise13_12 extends JFrame {

public Exercise13_12() {
    setLayout(new BorderLayout());
    add(new DrawSine(), BorderLayout.CENTER);
}

public static void main(String[] args) {
    Exercise13_12 frame = new Exercise13_12();
    frame.setSize(400, 300);
    frame.setTitle("Exercise13_12");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}

class DrawSine extends JPanel {

    double f(double x) {
        return Math.sin(x);
    }

    double gCos(double y) {
        return Math.cos(y);
    }

    protected void paintComponent(Graphics g) 
    {
        super.paintComponent(g);

        g.drawLine(10, 100, 380, 100);
        g.drawLine(200, 30, 200, 190);

        g.drawLine(380, 100, 370, 90);
        g.drawLine(380, 100, 370, 110);
        g.drawLine(200, 30, 190, 40);
        g.drawLine(200, 30, 210, 40);

        g.drawString("X", 360, 80);
        g.drawString("Y", 220, 40);

        Polygon p = new Polygon();
        Polygon p2 = new Polygon();

       for (int x = -170; x <= 170; x++) {
            p.addPoint(x + 200, 100 - (int) (50 * f((x / 100.0) * 2
                    * Math.PI)));

        }

        for (int x = -170; x <= 170; x++) {
            p2.addPoint(x + 200, 100 - (int) (50 * gCos((x / 100.0) * 2
                    * Math.PI)));

        }

        g.setColor(Color.red);
        g.drawPolyline(p.xpoints, p.ypoints, p.npoints);
        g.drawString("-2\u03c0", 95, 115);
        g.drawString("2\u03c0", 305, 115);
        g.drawString("0", 200, 115);

        g.setColor(Color.blue);
        g.drawPolyline(p2.xpoints, p2.ypoints, p2.npoints);

    }
 }
}

enter image description here

基本上它是相同的代码,但你需要一个新的多边形来绘制它。然后我使用Graphics的setColor()函数设置颜色。

答案 1 :(得分:2)

您可以将其添加到paintComponent方法:

        //Draw pi and -pi
        g.drawString("-\u03c0", 147, 100);
        g.drawString("\u03c0", 253, 100);   

        //Create a new polygon
        Polygon p2 = new Polygon();

        //Add the points of the cosine
        for (int x = -170; x <= 170; x++) {
            p2.addPoint(x + 200, 100 - (int) (50 * g((x / 100.0) * 2
                    * Math.PI)));
        }
        //Draw the function
        g.drawPolyline(p2.xpoints, p2.ypoints, p2.npoints);

通过它,您可以获得所需的结果。

答案 2 :(得分:0)

好的,现在程序已经完成,我最终得到了这个

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Exercise13_12 extends JFrame {

public Exercise13_12() {
setLayout(new BorderLayout());
add(new DrawSine(), BorderLayout.CENTER);
}

public static void main(String[] args) {
Exercise13_12 frame = new Exercise13_12();
frame.setSize(400, 300);
frame.setTitle("Exercise13_12");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

}

class DrawSine extends JPanel {

double f(double x) {
    return Math.sin(x);
}

double gCos(double y) {
    return Math.cos(y);
}

protected void paintComponent(Graphics g) 
{
    super.paintComponent(g);

    g.drawLine(10, 100, 380, 100);
    g.drawLine(200, 30, 200, 190);

    g.drawLine(380, 100, 370, 90);
    g.drawLine(380, 100, 370, 110);
    g.drawLine(200, 30, 190, 40);
    g.drawLine(200, 30, 210, 40);

    g.drawString("X", 360, 80);
    g.drawString("Y", 220, 40);

    Polygon p = new Polygon();
    Polygon p2 = new Polygon();

   for (int x = -170; x <= 170; x++) {
        p.addPoint(x + 200, 100 - (int) (50 * f((x / 100.0) * 2
                * Math.PI)));

    }

    for (int x = -170; x <= 170; x++) {
        p2.addPoint(x + 200, 100 - (int) (50 * gCos((x / 100.0) * 2
                * Math.PI)));

    }

    g.setColor(Color.red);
    g.drawPolyline(p.xpoints, p.ypoints, p.npoints);
    g.drawString("-2\u03c0", 95, 115);
    g.drawString("-\u03c0", 147, 115);
    g.drawString("\u03c0", 253, 115);  
    g.drawString("2\u03c0", 305, 115);
    g.drawString("0", 200, 115);

    g.setColor(Color.blue);
    g.drawPolyline(p2.xpoints, p2.ypoints, p2.npoints);

  }
 }
}

对于之后可能遇到同样问题的人。

感谢各位帮助我的人,永远感激不尽。

答案 3 :(得分:0)

检查出来.....

public class GuiBasicstest {    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic herei
        guiBasics gb = new guiBasics();
        JFrame jf = new JFrame();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setSize(500,500);
        jf.add(gb);            
        jf.setVisible(true);            
    }        
}    

................................................................................
package guibasics;    
import java.awt.Graphics;
import javax.swing.JPanel;

/**
 *
 * @author ALI
 */
public class guiBasics extends JPanel{
    //Graphics g = null;

    public void paintComponent(Graphics g){

        //super.paintComponent(g);
        for(double i = 0; i<200;i++){
            double y= Math.sin(i);
            draw(i,y,g);
        }
    }
    private void draw(double x , double y,Graphics g ){ 
         double x1, y1;
         x+=10;
         y+=10;
         x*=10;
         y*=30;

       x1=x+1;
       y1=y+1;
       Double d = new Double(x);
       int a = d.intValue();
       int b = (new Double(y)).intValue();
       int c = (new Double(x1)).intValue();
       int e = (new Double(y1)).intValue();
       g.drawLine(a, b, c, e);
    }
}