216 lines
5.7 KiB
Java
216 lines
5.7 KiB
Java
package edu.gatech.cs2335.lemmings.gui;
|
|
|
|
import java.awt.Color;
|
|
import java.awt.Graphics;
|
|
import java.awt.Graphics2D;
|
|
//import java.awt.AlphaComposite;
|
|
|
|
/**
|
|
* Class LFader: This little widget allows one to fade the screen to a
|
|
* certain color in different kinds of cool ways.
|
|
*
|
|
* <PRE>
|
|
* Revision History:
|
|
* v1.0 (Apr. 15, 2004) - Created the LFader class
|
|
* </PRE>
|
|
*
|
|
* @author <A HREF="mailto:gtg308i@mail.gatech.edu">Vladimir Urazov</A>
|
|
* @version Version 1.0, Apr. 15, 2004
|
|
*/
|
|
public class LFader extends LColoredComponent {
|
|
|
|
/**
|
|
* Show debug output?
|
|
*/
|
|
private static final boolean VERBOSE = false;
|
|
|
|
/**
|
|
* Just a little flag so we know when we have started fading.
|
|
*/
|
|
private boolean startedFading;
|
|
|
|
/**
|
|
* The amount of time remaining in the fading.
|
|
*/
|
|
private long timeRemaining;
|
|
|
|
/**
|
|
* Creates a new <code>LFader</code> instance.
|
|
*/
|
|
public LFader() {
|
|
startedFading = false;
|
|
}
|
|
|
|
/**
|
|
* Creates a new thread that will update the fading of the thing
|
|
* specially timed to complete in the specified number of millis.
|
|
*
|
|
* @param time a <code>long</code> value
|
|
* @return a <code>Thread</code> value
|
|
*/
|
|
private Thread createThread(final long time) {
|
|
Thread defThread = new Thread(new Runnable() {
|
|
public void run() { }
|
|
});
|
|
|
|
//First, make sure we have a valid time:
|
|
if (time < 0) {
|
|
return defThread;
|
|
}
|
|
|
|
if ((time * 6) / 100 == 0) {
|
|
//The time period provided is much too short.
|
|
return defThread;
|
|
}
|
|
|
|
Runnable r = new Runnable() {
|
|
public void run() {
|
|
//We would like to have 60 steps per second, so calculate
|
|
//the number of steps.
|
|
long numSteps = (time * 6) / 100;
|
|
|
|
//Calculate the delay between each step:
|
|
long delay = time / numSteps;
|
|
|
|
//Calculate the percent of the fade to do at each step:
|
|
float incrementPercent = 1.0f / numSteps;
|
|
|
|
//A variable to store the progress to the completion of the fade:
|
|
float currentPercentComplete = 0.0f;
|
|
|
|
//The time of the last update:
|
|
long lastTime = System.currentTimeMillis();
|
|
|
|
//The targetted end fade time:
|
|
long finalTime = time + lastTime;
|
|
|
|
if (VERBOSE) {
|
|
System.out.println("LFader: Set up fading... ");
|
|
System.out.println(" Time: " + time);
|
|
System.out.println(" Steps: " + numSteps);
|
|
System.out.println(" Delay: " + delay);
|
|
System.out.println(" Increment: " + incrementPercent);
|
|
System.out.flush();
|
|
}
|
|
|
|
resetFade();
|
|
|
|
startedFading = true;
|
|
|
|
for (int i = 0; i < numSteps; i++) {
|
|
//Update the fading:
|
|
update(currentPercentComplete);
|
|
|
|
//Increment percent to completion:
|
|
currentPercentComplete += incrementPercent;
|
|
|
|
//Sleep some:
|
|
lastTime += delay;
|
|
try {
|
|
Thread.sleep(Math.max(0, lastTime - System.currentTimeMillis()));
|
|
} catch (Exception e) {
|
|
//Sleep interrupted...
|
|
System.out.println("");
|
|
} finally {
|
|
lastTime = System.currentTimeMillis();
|
|
timeRemaining = finalTime - lastTime;
|
|
}
|
|
}
|
|
|
|
// startedFading = false;
|
|
}
|
|
};
|
|
|
|
return new Thread(r);
|
|
}
|
|
|
|
/**
|
|
* Fades to foreground in the time specified.
|
|
*
|
|
* @param time a <code>long</code> value
|
|
*/
|
|
public void fadeToForeground(long time) {
|
|
createThread(time).start();
|
|
}
|
|
|
|
/**
|
|
* Returns true if we are currently fading.
|
|
*
|
|
* @return a <code>boolean</code> value
|
|
*/
|
|
public boolean isFading() {
|
|
return startedFading;
|
|
}
|
|
|
|
/**
|
|
* Describe <code>getTimeRemaining</code> method here.
|
|
*
|
|
* @return a <code>long</code> value
|
|
*/
|
|
public long getTimeRemaining() {
|
|
return timeRemaining;
|
|
}
|
|
|
|
/**
|
|
* Updates the fading.
|
|
* @param progress the percent to completion.
|
|
*/
|
|
protected void update(float progress) {
|
|
if (VERBOSE) {
|
|
System.out.println("Updating - " + (progress * 100) + "%");
|
|
System.out.flush();
|
|
}
|
|
|
|
//Do a simple fade to black for now:
|
|
Color f = getForeground();
|
|
setForeground(new Color(f.getRed() / 255.0f,
|
|
f.getGreen() / 255.0f,
|
|
f.getBlue() / 255.0f,
|
|
progress));
|
|
}
|
|
|
|
/**
|
|
* Resets the fading to the original configuration.
|
|
*/
|
|
protected void resetFade() {
|
|
Color f = getForeground();
|
|
setForeground(new Color(f.getRed() / 255.0f,
|
|
f.getGreen() / 255.0f,
|
|
f.getBlue() / 255.0f,
|
|
0.0f));
|
|
}
|
|
|
|
/**
|
|
* Deep-copies self <b>into the component</b> passed in.
|
|
*
|
|
* @param component a <code>LComponent</code> value
|
|
*/
|
|
protected void copy(LComponent component) {
|
|
}
|
|
|
|
/**
|
|
* Performs all of the necessary drawing for this control only. The
|
|
* children will be taken care of separately. This method need not
|
|
* concern itself with them.
|
|
*
|
|
* @param g a <code>Graphics</code> value
|
|
* @return a <code>boolean</code> value
|
|
*/
|
|
protected boolean paint(Graphics g) {
|
|
if (!startedFading) {
|
|
return true;
|
|
}
|
|
if (VERBOSE) {
|
|
System.out.println("Fading - " + getForeground());
|
|
System.out.flush();
|
|
}
|
|
|
|
Graphics2D g2d = (Graphics2D) g;
|
|
g2d.setColor(getForeground());
|
|
g2d.fill(getBounds());
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|