用java绘制sin(x)图
我目前正试图用java绘制sin(x)图。作业说明要求我仅使用drawLine()作为绘制图形的方法。不过,我似乎不知道如何正确设置y值。现在我使用的是一个while循环,用于逐像素地绘制线,但无法获得正确的y值。这是我到目前为止所拥有的用java绘制sin(x)图,java,graph,trigonometry,Java,Graph,Trigonometry,我目前正试图用java绘制sin(x)图。作业说明要求我仅使用drawLine()作为绘制图形的方法。不过,我似乎不知道如何正确设置y值。现在我使用的是一个while循环,用于逐像素地绘制线,但无法获得正确的y值。这是我到目前为止所拥有的 public class GraphJComponent extends JComponent { public void paintComponent (Graphics g){ Color axis = new Color(128, 128, 12
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);
}
}
我的最终问题是:如何使我的y坐标与sin(x)图的y坐标成比例?换句话说,我怎样才能使它像sin(x)图那样“不直”并正确地弯曲
另一种看待这个问题的方式是,我如何用JFrame的像素正确地“缩放”sin(x)的y值?
感谢您抽出时间,我非常感谢您的帮助。大卫·华莱士在评论中提到,您需要减少
x
增量,但我认为您正试图在整个宽度
间隔内获得2*PI
使用
y=Math.sin(x*Math.PI*2/getWidth())
将缩放x
值,然后再将其插入sin
函数。好吧,David Wallace在评论中提到,您需要减少x
增量,但我认为您正试图在整个宽度
间隔内获得2*PI
使用
y=Math.sin(x*Math.PI*2/getWidth())
将缩放x
值,然后再将其插入sin
函数。好吧,David Wallace在评论中提到,您需要减少x
增量,但我认为您正试图在整个宽度
间隔内获得2*PI
使用
y=Math.sin(x*Math.PI*2/getWidth())
将缩放x
值,然后再将其插入sin
函数。好吧,David Wallace在评论中提到,您需要减少x
增量,但我认为您正试图在整个宽度
间隔内获得2*PI
使用
y=Math.sin(x*Math.PI*2/getWidth())
将缩放x
值,然后再将其插入sin
函数。下面是代码,它不仅可以绘制sin(x)的图形,还可以使用外部解析器绘制任何图形
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);
}
import javax.swing.*;
导入java.awt.*;
导入java.util.Scanner;
导入net.objecthunter.exp4j.*;
类数学扩展了JFrame
{
公共静态void main(字符串参数[])
{
数学m=新数学();
m、 setVisible(真);
m、 setLocationRelativeTo(空);
}
公共空间(图形G1,双sf)
{int i;
Graphics2D g21=(Graphics2D)G1;
g21.setColor(颜色为绿色);
对于(i=0;i=0);
双重计数=2*计数;
颜色c=颜色。红色;
g2.setColor(c.darker());
双码[]=新的双码[(整数)计数];
double xarr[]=新的double[(int)计数];
表达式E=新的ExpressionBuilder(输入).variables(“x”).build();
j=-sff;k=-sff;
对于(i=0;i这是代码,它不仅可以绘制sin(x)的图,还可以使用外部解析器绘制任何图
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);
}
import javax.swing.*;
导入java.awt.*;
导入java.util.Scanner;
导入net.objecthunter.exp4j.*;
类数学扩展了JFrame
{
公共静态void main(字符串参数[])
{
数学m=新数学();
m、 setVisible(真);
m、 setLocationRelativeTo(空);
}
公共空间(图形G1,双sf)
{int i;
Graphics2D g21=(Graphics2D)G1;
g21.setColor(颜色为绿色);
对于(i=0;i=0);
双重计数=2*计数;
颜色c=颜色。红色;
g2.setColor(c.darker());
双码[]=新的双码[(整数)计数];
double xarr[]=新的double[(int)计数];
表达式E=新的ExpressionBuilder(输入).variables(“x”).build();
j=-sff;k=-sff;
对于(i=0;i这是代码,它不仅可以绘制sin(x)的图,还可以使用外部解析器绘制任何图
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);
}
import javax.swing.*;
导入java.awt.*;
导入java.util.Scanner;
导入net.objecthunter.exp4j.*;
类数学扩展了JFrame
{
公共静态void main(字符串参数[])
{
数学m=新数学();
m、 setVisible(真);
m、 setLocationRelativeTo(空);
}
公共空间(图形G1,双sf)
{int i;
Graphics2D g21=(Graphics2D)G1;
g21.setColor(颜色为绿色);
对于(i=0;i=0);
双重计数=2*计数;
颜色c=颜色。红色;
g2.setColor(c.darker());
双码[]=新的双码[(整数)计数];
double xarr[]=新的double[(int)计数];
表达式E=新的ExpressionBuilder(输入).variables(“x”).build();
j=-sff;k=-sff;
对于(i=0;i这是代码,它不仅可以绘制sin(x)的图,还可以使用外部解析器绘制任何图
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);
}
import javax.swing.*;
导入java.awt.*;
导入java.util.Scanner;
导入net.objecthunter.exp4j.*;
类数学扩展了JFrame
{
公共静态void main(字符串参数[])
{
数学m=新数学();
m、 setVisible(真);
m、 setLocationRelativeTo(空);
}
公共空间(图形G1,双sf)
{int i;
Graphics2D g21=(Graphics2D)G1;
g21.setColor(颜色为绿色);
对于(i=0;i=0);
双重计数=2*计数;
颜色c=颜色。红色;
g2.setColor(c.darker());
双码[]=新的双码[(整数)计数];
double xarr[]=新的double[(int)计数];
表达式E=新的ExpressionBuilder(输入).variables(“x”).build();
j=-sff;k=-sff;
对于(i=0;iWell,如果你想要一条从0到2*pi
的平滑曲线,你需要每次将x
的增量远远小于1
。否则你只能得到7个连接点。它不应该是y=Math.sin(x)
?不清楚你所说的“增量”是什么意思y
值。如果您