首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >stdDraw绘制背景java

stdDraw绘制背景java
EN

Stack Overflow用户
提问于 2019-02-03 04:39:28
回答 1查看 1.5K关注 0票数 0

我正在做这个项目,用java的stdDraw模拟太阳系。我想做出改变,在那里我可以展示玉米的痕迹,因为我的最终目标是让这颗玉米飞成一颗心形,如果我能画出玉米的轨迹,我就能呈现出它的心脏。现在我一直在试着去做,但是看起来背景图像挡住了跟踪。如果我注释掉背景图像,所有的行星都会显示出它们的踪迹。我不知道该怎么办,救命!以下是对象类:

代码语言:javascript
复制
public class BodyExtreme{
    public double xxPos;
    public double yyPos;
    public double xxVel;
    public double yyVel;
    public double mass;
    public String imgFileName;
    private static final double G = 6.67e-11;


public BodyExtreme(double xP, double yP, double xV, double yV, double m, String img){
    xxPos = xP;
    yyPos = yP;
    xxVel = xV;
    yyVel = yV;
    mass = m;
    imgFileName = img;
}

public BodyExtreme(BodyExtreme b){
    xxPos = b.xxPos;
    yyPos = b.yyPos;
    xxVel = b.xxVel;
    yyVel = b.yyVel;
    mass = b.mass;
    imgFileName = b.imgFileName;
}

public double calcDistance(BodyExtreme b) {
    double dx = b.xxPos - this.xxPos;
    double dy = b.yyPos - this.yyPos;
    return Math.sqrt(dx * dx + dy * dy);
}

public double calcForceExertedBy(BodyExtreme b) {
    if (this.calcDistance(b) == 0) {
        return 0;
    } else {
        return (G * b.mass * this.mass)/(this.calcDistance(b) * this.calcDistance(b));
    }
}

public double calcForceExertedByX(BodyExtreme b) {
    return (this.calcForceExertedBy(b) * (b.xxPos - this.xxPos) / this.calcDistance(b));
}

public double calcForceExertedByY(BodyExtreme b) {
    return (this.calcForceExertedBy(b) * (b.yyPos - this.yyPos) / this.calcDistance(b));
}

public double calcNetForceExertedByX(BodyExtreme[] b) {
    int i = 0;
    double sum = 0;
    while (i < b.length) {
        if (this.equals(b[i])) {
            sum += 0;
            i += 1;
        } else {
            sum = sum + this.calcForceExertedByX(b[i]);
            i += 1;
        }
    } return sum;
}

public double calcNetForceExertedByY(BodyExtreme[] b) {
    int i = 0;
    double sum = 0;
    while (i < b.length) {
        if (this.equals(b[i])) {
            sum += 0;
            i += 1;
        } else {
            sum = sum + this.calcForceExertedByY(b[i]);
            i += 1;
        }
    } return sum;
}

public void update(double dt, double fX, double fY) {
    double ax = fX / this.mass;
    double ay = fY / this.mass;
    double vx = this.xxVel + dt * ax;
    double vy = this.yyVel + dt * ay;
    double px = this.xxPos + dt * vx;
    double py = this.yyPos + dt * vy;
    this.xxPos = px;
    this.yyPos = py;
    this.xxVel = vx;
    this.yyVel = vy;
}

public void draw() {
    StdDraw.picture(this.xxPos, this.yyPos, "images/" + this.imgFileName);
}

public void lonelyplanet_update1(){
    this.xxPos = this.xxPos + 45000000; 
    this.yyPos = this.yyPos + 100000000;
    this.draw();
}

}

以下是主要的方法类:

代码语言:javascript
复制
import java.util.Scanner; 
public class NBodyExtreme{
    public static double readRadius(String name) {
        In in = new In(name);
        int NumPlanets = in.readInt();
        double Size = in.readDouble();
        return Size;
    }

public static BodyExtreme[] readBodies(String name) {
    In in = new In(name);
    int NumPlanets = in.readInt();
    double Size = in.readDouble();
    BodyExtreme[] bodies = new BodyExtreme[NumPlanets];
    int i = 0;
    while (i < NumPlanets) {
        bodies[i] = new BodyExtreme(in.readDouble(), in.readDouble(), in.readDouble(), in.readDouble(), in.readDouble(), in.readString());
        i += 1;
    }
    return bodies;
}

public static void main(String[] args) {
    double T = Double.parseDouble(args[0]);         /** Stoping Time */
    double dt = Double.parseDouble(args[1]);        /** Time Step */
    String filename = args[2];
    BodyExtreme[] bodies = readBodies(filename);            /** Array of Bodies */
    double radius = readRadius(filename);               /** Canvas Radius */
    In in = new In(filename);
    int NumPlanets = in.readInt();                  /** Number of Planets */
    String imageToDraw = "images/starfield.jpg";    /** Background */
    StdDraw.enableDoubleBuffering();
    StdDraw.setScale(-2*radius, 2*radius);
    StdDraw.clear();
    StdDraw.picture(0, 0, imageToDraw);             /** Draw Initial Background */
    StdDraw.show();
    int k = 0;
    while (k < NumPlanets-1) {                      /** Draw Planets */
        bodies[k].draw();
        k += 1;
    }

    StdDraw.enableDoubleBuffering();
    double time = 0.0;
    while (time < T) {
        double[] xForces = new double[NumPlanets-1];
        double[] yForces = new double[NumPlanets-1];
        int i = 0;
        while (i < NumPlanets-1) {
            xForces[i] = bodies[i].calcNetForceExertedByX(bodies);
            yForces[i] = bodies[i].calcNetForceExertedByY(bodies);
            i += 1;
        }
        i = 0;
        while (i < NumPlanets-1) {
            bodies[i].update(dt, xForces[i], yForces[i]);
            i += 1;
        }
        bodies[NumPlanets-1].lonelyplanet_update1();
        bodies[NumPlanets-1].draw();
        StdDraw.show();
        StdDraw.picture(0, 0, imageToDraw);
        int j = 0;
        while (j < NumPlanets) {
            bodies[j].draw();
            j += 1;
        }
        StdDraw.show();
        StdDraw.pause(10);
        }
        time += dt;
}

}

当使用背景图像阻塞路由跟踪时,会发生这样的情况:

这就是当跟踪没有被阻塞时会发生的情况,但我只希望玉米的跟踪不要成为blokcekd。

EN

回答 1

Stack Overflow用户

发布于 2019-02-06 11:31:51

动画逻辑中有一个缺陷:

要动画你的屏幕,你必须

  1. 更新模型(每个元素,例如。背景或bodY),
  2. 然后使用StdDraw.picture(..)
  3. 最后(通过使用StdDraw.show();)使该内容可见。

对于动画中的每一个时间步骤(while (time < T){..}),您必须完成所有三个步骤。

代码语言:javascript
复制
public static void main(String[] args) {
    ...
    StdDraw.enableDoubleBuffering(); //it's enough to call this only once!
    double time = 0.0;
    while (time < T) {
        //do all the update stuff first, as shown in your code above

        //then draw ONCE the BackGround:
        StdDraw.picture(0, 0, imageToDraw);

        //then draw all the planets (yes, i missed one)
        while (j < NumPlanets) {
            bodies[j].draw();
            j += 1;
        }

        //finally make all the previous drawing visible  
        StdDraw.show();

        //wait a bit for the next animation step
        StdDraw.pause(10);
        time += dt;
    }

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54500002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档