All pastes #291015 Raw Edit

Clock2

public java v1 · immutable
#291015 ·published 2006-12-24 17:28 UTC
rendered paste body
/* * MyClock.java * * Created on 19 décembre 2006, 08:37 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */package ClockAndCalendar;import java.awt.Color;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.RenderingHints;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.text.SimpleDateFormat;import java.util.Date;import javax.swing.JComponent;/** * * @author masy2 */public class Clock2 extends JComponent implements Runnable, MouseListener, ClockListener {        private SimpleDateFormat hour;    private volatile Thread t = null;    private ClockListener delegate;    private boolean isStopped, wasSet=false;     private String sHour=null;    private long time = -1;        /** Creates a new instance of MyClock */    public Clock2() {        hour = new SimpleDateFormat("HH:mm:ss");        addMouseListener(this);        isStopped = false;    }         public void start()    {        t = new Thread(this);        if(time == -1) time = System.currentTimeMillis();        if(!wasSet) time = time + (System.currentTimeMillis() - time);        else        {            System.out.println("L'heure a été changé");            wasSet=false;        }        t.start();            }        public void setTime(long newtime)    {        System.out.println("plop");        this.stop();        time = newtime;        wasSet=true;        this.start();    }        public void resetTime()    {        this.stop();        time = -1;        this.start();    }        public void stop()    {           t=null;    }       public void paint(Graphics g)    {        Graphics2D g2d = (Graphics2D)g.create();        RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);        g2d.setRenderingHints(qualityHints);                if(!isStopped)        {            Date date = new Date(this.time);            sHour = new String("" + hour.format(date));        }                   Font fnt = new Font("Serif", Font.BOLD, 22);        FontMetrics fm = getFontMetrics(fnt);                setBackground(Color.BLACK);        g2d.setColor(Color.GRAY);        g2d.setFont(fnt);                int y = (getHeight()-fm.getHeight())/2 + fm.getAscent();        g2d.drawString(sHour,(int)((getWidth()-fm.stringWidth(sHour))/2),y);    }    public void run(){       try        {           while(t == Thread.currentThread())            {                t.sleep(1000);                time += 1000;                repaint();            }       }        catch(InterruptedException e)        {            e.printStackTrace();        }             }        public void setDelegate(ClockListener l)    {        this.delegate = l;    }    public void mouseClicked(MouseEvent mouseEvent) {        if(isStopped)        {            if(delegate != null)            {                delegate.clockStarted(this);            }        }        else        {            if(delegate != null)            {                delegate.clockStopped(this);            }        }    }    public void mousePressed(MouseEvent mouseEvent) {    }    public void mouseReleased(MouseEvent mouseEvent) {    }    public void mouseEntered(MouseEvent mouseEvent) {    }    public void mouseExited(MouseEvent mouseEvent) {    }    public void clockStarted(Clock2 clk) {        this.isStopped = false;        this.start();    }    public void clockStopped(Clock2 clk) {        this.isStopped = true;        this.stop();    }    }