Java 褪色算法不起作用?

Java 褪色算法不起作用?,java,multithreading,algorithm,colors,fade,Java,Multithreading,Algorithm,Colors,Fade,我试图在两种颜色和一个计时器之间创建一个褪色/过渡颜色算法;计时器将确定颜色之间交换的速度。唯一的问题是:我添加的渐变对象越多,它们的移动速度就越快。例如:如果我添加一个StandardFade(类)对象,它将在我给定的计时器(代码中的alpha)上运行。但是,如果屏幕上出现更多“淡入淡出”的对象,则计时器不再相关,并且它们都以相同的速度移动,随着每个对象的移动速度越来越快。有人能解释为什么吗 //Test Game Class, Implements the StandardFades imp

我试图在两种颜色和一个计时器之间创建一个褪色/过渡颜色算法;计时器将确定颜色之间交换的速度。唯一的问题是:我添加的渐变对象越多,它们的移动速度就越快。例如:如果我添加一个StandardFade(类)对象,它将在我给定的计时器(代码中的alpha)上运行。但是,如果屏幕上出现更多“淡入淡出”的对象,则计时器不再相关,并且它们都以相同的速度移动,随着每个对象的移动速度越来越快。有人能解释为什么吗

//Test Game Class, Implements the StandardFades
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;

