Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 绘制正弦波曲线_Java_Swing_Animation_Graph_Curve - Fatal编程技术网

Java 绘制正弦波曲线

Java 绘制正弦波曲线,java,swing,animation,graph,curve,Java,Swing,Animation,Graph,Curve,输出是一条直线,但应该是一条曲线。 不确定问题是数学还是代码。 我知道问题出在它所说的(Math.sin(sum3.getValue())位上,但我无法计算出它应该是什么 出于某种原因,我得到了一张便利贴,上面写着I(V)=SIN(V);我想这就是我试图实现的,以得到I(V)曲线。 Sum3(V)来自不同的类 import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; public class Grap

输出是一条直线,但应该是一条曲线。 不确定问题是数学还是代码。 我知道问题出在它所说的(
Math.sin(sum3.getValue()
)位上,但我无法计算出它应该是什么

出于某种原因,我得到了一张便利贴,上面写着I(V)=SIN(V);我想这就是我试图实现的,以得到I(V)曲线。 Sum3(V)来自不同的类

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;

public class GraphApp extends JFrame {

    int x,y;
    int ax,by;
    IVChar sum3 = new IVChar();

    //create a window in which the graph will be shown

    public GraphApp(){
        setTitle("Graph App");
        setSize(700,700);
        setResizable(true);
        setVisible(true);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        x =  30;
        y = 300;
    }

    // create the axis
    @Override
    public void paint(Graphics g){
        g.drawLine(300, 30, 300, 600); // y axis
        g.drawLine(30, 300, 600, 300); // x axis
        g.setColor(Color.blue);//colour of drawLine
        g.fillOval(x, y, 3, 3);
        g.drawString("I", 310, 40);
        g.drawString("V'", 600, 314);
        run();
        repaint(); //makes it run again
    }
    // implement and draw graphical functions
    public void run(){

        try{
            Thread.sleep(10); //speed line is drawn
            float ax,by;
            ax = x-300;
            by = y-300;
            //specify the function
            by = (float) Math.sin(sum3.getValue());//makes a sin wave
            x = (int) (ax + 300);
            y = (int) (300 - by);
            x++;

        }catch(Exception e){
            System.out.println("Error!");
        }
    }
    public static void main(String[]args){
        new GraphApp();
    }
}
您可以使用来设置绘制曲线的动画:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class GraphApp extends JFrame {

    public GraphApp(){
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        add(new SineWave());
        pack();
        setVisible(true);
    }

    public static void main(String[]args){
        SwingUtilities.invokeLater(()->new GraphApp());
    }
}

class SineWave extends JPanel{

    //use constants for better readability
    private static final double W = 600, H = 700, AMPLITUDE = H/3;
    private static final int MARGIN =30, GAP = 15,DOT_SIZE = 3, SPEED = 10;

    //starting point
    private double x = MARGIN;
    private final double y = H/2;
    private final int dX = 1; //x increment

    //you need to use doubles to avoid rounding error and have use non integer coordinates
    private final List<Point2D.Double> points;
    private final Timer timer;

    public SineWave() {
        setPreferredSize(new Dimension((int)W, (int)H));
        points = new ArrayList<>();
        timer = new Timer(SPEED, e->addPoints()); //timer to add sine points
        timer.start();
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;

        Shape xAxis = new Line2D.Double(MARGIN, H/2, W-MARGIN, H/2);
        g2.draw(xAxis);
        Shape yAxis = new Line2D.Double(W/2, MARGIN, W/2, H-MARGIN);
        g2.draw(yAxis);
        g.drawString("I", (int)(W/2-GAP),MARGIN);
        g.drawString("V'", (int)(W-GAP), (int)(H/2+GAP));

        g2.setColor(Color.blue);//colour of graph
        for(Point2D.Double p : points){
            Shape point = new Ellipse2D.Double(p.getX(), p.getY(),DOT_SIZE , DOT_SIZE);
            g2.draw(point);
        }
    }

    private void addPoints() {

        double angel = - Math.PI + 2* Math.PI * ((x-MARGIN)/(W- 2*MARGIN));//angle in radians
        double newY = y + AMPLITUDE  * Math.sin(angel);
        points.add(new Point2D.Double(x, newY));
        x += dX;
        if(x >= W-MARGIN) {
            timer.stop();
        }
        repaint();
    }
}
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.Shape;
导入java.awt.geom.Ellipse2D;
导入java.awt.geom.Line2D;
导入java.awt.geom.Point2D;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
导入javax.swing.Timer;
公共类GraphApp扩展了JFrame{
公共图书馆{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
添加(新正弦波());
包装();
setVisible(真);
}
公共静态void main(字符串[]args){
调用器(()->new GraphApp());
}
}
类正弦波扩展JPanel{
//使用常量以提高可读性
专用静态最终双W=600,H=700,振幅=H/3;
专用静态最终整型余量=30,间隙=15,圆点尺寸=3,速度=10;
//起点
私人双x=保证金;
私人最终双y=H/2;
私有最终整数dX=1;//x增量
//您需要使用双精度以避免舍入错误,并使用非整数坐标
私人最终名单点;
私人最终定时器;
公共正弦波{
setPreferredSize(新尺寸((int)W,(int)H));
points=新的ArrayList();
计时器=新计时器(速度,e->addPoints());//添加正弦点的计时器
timer.start();
}
@凌驾
公共组件(图形g){
超级组件(g);
图形2d g2=(图形2d)g;
形状xAxis=新线条2D.Double(边距,H/2,W边距,H/2);
g2.绘制(xAxis);
形状yAxis=新线条2D.双(W/2,边距,W/2,H-边距);
g2.绘制(yAxis);
g、 抽绳(“I”和(int)(W/2-间隙),边距);
g、 抽绳(V’,(int)(W-GAP),(int)(H/2+GAP));
g2.setColor(Color.blue);//图形的颜色
用于(点2D.Double p:点){
形状点=新的椭圆2d.Double(p.getX(),p.getY(),点大小,点大小);
g2.绘制(点);
}
}
私有void addPoints(){
双角度=-Math.PI+2*Math.PI*((x-MARGIN)/(W-2*MARGIN));//以弧度表示的角度
双新y=y+振幅*Math.sin(天使);
添加(新点2d.Double(x,newY));
x+=dX;
如果(x>=W-裕度){
timer.stop();
}
重新油漆();
}
}

将为您提供基本想法,