【发布时间】:2019-01-07 22:36:06
【问题描述】:
输出是一条直线,但应该是一条曲线。
不确定问题是数学还是代码。
我知道问题出在它所说的地方 (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();
}
}
【问题讨论】:
标签: java swing animation graph curve