在java中绘制sin(x)图 [英] Drawing the graph of sin(x) in java
问题描述
我目前正试图在java中绘制sin(x)图。根据我的任务指示,我只需要使用drawLine()作为绘制图形的方法。我似乎无法弄清楚如何正确设置我的y值。现在我所拥有的是一个while循环,用于逐像素地绘制线条,但无法设法使y值正确。这是我到目前为止。
public class GraphJComponent extends JComponent {
public void paintComponent(Graphics g){
颜色轴=新颜色(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(\\\π,getWidth()/ 2,getHeight()/ 2 + yShift2);
g.drawString(2+\\\π,getWidth() - (2 * xShift),getHeight()/ 2 + yShift2);
颜色线=新颜色(255,0,0);
g.setColor(line);
int x = xShift;
int y = getHeight()/ 2;
while(x x + = 1;
y = ??;
g.drawLine(x,y,x,y);
}
}
}
是的,我知道有无数的事情,我可以整理或简化,这只是一个粗略的草案,本身,我会清理一旦我有一切工作。
我已经尝试了多种方法来设置y值,最接近的我画了一条直线,但它不像本应该弯曲的那样。我能做些什么来正确地设置y值,以便正确绘制[0,2pi]的正弦图?
试图澄清问题:
问题在于改变drawLine函数的y值。基本上,我所做的工作并不能正确地描绘罪的功能,因为我只能想出分数递增它。它看起来像这样:
/ \
/ \ /
\ /
我得到的最终问题是:如何使它成为我的y坐标将使用适当的y坐标sin(x)图的?换句话说,我怎样才能使它不直,并像sin(x)图应该正确地弯曲。
另一种观察问题的方式是我可以用我的JFrame的像素来正确地缩放sin(x)的y值吗?
感谢您的时间,我感谢您的任何帮助。
x
增量,但是我认为你试图在整个 2 * PI
>宽度间隔。使用 y = Math.sin(x * Math.PI * 2 / getWidth() ); 在将 x
值插入到 sin
函数中之前,它将缩放您的 x
值。 / p>
I am currently trying to draw the graph of sin(x) in java. I am required by the instructions of my assignment to only use drawLine() as the method of drawing the graph. I can't seem to figure out how to properly set my y value though. Right now what I have is a while loop used in order to draw the line pixel by pixel, but cannot manage to get the y value to be correct. Here is what I have so far.
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);
}
}
}
Yes I know there are numerous things that I could tidy up or simplify, this is just a rough draft, per-se, that I will clean up once I have everything working.
I have tried multiple methods of getting the y value to properly be set, the closest I got ended up drawing a straight diagonal lines, it wasn't curved like it was supposed to be. What can I do to properly set the y value so that the sin graph from [0, 2pi] will be properly drawn?
To try to clarify the issue:
The problem lies in changing the y value of my drawLine function. Basically, what I had working wouldn't draw the sin function properly because I only could figure out to lineraly increment it. It looked something like this:
/\
/ \ /
\/
The ultimate question I have is: How can I make it so my y coordinate will scale with the proper y coordinate of the sin(x) graph? In other words, how can I make it "not-straight" and properly curved like the sin(x) graph should.
Another way to look at the issue is how could I properly "scale" the y value of sin(x) with the pixels of my JFrame?
Thank you for your time I appreciate any help.
解决方案 Well, David Wallace mentioned in comments that you need to reduce your x
increment, but I think you are trying to get 2*PI
over the entire width
interval.
Use y = Math.sin(x*Math.PI*2/getWidth());
which will scale your x
values before plugging them into your sin
function.
这篇关于在java中绘制sin(x)图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!