import java.awt.Color; @SuppressWarnings("serial") class PanneauChiffres extends JPanel implements ActionListener { Image[] chiffre = new Image[10]; int largeur = 200, hauteur = 150; int largeurChiffre; int hauteurChiffre; int x1, x2, x3, x4; int y; Color fond = Color.BLACK; MediaTracker tracker; int dep = 0; JButton pause = new JButton("pause"); JButton reprendre = new JButton("reprendre"); Timer timer; int heure; PanneauChiffres(JApplet applet) { setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); setPreferredSize(new Dimension(largeur, hauteur)); add(pause); add(reprendre); pause.addActionListener(this); reprendre.addActionListener(this); tracker = new MediaTracker(this); for (int i = 0; i < 10; i++) { chiffre[i] = applet.getImage(applet.getCodeBase(), "chiffre"+ i +".gif"); tracker.addImage(chiffre[i],0); } try { tracker.waitForID(0); } catch(InterruptedException exc) {} largeurChiffre = chiffre[0].getWidth(this); hauteurChiffre = chiffre[0].getHeight(this); x1 =(largeur - 3 * largeurChiffre) / 2; x2 = x1 + largeurChiffre; x3 = x2 + largeurChiffre; y = 2*(hauteur-hauteurChiffre)/3; setForeground(Color.white); setBackground(fond); timer = new Timer(1000, this); timer.start(); } public void actionPerformed(ActionEvent evt) { Object source = evt.getSource(); if (source == timer) { heure = heure + 1; repaint(); } else if (source == pause) timer.stop(); else if (source == reprendre) timer.restart(); } public void paintComponent(Graphics g) { int a, b, c; int copieHeure = heure; super.paintComponent(g); c = copieHeure % 10; copieHeure /= 10; b = copieHeure % 10; copieHeure /= 10; a = copieHeure % 10; g.drawImage(chiffre[c], x3, y, this); if (heure > 9) g.drawImage(chiffre[b], x2, y, this); if (heure > 99) g.drawImage(chiffre[a], x1, y, this); } } @SuppressWarnings("serial") public class ChiffresA extends JApplet { public void init() { try { setLayout(new FlowLayout(FlowLayout.CENTER)); add(new PanneauChiffres(ChiffresA.this)); } catch(Exception exc) { showStatus(exc.toString()); } } }