Java 绘制正弦和余弦函数

Java 绘制正弦和余弦函数,java,math,graph,pi,trigonometry,Java,Math,Graph,Pi,Trigonometry,我目前在家庭作业方面遇到了一些问题 以下是练习: (绘制正弦和余弦函数)编写一个程序,用红色绘制正弦函数,用蓝色绘制余弦函数 提示:Pi的Unicode是\u03c0。要显示-2Pi,请使用g.drawString(“-2\u03c0”,x,y)。对于像sin(x)这样的三角函数,x的单位是弧度。使用以下循环将点添加到多边形p for(intx=-170;x试试这个: import java.awt.BorderLayout; import java.awt.Graphics; import j

我目前在家庭作业方面遇到了一些问题

以下是练习:

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

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

for(intx=-170;x试试这个:

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);

    }
 }
}
导入java.awt.BorderLayout;
导入java.awt.Graphics;
导入java.awt.Polygon;
导入java.awt.Color;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公共类练习13_12扩展JFrame{
公众锻炼13_12(){
setLayout(新的BorderLayout());
添加(新的DrawSine(),BorderLayout.CENTER);
}
公共静态void main(字符串[]args){
Exercise13_12 frame=新Exercise13_12();
框架。设置尺寸(400300);
框架.设置标题(“练习13_12”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
类DrawSine扩展了JPanel{
双f(双x){
返回Math.sin(x);
}
双GCO(双y){
返回Math.cos(y);
}
受保护组件(图形g)
{
超级组件(g);
g、 抽绳(101001380100);
g、 抽绳(200、30、200、190);
g、 抽绳(38010037090);
g、 抽绳(380100370110);
g、 抽绳(200、30、190、40);
g、 抽绳(200、30、210、40);
g、 抽绳(“X”、360、80);
g、 抽绳(“Y”、220、40);
多边形p=新多边形();
多边形p2=新多边形();

对于(int x=-170;x您可以将其添加到
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);
//绘制pi和-pi
g、 抽绳(“-\u03c0”,147100);
g、 抽绳(“\u03c0”,253100);
//创建一个新多边形
多边形p2=新多边形();
//添加余弦的点

对于(intx=-170;x好的,现在程序已经完成了,我就这样结束了

导入java.awt.BorderLayout;
导入java.awt.Graphics;
导入java.awt.Polygon;
导入java.awt.Color;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公共类练习13_12扩展JFrame{
公众锻炼13_12(){
setLayout(新的BorderLayout());
添加(新的DrawSine(),BorderLayout.CENTER);
}
公共静态void main(字符串[]args){
Exercise13_12 frame=新Exercise13_12();
框架。设置尺寸(400300);
框架.设置标题(“练习13_12”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
类DrawSine扩展了JPanel{
双f(双x){
返回Math.sin(x);
}
双GCO(双y){
返回Math.cos(y);
}
受保护组件(图形g)
{
超级组件(g);
g、 抽绳(101001380100);
g、 抽绳(200、30、200、190);
g、 抽绳(38010037090);
g、 抽绳(380100370110);
g、 抽绳(200、30、190、40);
g、 抽绳(200、30、210、40);
g、 抽绳(“X”、360、80);
g、 抽绳(“Y”、220、40);
多边形p=新多边形();
多边形p2=新多边形();
对于(intx=-170;x检查它

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);
    }
}
public类guibasictest{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
//TODO代码应用程序逻辑
guiBasics gb=新的guiBasics();
JFrame jf=新JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT\u ON\u CLOSE);
jf.设置尺寸(500500);
jf.添加(gb);
jf.setVisible(真);
}        
}    
................................................................................
包装基础;
导入java.awt.Graphics;
导入javax.swing.JPanel;
/**
*
*@作者阿里
*/
公共类GUI扩展了JPanel{
//图形g=空;
公共组件(图形g){
//超级组件(g);

对于(double i=0;i您的问题到底是什么?好吧,我需要用与sin函数相同的方法使用cos函数,并在图形中添加第二行。返回余弦的
g
函数将遇到问题,因为您还将
g
用于
图形
参数,编译器将使用
g
et混淆。更改返回
Math.cos
的函数的名称。或者只使用
Math.cos
而不使用另一个函数。您实际上不需要
f
;您可以将使用
f
的代码改为使用
Math.sin
。对于
Math.cos
也一样。(编辑:也许编译器不会混淆。但是人类读者肯定会混淆。)感谢您回答@ajb,所以我删除了
f
g
。现在我收到了这个错误。“在删除
f
之后,类型JFrame的方法paintComponent(Graphics)是未定义的。对于(int x=-170;x@Khilmarsen我在看到它工作之前添加了最后一条评论。太好了!也许你不是有意这么做的,但我会避免制作多边形并直接在paintComponent方法中添加点。我认为paintComponent会为每个repaint()问题调用。
        //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);
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);

  }
 }
}
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);
    }
}