public class TestGame extends Canvas implements Runnable{

private static final long serialVersionUID = -7267473597604645224L;

private Thread thread;
private boolean running = false;
private boolean consoleFPS = true;
private boolean titleFPS = true;

private TestWindow window;

//Fade objects, two is for a really long timer to test to see if they would transition at different times
StandardFade one = new StandardFade(Color.RED,Color.BLUE,.005);
StandardFade two = new StandardFade(Color.RED,Color.GREEN,.00000000000000001);
StandardFade three = new StandardFade(Color.RED,Color.YELLOW,.000005);

private int currentFPS;
private int frames;

public int levelNum;

public TestGame(int width, int height, String title){
    this.window = new TestWindow(width,height, title, this);

    this.initFades();
    this.start();
}

private synchronized void start(){
    if(running)
        return;
    else{
        this.thread = new Thread(this);
        this.thread.start();
        this.running = true;
    }
}

private synchronized void stop(){
    if(!this.running)
        return;
    else{
        try{
            this.thread.join();
        }catch(InterruptedException e){
            e.printStackTrace();
        }
        this.running = false;
        System.exit(0);
    }
}
/**
 * This game loop was provided by online sources, though there are many examples
 * of a game loop online.
 * 
 * @author RealTutsGML
 */
public void run() {
    requestFocus(); //Focuses the click/input on the frame/canvas.
    long lastTime = System.nanoTime(); //The current system's nanotime.
    double ns = 1000000000.0 / 60.0; //Retrieves how many nano-seconds are currently in one tick/update.
    double delta = 0; //How many unprocessed nanoseconds have gone by so far.
    long timer = System.currentTimeMillis();
    int frames = 0; //The frames per second.
    int updates = 0; //The updates per second.
    while (running) {

        boolean renderable = false; //Determines if the game should render the actual graphics.

        long now = System.nanoTime();//At this point, the current system's nanotime once again.
        delta += (now - lastTime) / ns;
        lastTime = now;
        //If the amount of unprocessed ticks is or goes above one...
        //Also determines if the game should update or not/render. Approximately sixty frames per second.
        while (delta >= 1) {
            tick();

            delta--;
            updates++;

            renderable = true;
        }

        if(renderable){
            frames++;
            render();
        }

        if (System.currentTimeMillis() - timer > 1000) {
            timer += 1000;
            System.out.println(frames);
            updates = 0;
            frames = 0;
        }
    }

    this.stop();
}

/**
 * This method should tick everything that needs to be updated via positioning, 
 * mouse input, etc.
 */
private void tick(){
    /********************PUT ALL TICKABLE METHODS IN THIS METHOD; ALL CALCULATIONS, EVERYTHING*********************/
    //this.stdFadeHandler.get(0).tick();

    //this.stdFadeHandler.tick();

    one.tick();
    two.tick();
    three.tick();
    /**********************************END OF TICK METHOD INFORMATION AND METHODS******************************/
}

private void render(){
    BufferStrategy bs = this.getBufferStrategy();

    if(bs == null){
        createBufferStrategy(3);
        return;
    }

    Graphics g = bs.getDrawGraphics();
    Graphics2D g2 = (Graphics2D) g;

    g2.setColor(Color.BLACK);       
    g2.fillRect(0, 0, window.getWidth(), window.getHeight());
    /*******************PLACE ALL DRAWING INSTRUCTIONS WITHIN THIS SECTION OF THE RENDER METHOD*************************/

    g2.setColor(one.getColor());
    g2.fillRect(20, 20, 200, 200);
    g2.setColor(two.getColor());
    g2.fillRect(20, 300, 200, 200);
    g2.setColor(three.getColor());
    g2.fillRect(20, 540, 200, 200);

    //this.stdFadeHandler


    /*******************DO NOT PLACE ANY MORE DRAWING INSTRUCTIONS WITHIN THIS SECTION OF THE RENDER METHOD***************/

    g.dispose();
    g2.dispose();

    bs.show();
}

private void initFades(){


}

public static void main(String[] args){

    TestGame stdGame = new TestGame(800,800,"Test Standard Game");
}
下面是制作帧的实际类:StandardFade

import java.awt.Color;
import java.awt.Graphics2D;

public class StandardFade {

private static float time = 0;
private static boolean firstColor = true;

private Color color1;
private Color color2;
private double alpha;

private Color fadeColor;

/**
 * Lines that implement the 
 * r,g and b values come from Princeton University; I will author
 * them in at the bottom.
 * 
 * Method takes two parameters and fades them into one another according to a 
 * timer/clock value: alpha.
 * @param c1 First color to be used. 
 * @param c2 Second color to be used. 
 * @param alpha How fast colors should shift. 0 <= n <= 1. 
 * Closer value is to zero, the longer it will take to shift.
 * ***Important note about alpha: for non-seizure inducing colors, alpha <= .0005***
 * 
 * The issue that is occurring is that, no matter what I do, no matter if I make 
 * different StandardFade objects and assign them, they will always render at the 
 * same rate, and faster and faster, depending on how many objects are fading.
 *
 * @return new Color based on r, g, and b values calculated.
 * 
 * @author (Only code utilized was lines 58-60):
 * http://introcs.cs.princeton.edu/java/31datatype/Fade.java.html
 */
public StandardFade(Color c1, Color c2, double alpha){
    this.color1 = c1;
    this.color2 = c2;

    this.alpha = alpha;
}

public void tick() {

    if(time <= 1f && firstColor){
        time += alpha;
    }

    else{
        firstColor = false;
    }
    if(time >= 0f && !firstColor)
        time -= alpha;
    else{
        firstColor = true;
    }

    //System.out.println(time);

    short r = (short) (time * color2.getRed()   + (1 - time) * color1.getRed());
    short g = (short) (time * color2.getGreen() + (1 - time) * color1.getGreen());
    short b = (short) (time * color2.getBlue()  + (1 - time) * color1.getBlue());

    if(r > 255) r = 255;
    if(g > 255) g = 255;
    if(b > 255) b = 255;

    if(r < 0) r = 0;
    if(g < 0) g = 0;
    if(b < 0) b = 0;


    this.fadeColor = new Color(r, g, b);
}

public Color getColor(){
    return this.fadeColor;
}
导入java.awt.Color;
导入java.awt.Graphics2D;
公共类标准{
私有静态浮动时间=0;
私有静态布尔值firstColor=true;
私人色彩1;
私人色彩2;
私人双阿尔法;
私人色彩;
/**
*实现
*r、g和b值来自普林斯顿大学;我将撰写
*把它们放在底部。
* 
*方法获取两个参数,并根据
*计时器/时钟值:alpha。
*@param c1要使用的第一种颜色。
*@param c2将使用第二种颜色。
*@param alpha颜色移动的速度。0 255)b=255;
如果(r<0)r=0;
如果(g<0)g=0;
如果(b<0)b=0;
this.fadeColor=新颜色(r、g、b);
}
公共颜色getColor(){
返回这个.fadeColor;
}
唯一的问题是:我添加的渐变对象越多,它们的移动速度就越快

此时使用的是静态变量。此变量由ColorFade类的所有实例共享。因此,每个淡入淡出的对象更新相同的变量

不要使用静态变量(只需去掉static关键字)。每个衰落对象都需要自己的“时间”变量

private float time = 0;
我还质疑firstColor变量是否应该是静态的

唯一的问题是:我添加的渐变对象越多,它们的移动速度就越快

此时使用的是静态变量。此变量由ColorFade类的所有实例共享。因此,每个淡入淡出的对象更新相同的变量

不要使用静态变量(只需去掉static关键字)。每个衰落对象都需要自己的“时间”变量

private float time = 0;

我还质疑firstColor变量是否应该是静态的。

这需要执行大量代码(以后尽量减少您的问题),但根据您对问题的描述-对象越多速度越快-我猜您的问题是这一行:

private static float time = 0;
我假设您知道静态字段在类实例之间共享。它们并不各自存储各自的单独值

这会导致问题,因为每次调用
tick
方法时,每个实例都会增加时间值。当只有一个实例时,这是可以的,但当有更多实例时,这是有问题的

只需删除
static
关键字,它就会正常工作:

private float time = 0;

这需要经历很多代码(以后尽量减少您的问题),但根据您对问题的描述-对象越多,速度越快-我猜您的问题是这一行:

private static float time = 0;
我假设您知道静态字段在类实例之间共享。它们并不各自存储各自的单独值

这会导致问题,因为每次调用
tick
方法时,每个实例都会增加时间值。当只有一个实例时,这是可以的,但当有更多实例时,这是有问题的

只需删除
static
关键字,它就会正常工作:

private float time = 0;

非常感谢。这就解决了问题。firstColor也不应该是静态的,因为它可以防止在出现多个褪色的对象时颜色一直交换。非常感谢,谢谢!这就解决了问题。firstColor也不应该是静态的,因为它可以防止在出现多个褪色的对象时颜色一直交换。非常感谢你。