gpt4 book ai didi

java - 如何在动画后面留下痕迹?

转载 作者:行者123 更新时间:2023-11-30 03:01:32 25 4
gpt4 key购买 nike

我有一个程序,可以获取太空中两个行星/天体的信息,并真实地动画显示它们围绕彼此的轨道。它有效,但当我重新绘制时,它每次都会清除屏幕并且不会留下痕迹。

我的问题是我想留下痕迹,尽管我在网上找到的任何答案都只解释了如何消除痕迹。不过,我没有这个问题。

在其他计算机上,我可以在绘制方法中省略 super.paintComponent() ,这会导致它留下痕迹,但在这台计算机上,它不会这样做,它似乎会自动清除屏幕。那么我怎样才能有效地在我的轨道行星后面画出一条轨迹呢?我的代码如下。

JPanel 类第一:

import javax.swing.*;
import java.awt.*;
/**
* Created by chris on 3/2/16.
*/
public class SpacePanel2 extends JPanel{
private Body2[] planets;
public static final Dimension SCREENSIZE = Toolkit.getDefaultToolkit().getScreenSize();
public static double scale = 5e6; //m/p
public static Color[] colors = {Color.black, Color.red};

public SpacePanel2(Body2[] planets) {
this.planets = planets;
this.setPreferredSize(SCREENSIZE);
}

@Override
public void paint(Graphics g){
for (int i = 0; i < planets.length; i++) {
g.setColor(colors[i]);
int r = planets[i].getPixelRadius()/2;
int x = planets[i].getPixelX();
int y = planets[i].getPixelY();
g.fillOval(x, y, r, r);
}
}
}

车身等级:

/**
* Created by chris on 3/2/16.
*/
public class Body2 {
private double mass; //in kilograms
private double radius; //in meters
private static final double GRAVITATIONAL_CONSTANT = 6.67408e-11;
private static final double AVERAGE_DENSITY = 5515; //kg/m^3

/**
* Movement variables
*/
private double dx; //in m/s
private double dy; //in m/s

private double x; //in m
private double y; //in m

public Body2() {
radius = 1;
mass = AVERAGE_DENSITY;
x = 0;
y = 0;
dx = 0;
dy = 0;
}

public double getX() {
return x;
}

public double getY() {
return y;
}

public double getMass() {
return mass;
}

public double getRadius() {
return radius;
}


public int getPixelX() {
return (int)((this.x-radius)/SpacePanel2.scale);
}

public int getPixelY() {
return (int)((this.y-radius)/SpacePanel2.scale);
}
public int getPixelRadius(){
return (int)(this.radius/SpacePanel2.scale);
}

public void setMass(double mass) {
this.mass = mass;
}

public void setRadius(double radius) {
this.radius = radius;
}

public void setDx(double dx) {
this.dx = dx;
}

public void setDy(double dy) {
this.dy = dy;
}

public void setX(double x) {
this.x = x;
}

public void setY(double y) {
this.y = y;
}


public void exertForce2(double diffY, double diffX, double F){
double dist = Math.sqrt(diffY*diffY + diffX*diffX);
double ratio = F / dist;
this.dy = this.dy + ratio*diffY/this.mass;
this.dx = this.dx + ratio*diffX/this.mass;
}

public void tick(double timeScale) {
x+=(dx/1000.0)*timeScale;
y+=(dy/1000.0)*timeScale;
}

public static double getForce(Body2 a, Body2 b){
double dX = a.getX() - b.getX();
double dY = a.getY() - b.getY();
double distance = Math.sqrt(Math.pow(dX,2)+Math.pow(dY,2));
return (a.getMass()*b.getMass()*GRAVITATIONAL_CONSTANT)/(distance*distance);
}

public static double getStandardMass(double radius){
return (4.0/3.0)*Math.pow(radius, 3) * Math.PI;
}

public double getDy() {
return dy;
}

public double getDx() {
return dx;
}

public static double predictCentripetalForce(Body2 sun, Body2 planet){
return Math.sqrt(getForce(planet, sun)*(sun.getY()-planet.getY())/planet.mass);
}
}

