Chronometer

Contenu du snippet

Un chronomètre de plus avec spécification de l'intervalle de déclenchement en millisecondes.

Source / Exemple :


package com.infodavid.util;
/*

  • Copyright (c) 2001 David Rolland
*
  • Redistribution and use in source and binary forms, with or without
  • modification, are permitted provided that the following conditions are met:
*
  • -Redistributions of source code must retain the above copyright notice, this
  • list of conditions and the following disclaimer.
*
  • -Redistribution in binary form must reproduct the above copyright notice,
  • this list of conditions and the following disclaimer in the documentation
  • and/or other materials provided with the distribution.
*
  • This software is provided "AS IS," without a warranty of any kind. ALL
  • EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  • IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  • NON-INFRINGEMENT, ARE HEREBY EXCLUDED. D.Rolland SHALL NOT BE
  • LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  • OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL D.Rolland
  • BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  • INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  • CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  • OR INABILITY TO USE SOFTWARE, EVEN IF D.Rolland HAS BEEN ADVISED OF THE
  • POSSIBILITY OF SUCH DAMAGES.
*
  • You acknowledge that Software is not designed,licensed or intended for use in
  • the design, construction, operation or maintenance of any nuclear facility.
  • /
import java.awt.event.ActionEvent; import java.io.Serializable; /**
  • Title:
  • Description:
  • Copyright: Copyright (c) 2001
  • Company: David Rolland, infodavid@chez.com
  • @author David Rolland, infodavid@chez.com
  • @version 1
  • /
public class Chronometer extends ListenedObjectImpl implements Runnable, Serializable { private Thread chronometre; private float step = 0; private long minutes = 0; private int delay = 100; public void reset() { step = 0; minutes = 0; } public void setDelay(int ms) { delay = ms; } public int getDelay() { return delay; } public void start() { reset(); chronometre = new Thread (this); chronometre.start(); } public void cont() { if (chronometre != null) { chronometre.start(); } else { start(); } } public int[] getTime() { int time[] = new int[4]; //heure time[0] = (int)(minutes / 60); //minute time[1] = (int)(minutes % 60); //seconde time[2] = (int)(((step * delay) / 1000) % 60); //dixieme time[3] = (int)((step * delay) % 10); return time; } protected boolean isAlive() { if (chronometre != null) { return chronometre.isAlive(); } else { return false; } } protected void increment() { step++; final float minute = 60000 / (float)delay; if (step >= minute) { short i = (short)(step / minute); minutes += i; step -= i * minute; } raiseEvent(new ActionEvent(this, 0 , getClass().getName(), minutes, 0)); } public void run() { try { while (isAlive()) { Thread.sleep(getDelay()); increment(); } } catch (InterruptedException e) { } } public void stop() { if (isAlive()) { chronometre.interrupt(); } } }

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.

Du même auteur (cs_trap)