import javax.swing.*; import java.awt.*; public class WaitingBar extends JComponent implements Runnable { private static final long serialVersionUID = 1L; private boolean isRunning; private int cptr; private Color[] tabCouleurs; public WaitingBar() { this(120, 32); } public WaitingBar(int width, int height) { super(); this.cptr = 0; this.isRunning = false; this.tabCouleurs = new Color[19]; this.setCouleurs(Color.WHITE, Color.BLACK); this.setSize(width, height); 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] = 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[19 - 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; } /** * @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; //TODO: Afficher 5 carrées... /* * nbCarres = 5; * * double deltaY = getHeight() * 0.02; * double deltaX = getWidth() * 0.03; * double rectWidth = getWidth() * 0.82 / 5; * double rectHeight = getHeight() * 0.96; * * for(int i = 0; i < 5; i++){ * g.setColor(tabCouleur[ (cptr + i * 2) % tabCouleurs.length ]); * g.fillRect( * x = deltaX + (i * (deltaX + rectWidth), * y = deltaY, * width = rectWidth, * height = rectHeight * ); * } * */ g.setColor(tabCouleurs[cptr % 19]); g.fillRect(0,0,getWidth(),getHeight()); } /** * Runnable.run() */ public void run(){ while(true){ if(isRunning()){ repaint(); cptr++; } try { Thread.sleep(1000 / 12); } catch (InterruptedException e) {} } } }