主类:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
* Created by chris on 3/2/16.
*/
public class MainSpace2 {
static JFrame frame;
static SpacePanel2 panel;

static int fps = 60;
static boolean getLarger = false;
static boolean getSmaller = false;
static Dimension size = Toolkit.getDefaultToolkit().getScreenSize();

public static void main(String[] args) {

Body2[] test = new Body2[2];

Body2 sun = new Body2();
sun.setRadius(696300000);
sun.setMass(1.989e30);
sun.setX(getScope('x') / 2);
sun.setY(getScope('y') / 2);
sun.setDx(0);
sun.setDy(0);
test[0] = sun;
int literalSizeSun = (int)(sun.getRadius()/SpacePanel2.scale);

Body2 mercury = new Body2();
mercury.setRadius(24400000);
mercury.setMass(Body2.getStandardMass(mercury.getRadius()));
mercury.setDx(Body2.predictCentripetalForce(sun, mercury)*2);
mercury.setDy(0);
mercury.setX(sun.getX());
mercury.setY(sun.getY() + 2 * sun.getRadius());
test[1] = mercury;
int literalSizeMercury = (int)(mercury.getRadius()/SpacePanel2.scale);


frame = new JFrame();
frame.setPreferredSize(size);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new SpacePanel2(test);

frame.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyChar()) {
case '-':
getSmaller = true;
getLarger = false;
break;
case '=':
getLarger = true;
getSmaller = false;
break;
}
}

@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyChar()) {
case '-':
getSmaller = false;
break;
case '=':
getLarger = false;
break;
}
}
});

double timeScale = 60*24;
Timer time = new Timer((int) (1000.0 / fps), new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double F = Body2.getForce(test[0], test[1]);
double dY = test[1].getY() - test[0].getY();
double dX = test[1].getX() - test[0].getX();
test[0].exertForce2(dY, dX, F);
test[1].exertForce2(-dY, -dX, F);
for (int j = 0; j < test.length; j++) {
test[j].tick(timeScale);
}
panel.repaint(sun.getPixelX(), sun.getPixelY(), literalSizeSun, literalSizeSun);
panel.repaint(mercury.getPixelX(), mercury.getPixelY(), literalSizeMercury, literalSizeMercury);
}
});

frame.add(panel);
frame.pack();
frame.setVisible(true);
time.start();
}

public static double getScope(char k) {
switch (k) {
case 'x':
return size.width * SpacePanel2.scale;
case 'y':
return size.height * SpacePanel2.scale;
default:
return 0;
}
}
}

最佳答案

  1. 自定义绘制是通过重写 paintComponent() 方法而不是 Paint() 来完成的。

  2. 如果留下连续的轨迹,一旦进行 360 度旋转,您将不会再看到任何动画,因此我认为您最终需要清除屏幕。

如果你想留下痕迹,你可以保留你想要绘制的对象的ArrayList。然后,在 paintComponent() 方法中,您可以迭代 List。这将允许您从列表中添加/删除对象,以便您可以控制要绘制每个动画的对象数量。

查看 Custom Painting Approaches 中的 DrawOnComponent 示例有关此方法的示例。因此,您的动画逻辑基本上会向列表中添加一个新对象(并可能在达到一定限制后删除一个对象?)。然后您只需在面板上调用 repaint() 即可绘制所有对象。

您已经有了用于绘制每个行星的列表。因此,您需要在每个动画中将每个行星的新位置添加到列表中。

或者,该链接显示了如何绘制 BufferedImage。但这种方法不允许你在画完成后将其移除。因此,这取决于您使用哪种方法的具体要求。

关于java - 如何在动画后面留下痕迹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35816588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com