All pastes #476623 Raw Edit

WAITINGBAR.JAVAAAA

public java v1 · immutable
·published 2007-05-08 07:06 UTC
rendered paste body
import javax.swing.*;import java.awt.*;public class WaitingBar extends JComponent implements Runnable{	//pour tester	public static void main(String[] args){		JFrame form = new JFrame();		form.getContentPane().add(new WaitingBar(				300,				300,				Color.LIGHT_GRAY,				Color.DARK_GRAY,				9));		form.setSize(300,300);		form.setVisible(true);	}		private static final long serialVersionUID = 1L;	private boolean isRunning, isDead;	private int cptr, nbBarres, fps;	private Color[] tabCouleurs;	public WaitingBar()	{		this(120, 32, Color.WHITE, Color.BLACK, 10);	}	public WaitingBar(int width, int height, Color couleurDebut, Color couleurFin, int nbBarres)	{		super();		this.nbBarres = nbBarres;		this.cptr = 0;		this.isRunning = false;		this.isDead = false;		this.tabCouleurs = new Color[19];		this.fps = 24;		this.setCouleurs(couleurDebut, couleurFin);		this.setSize(width, height);		this.setOpaque(true);					new Thread(this).start();		this.startAnim();	}	/**	 * Définit les 2 couleurs entre lesquelles l'animation oscille.	 * 	 * @param c1 Couleur de départ pour l'animation.	 * @param c2 Couleur de fin pour l'animation.	 */	public void setCouleurs(Color c1, Color c2){		double diffR, diffG, diffB;		diffR = ((double)c1.getRed() - (double)c2.getRed()) / 10;		diffG = ((double)c1.getGreen() - (double)c2.getGreen()) / 10;		diffB = ((double)c1.getBlue() - (double)c2.getBlue()) / 10;		tabCouleurs[0] = tabCouleurs[18] = c1;		for(int i = 1; i <= 10; i++){			tabCouleurs[i] = new Color(					(int)(c1.getRed() - diffR * i),					(int)(c1.getGreen() - diffG * i),					(int)(c1.getBlue() - diffB * i));			tabCouleurs[18 - i] = tabCouleurs[i];		}	}	/**	 * Démarre l'animation de la WaitingBar.	 */	public void startAnim(){		isRunning = true;	}	/**	 * Arrête l'animation de la WaitingBar.	 */	public void stopAnim(){		isRunning = false;	}		public void killAnim(){		isDead = true;	}	/**	 * @return Retourne TRUE si l'animation de la WaitingBar est en train de jouer	 * ou FALSE si elle est stoppée.	 */	public boolean isRunning(){		return isRunning;	}	/**	 * JComponent.paint(Graphics g)	 */	public void paint(Graphics og){		//TODO: Double-buffering...		Graphics2D g = (Graphics2D) og;		g.setColor(Color.BLACK);		g.fillRect(0,0,getWidth(),getHeight());				//delta en pourcentage		double delta = 0.03;		double deltaY = getHeight() * delta;		double deltaX = getWidth() * delta;		double rectWidth = getWidth() * (1 - (nbBarres + 1) * delta) / nbBarres;		double rectHeight = getHeight() * (1 - 2 * delta);		for(int i = 0; i < nbBarres; i++){			g.setColor(tabCouleurs[ (int)(cptr + i * 2) % tabCouleurs.length ]);			g.fillRect(					(int)(deltaX + (i * (deltaX + rectWidth))),							(int)deltaY,							(int)rectWidth,							(int)rectHeight					);		}		//g.setColor(tabCouleurs[cptr % 19]);		//g.fillRect(0,0,getWidth(),getHeight());		}	/**	 * Runnable.run()	 */	public void run(){		while(true && !isDead){			if(isRunning()){				repaint();				cptr++;			}			try {				Thread.sleep(1000 / fps);			} catch (InterruptedException e) {}		}	}}