Java 使正弦图移动 公共类SimpleHarmonic{ 公共静态void main(字符串[]args){ 标准数据集(0900); 标准偏差(0700); while(true){ 标准颜色(标准黑色); StdDraw.line(0350900350);//x轴 标准测线(450,0450900);//y轴 标准颜色(标准红色); 对于(double x=-450;x

Java 使正弦图移动 公共类SimpleHarmonic{ 公共静态void main(字符串[]args){ 标准数据集(0900); 标准偏差(0700); while(true){ 标准颜色(标准黑色); StdDraw.line(0350900350);//x轴 标准测线(450,0450900);//y轴 标准颜色(标准红色); 对于(double x=-450;x,java,stddraw,Java,Stddraw,我查看了您正在使用的StdDraw类,看起来您想要的是 StdDRaw.show(int)method,此方法注释说明: public class SimpleHarmonic { public static void main(String[] args) { StdDraw.setXscale(0,900); StdDraw.setYscale(0,700); while (true) { StdDraw.se

我查看了您正在使用的
StdDraw
类,看起来您想要的是

StdDRaw.show(int)
method,此方法注释说明:

public class SimpleHarmonic {
    public static void main(String[] args) {
        StdDraw.setXscale(0,900);
        StdDraw.setYscale(0,700);

        while (true) {
            StdDraw.setPenColor(StdDraw.BLACK);
            StdDraw.line(0,350,900,350); // x-axis
            StdDraw.line(450,0,450,900); // y-axis
            StdDraw.setPenColor(StdDraw.RED);

            for (double x = -450; x <= 450; x += 0.5) {
                double y = 50 * Math.sin(x * (Math.PI / 180));
                int Y = (int) y;
                int X = (int) x;
                StdDraw.line(450 + X, 350 - Y, 450 + X, 350 - Y);
            }
            StdDraw.clear();

        }
    }
}

代码中的一些改进

1在你的循环中,你画的每个“点”都是递增的
.5
。因为X值实际上是1个像素,所以你去
.5
而不是
1
。1实际上是你在这个环境中可以看到的最小的点。我建议至少是
X+=1

    double offset = 0;
    while (true) {
        offset+=1; // move the frame slightly
        StdDraw.show(10); // defer repainting for 10 milisecoinds

        StdDraw.clear(); // clear before painting

        StdDraw.setPenColor(StdDraw.BLACK);
        StdDraw.line(0,350,900,350); // x-axis
        StdDraw.line(450,0,450,900); // y-axis
        StdDraw.setPenColor(StdDraw.RED);

        for (double x = -450; x <= 450; x += 0.5) {
            // apply the offset inside of calculation of Y only such that it 
            // slowly "moves" the sin wave
            double y = 50 * Math.sin((offset+x) * (Math.PI / 180));
            int Y = (int) y;
            int X = (int) x;
            StdDraw.line(450 + X, 350 - Y, 450 + X, 350 - Y);
        }

        StdDraw.show(); // end animation frame. force a repaint 
    }
3这不是您的代码,但在
StdDraw.init
方法中,您可以设置一些渲染提示,以允许更清晰的线条。这应该会使它看起来更好

double prevX = -450;
double prevY = 50 * Math.sin((prevX+offset) * (Math.PI / 180)); // seed the previous Y to start
for (double x = 0; x <= 450; x += 3) {
    double y = 50 * Math.sin((x+offset) * (Math.PI / 180));
    StdDraw.line(450 + (int)prevX, 350 - (int)prevY, 450 + (int)x, 350 - (int)y);
    prevX = x;
    prevY = y;
}

结合所有这些,这就是我写的

offscreen.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, 
                           RenderingHints.VALUE_STROKE_PURE);
publicstaticvoidmain(字符串[]args){
标准数据集(0900);
标准偏差(0700);
双偏移=0;
while(true){
StdDraw.show(10);
StdDraw.clear();
偏移量-=1;
标准颜色(标准黑色);
StdDraw.line(0350900350);//x轴
标准测线(450,0450900);//y轴
标准颜色(标准红色);
双prevX=0;
double-prevY=50*Math.sin((prevX+offset)*(Math.PI/180));//为上一个Y设置种子以开始
标准填充圆(450+上一个,350-上一个,5);

对于(double x=0;x好的,您对所选的包有一个挑战。通常您会将图形移到左侧,并重新绘制右侧的最后一个像素。在这里,您必须完全重新绘制。绘制一个白色矩形,并每次再次绘制轴和图形。我怀疑它是否正确fluent@norbert这意味着我们必须清除屏幕并且一遍又一遍地使用不同的点重新绘制?这个包看起来就是这样(我在谷歌上搜索并阅读了API)@norbert当前的问题是,我当前的程序逐点绘制图形,而不是一次绘制整个正弦图。这意味着当我移动下一个点时,将很难看到差异。我将更新我的代码。逐点绘制基本图形不支持的曲线几乎是不可避免的所有库和硬件(因此,这一点非常深入)
double prevX = -450;
double prevY = 50 * Math.sin((prevX+offset) * (Math.PI / 180)); // seed the previous Y to start
for (double x = 0; x <= 450; x += 3) {
    double y = 50 * Math.sin((x+offset) * (Math.PI / 180));
    StdDraw.line(450 + (int)prevX, 350 - (int)prevY, 450 + (int)x, 350 - (int)y);
    prevX = x;
    prevY = y;
}
offscreen.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, 
                           RenderingHints.VALUE_STROKE_PURE);
public static void main(String[] args) {
    StdDraw.setXscale(0,900);
    StdDraw.setYscale(0,700);

    double offset = 0;
    while (true) {
        StdDraw.show(10);
        StdDraw.clear();
        offset-=1;

        StdDraw.setPenColor(StdDraw.BLACK);
        StdDraw.line(0,350,900,350); // x-axis
        StdDraw.line(450,0,450,900); // y-axis
        StdDraw.setPenColor(StdDraw.RED);


        double prevX = 0;
        double prevY = 50 * Math.sin((prevX+offset) * (Math.PI / 180)); // seed the previous Y to start
        StdDraw.filledCircle(450 + prevX, 350 - prevY, 5);

        for (double x = 0; x <= 450; x += 3) {
            double y = 50 * Math.sin((x+offset) * (Math.PI / 180));
            StdDraw.line(450 + (int)prevX, 350 - (int)prevY, 450 + (int)x, 350 - (int)y);
            prevX = x;
            prevY = y;
        }
        StdDraw.show();

    }
}