绘图功能 [英] Graphing functions
问题描述
一般来说,我是Java的新手,更不用说使用GUI了,而且我正在做一个分配图以绘制3个函数:
(余弦的四瓣丁香) 受域限制
0,4
-10,10
0< x < 2pi
到目前为止,我已经对前两个函数做了一个粗略的图形,并且不知道如何绘制第三个函数.
我所拥有的:
import java.awt.*;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class DrawingStuff extends JComponent {
public void paintComponent(Graphics g)
{
//w is x, and h is y (as in x/y values in a graph)
int w = this.getWidth()/2;
int h = this.getHeight()/2;
Graphics2D g1 = (Graphics2D) g;
g1.setStroke(new BasicStroke(2));
g1.setColor(Color.black);
g1.drawLine(0,h,w*2,h);
g1.drawLine(w,0,w,h*2);
g1.drawString("0", w - 7, h + 13);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(2));
g2.setColor(Color.red);
//line1
Polygon p = new Polygon();
for (int x = 0; x <= 4; x++) {
p.addPoint(w+x, h - ((x*x*x) + x - 3));
}
g2.drawPolyline(p.xpoints, p.ypoints, p.npoints);
Polygon p1 = new Polygon();
for (int x = -10; x <= 10; x++) {
p1.addPoint(w + x, h - ((x*x*x)/100) - x + 10);
}
g2.drawPolyline(p1.xpoints, p1.ypoints, p1.npoints);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(800, 600);
frame.setTitle("Graphs");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
DrawingStuff draw = new DrawingStuff();
frame.add(draw);
frame.setVisible(true);
}
}
这给了我
我现在想做的是绘制余弦函数图,我不确定该如何绘制,因为它是cos(2x)而不是2cos(x).
我还想更改比例,以使分段看起来不会太小(也许是图形标记?但并不是特别重要).
我想添加一些内容,以便一次只显示一个功能.
要添加最后一个函数,请执行与以前相同的操作(创建带有点等的多边形),但要使用新函数:
addPoint(w + x, h - Math.round(scale*Math.cos(2*x)))
这比其他技巧有些棘手,因为Math.cos
返回double
,而addPoint
接受int
.为了解决这个问题,我们四舍五入到最近的int
.但是,余弦[-1,1]的范围仅包含我们可以舍入的三个整数.这产生了一个丑陋的余弦波(看起来像三角波).
为减轻这种丑陋感,使用了比例因子.例如,乘数10将使范围[-10,10]变大,其中有更多的整数,从而使波形更平滑.
此比例因子对于其他功能也很有用,即根据需要将其放大:
int scale = 10;
for (int x = 0; x <= 4; x++) {
p.addPoint(w+scale*x, h - scale*((x*x*x) + x - 3));
}
//...lines skipped
for (int x = -10; x <= 10; x++) {
p1.addPoint(w + scale*x, h - scale*((x*x*x)/100) - x + 10);
}
要一次只显示一个功能,只需在代码中包含if
语句即可.
假设每个功能都有一个JRadioButton
:
if(button1.isSelected()){
Polygon p = new Polygon();
for (int x = 0; x <= 4; x++) {
p.addPoint(w+x, h - ((x*x*x) + x - 3));
}
g2.drawPolyline(p.xpoints, p.ypoints, p.npoints);
}
else if(button1.isSelected()){
//other polygon
}
//etc...
I'm new to Java in general, not to mention working with GUIs and I'm working on an assignment to graph the 3 functions:
(the four leafed cloves for cosine) with domain restrictions
0,4
-10,10
0 < x < 2pi
I've so far made a crude graph of the first 2 functions, and no idea how to plot the third.
What I have:
import java.awt.*;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class DrawingStuff extends JComponent {
public void paintComponent(Graphics g)
{
//w is x, and h is y (as in x/y values in a graph)
int w = this.getWidth()/2;
int h = this.getHeight()/2;
Graphics2D g1 = (Graphics2D) g;
g1.setStroke(new BasicStroke(2));
g1.setColor(Color.black);
g1.drawLine(0,h,w*2,h);
g1.drawLine(w,0,w,h*2);
g1.drawString("0", w - 7, h + 13);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(2));
g2.setColor(Color.red);
//line1
Polygon p = new Polygon();
for (int x = 0; x <= 4; x++) {
p.addPoint(w+x, h - ((x*x*x) + x - 3));
}
g2.drawPolyline(p.xpoints, p.ypoints, p.npoints);
Polygon p1 = new Polygon();
for (int x = -10; x <= 10; x++) {
p1.addPoint(w + x, h - ((x*x*x)/100) - x + 10);
}
g2.drawPolyline(p1.xpoints, p1.ypoints, p1.npoints);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(800, 600);
frame.setTitle("Graphs");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
DrawingStuff draw = new DrawingStuff();
frame.add(draw);
frame.setVisible(true);
}
}
which gives me
What I'd like to do now is graph the cosine function, which I'm not sure how to plot because it's cos(2x) and not 2cos(x).
I'd also like to change the scaling so that the segments don't look so tiny (and maybe graph markers? but not particularly important).
And I'd like to possibly add something so that only one function is displayed at a time.
To add the last function, do the same as before (create a polygon with points, etc), but with your new function:
addPoint(w + x, h - Math.round(scale*Math.cos(2*x)))
It's a bit trickier than the others as Math.cos
returns a double
and addPoint
takes an int
. To get around this we round to the nearest int
. However, the range of cosine, [-1,1], only contains three integers we can round to. This makes for an ugly cosine wave (one that looks like a triangle wave).
To alleviate this ugliness, a scale factor is used. A factor of say, 10, would make the range [-10,10], in which there are many more integers, leading to a smoother wave.
This scale factor is also useful for your other functions, namely to make them bigger as you wanted:
int scale = 10;
for (int x = 0; x <= 4; x++) {
p.addPoint(w+scale*x, h - scale*((x*x*x) + x - 3));
}
//...lines skipped
for (int x = -10; x <= 10; x++) {
p1.addPoint(w + scale*x, h - scale*((x*x*x)/100) - x + 10);
}
To display only one function at a time, just have if
statements in your code.
Say you have a JRadioButton
for each function:
if(button1.isSelected()){
Polygon p = new Polygon();
for (int x = 0; x <= 4; x++) {
p.addPoint(w+x, h - ((x*x*x) + x - 3));
}
g2.drawPolyline(p.xpoints, p.ypoints, p.npoints);
}
else if(button1.isSelected()){
//other polygon
}
//etc...
这篇关于绘图功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!