Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.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_Graph_Graphing - Fatal编程技术网

Java 函数图形的描绘

Java 函数图形的描绘,java,graph,graphing,Java,Graph,Graphing,一般来说,我对Java是新手,更不用说使用GUI了,我正在进行一项分配,以绘制3个函数: (四叶丁香代表余弦) 有域名限制 0,4 -10,10 0

一般来说,我对Java是新手,更不用说使用GUI了,我正在进行一项分配,以绘制3个函数:

(四叶丁香代表余弦) 有域名限制

0,4

-10,10

0 到目前为止,我已经绘制了前两个函数的粗略图表,但不知道如何绘制第三个函数

我所拥有的:

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);
    }
}
import java.awt.*;
导入javax.swing.JComponent;
导入javax.swing.JFrame;
公共类DrawingStaff扩展JComponent{
公共组件(图形g)
{   
//w是x,h是y(如图中的x/y值)
int w=this.getWidth()/2;
int h=this.getHeight()/2;
图形2d g1=(图形2d)g;
g1.设定行程(新基本行程(2));
g1.设置颜色(颜色为黑色);
g1.抽绳(0,高,宽*2,高);
g1.抽绳(w,0,w,h*2);
g1.抽绳(0英寸,w-7英寸,h+13英寸);
图形2d g2=(图形2d)g;
g2.设定行程(新基本行程(2));
g2.设置颜色(颜色为红色);
//第1行
多边形p=新多边形();

对于(int x=0;x要添加最后一个函数,请执行与之前相同的操作(创建带点的多边形等),但使用新函数:

addPoint(w + x, h - Math.round(scale*Math.cos(2*x)))
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...
它比其他的有点复杂,因为
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);
}

要添加最后一个函数,请执行与之前相同的操作(创建带点的多边形等),但使用新函数:

addPoint(w + x, h - Math.round(scale*Math.cos(2*x)))
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...
它比其他的有点复杂,因为
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);
}

+基于已接受的答案生成所需的
cos(2x)
图形

    final double MAX_X = 2 * Math.PI;
    final double SCALE = w / MAX_X;

    for (int x = 0; x <= w; ++x) {
        double xScaled = x / SCALE;
        p.addPoint(w + x, h - (int)Math.round(SCALE * Math.cos(2 * xScaled)));
    }
final double MAX_X=2*Math.PI;
最终双刻度=w/MAX_X;

对于(int x=0;x+基于已接受的答案生成请求的
cos(2x)
图形

    final double MAX_X = 2 * Math.PI;
    final double SCALE = w / MAX_X;

    for (int x = 0; x <= w; ++x) {
        double xScaled = x / SCALE;
        p.addPoint(w + x, h - (int)Math.round(SCALE * Math.cos(2 * xScaled)));
    }
final double MAX_X=2*Math.PI;
最终双刻度=w/MAX_X;

对于(int x=0;x)为什么cos 2x的技术与2 cos x的技术有任何不同?这只是一个不同的公式。你所说的“一次”是什么意思?只需删除对绘制关联函数多边形的调用,它就不会绘制该多边形。我的意思是,我想绘制所有3个多边形,但每次只显示一个。@Ozymandias为了显示下一个多边形做了哪些更改?时间?鼠标单击?我在想类似单选按钮的事情?@river为什么cos 2x的技术与此不同t代表2 cos x?这只是一个不同的公式。你说的“一次”是什么意思?只需删除对绘制关联函数多边形的调用,它就不会绘制该多边形。我的意思是,我想绘制所有3个多边形,但一次只能显示一个。@Ozymandias为了显示下一个多边形做了哪些更改?一次?鼠标单击?我想是单选按钮?@RiverAddPoint方法只接受整数,而不接受整数es到双精度。也适用于0到2pi的域。如何解决此问题?addPoint方法仅接受整数,而cos计算为双精度。同样适用于0到2pi的域。如何解决此问题?