gpt4 book ai didi

java - 需要帮助在 Swing 中制作简单的运动模糊/轨迹效果

转载 作者:行者123 更新时间:2023-12-02 00:45:33 25 4
gpt4 key购买 nike

我目前无事可做,所以我只想创建一个简单的烟花程序。它工作得很好,但现在我想在 starterParticle (Rocket) 中添加一点 MotionBlur-Trail,但我想不出一种方法来做到这一点。

我想我需要一个根据火箭速度计算轨迹的函数。该函数需要计算需要有多少粒子以及它们具有哪些不透明度和坐标。

这是我的尝试(位于 Firework 类中):

public void draw(Graphics g) {
for(Sparkle s : sparkles) {
s.draw(g);
}
if(!exploded) {
for(int alpha = 255, i = 0; alpha >= 5; alpha-=starterSparkle.ySpeed*10, i++) {
g.setColor(new Color(starterSparkle.myColor.getRed(), starterSparkle.myColor.getGreen(),
starterSparkle.myColor.getBlue(), alpha));
g.fillRect((int) starterSparkle.x, (int) (starterSparkle.y + (starterSparkle.ySpeed * i)),
(int) this.myPanel.SPARKLESIZE_PIXELS, (int) this.myPanel.SPARKLESIZE_PIXELS);
}
}
}

烟花类:

package Firework;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Random;

public class Firework {

public Window myPanel;
public ArrayList<Sparkle> sparkles;
public Sparkle starterSparkle;
public boolean exploded;
public boolean expired;
public Random rand = new Random();
public Color groupColor;

public ArrayList<Sparkle> starterTrail;

public Firework(Window myPanel) {
this.myPanel = myPanel;
this.exploded = false;
this.groupColor = new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255));
sparkles = new ArrayList<Sparkle>();
starterSparkle = new Sparkle(this, true);
starterTrail = new ArrayList<Sparkle>();
sparkles.add(starterSparkle);
for(int i = 0; i < myPanel.SPARKLES_PER_FIREWORK; i++) {
sparkles.add(new Sparkle(this,false));
}
}

public void draw(Graphics g) {
for(Sparkle s : sparkles) {
s.draw(g);
}
if(!exploded) {
for(int alpha = 255, i = 0; alpha >= 5; alpha-=starterSparkle.ySpeed*10, i++) {
g.setColor(new Color(starterSparkle.myColor.getRed(), starterSparkle.myColor.getGreen(),
starterSparkle.myColor.getBlue(), alpha));
g.fillRect((int) starterSparkle.x, (int) (starterSparkle.y + (starterSparkle.ySpeed * i)),
(int) this.myPanel.SPARKLESIZE_PIXELS, (int) this.myPanel.SPARKLESIZE_PIXELS);
}
}
}

public void move() {
for(Sparkle s : sparkles) {
s.move();
}
if(sparkles.get(0).ySpeed < 0.5F) {
explode();
}
}

public void explode() {
int counter = 0;
if(!exploded) {
for(Sparkle s : sparkles) {
s.show = true;
//System.out.println(counter++);
if(rand.nextInt(2) == 1) {
s.shuffle(true);
System.out.println("im individual!");
} else {
s.shuffle(false);
System.out.println("im not individual!");
}
}
this.exploded = true;
System.out.println("exploded!");
}
if(sparkles.get(0).lifespan < 1) {
expired = true;
}
}

}

闪光等级:

package Firework;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

public class Sparkle {

public Random randomizer;
public float x,y;
public int lifespan;
public static float gravity = 0.95F;
public float ySpeed;
public float xSpeed;
public Color myColor;
public boolean show;
public Firework myFirework;
public static int counter = 0;

public Sparkle(Firework myFirework, boolean show) {
this.randomizer = new Random();
this.lifespan = 255;
this.myFirework = myFirework;
this.show = show;
this.myColor = Color.WHITE;
this.xSpeed = 0F;
if(show) {
this.y = myFirework.myPanel.getHeight();
this.x = randomizer.nextInt(myFirework.myPanel.getWidth());
this.ySpeed = (randomizer.nextFloat()*30) + 15F;
}

}

public void draw(Graphics g) {
if(show) {
g.setColor(myColor);
g.fillOval((int) x, (int) y, (int) myFirework.myPanel.SPARKLESIZE_PIXELS, (int) myFirework.myPanel.SPARKLESIZE_PIXELS);
Color temp = myColor;
myColor = new Color(temp.getRed(), temp.getGreen(), temp.getBlue(), lifespan);
}
}

public void move() {
this.y -= this.ySpeed;
this.x += this.xSpeed;
this.xSpeed *= this.gravity;
this.ySpeed *= this.gravity;
if(myFirework.exploded) {
lifespan = (int) ( (float) lifespan * 0.98F );
}
}

public void shuffle(boolean isIndividual) {

this.myColor = myFirework.groupColor;
if(isIndividual) {
this.myColor = new Color(randomizer.nextInt(255), randomizer.nextInt(255), randomizer.nextInt(255));

}
float maxOverall = randomizer.nextInt(4);
this.x = this.myFirework.starterSparkle.x;
this.y = this.myFirework.starterSparkle.y;
this.ySpeed = (randomizer.nextFloat()*4F) - 2F;
this.xSpeed = maxOverall - Math.abs(ySpeed);
if(randomizer.nextInt(2) == 1) {
//System.out.println("turned vorzeichen");
xSpeed -= (2*xSpeed);
//System.out.println(xSpeed);
}
//this.xSpeed = (randomizer.nextFloat()*50F) - 25F;
}

}

如果您能给我一些建议,那就太好了,您不需要编写代码,如果您给我一些想法,它也会对我有帮助。

是的,我知道一切都是公开的,但我只是想写一个快速的烟花程序,仅此而已:)

如果你想测试的话我也可以给你完整的代码^^

最佳答案

为什么不对闪光轨迹中的点设置不同的类?将每个点称为 SparkleTrail,让它们静止并让它们以缓慢降低的不透明度绘制自己,最后使它们消失(= 将其从 draw() 循环中删除)一旦它们的不透明度达到 0。

建议的更改:

  • 新的 SparkleTrail 类,有两个坐标,初始不透明度为 0.5。当您调用其 draw 方法时绘制自身。有一个 slowlyVanish 方法,每当您调用它时,它都会将不透明度降低 0.01,您将每隔 1/50 秒执行一次(或无论您的帧速率是多少),因此在大约 1/50 秒后慢慢消失。 1 秒。
  • 每移动 Sparkle 10 帧就创建一次它们,并将它们与当前 Sparkle 的位置一起添加到 trail 数组列表中。当闪光移动缓慢时,这将使轨迹变得更亮,当闪光移动非常快时,这将使轨迹分开。
  • 每当重绘所有内容时,都会对 trail 数组列表中的所有 SparkleTrail 实例调用 draw。确保调用它们的 slowlyVanish 方法,当不透明度达到零时,将它们从 trail 中删除。

关于java - 需要帮助在 Swing 中制作简单的运动模糊/轨迹效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57902518/

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