Département R & T - 1er année [email protected] TP 1 : Introduction à la programmation graphique en Java 1er février 2017 1 Programmation d’une montre digitale On voudrais développer une class abstraite Montre qui implante une montre digitale. La classe montre utilise une autre classe abstraite Afficheur qui permet d’afficher l’heure. Cette classe offre deux méthodes abstraites d’affichage de l’heure. La première méthode acceptent en entrée trois entiers qui représentent respectivement, les heures, les minutes et les secondes. La deuxième méthode prends en entrée une chaîne de caractères qui représente l’heure à afficher. Différents types d’afficheurs peuvent être imaginés alors : en mode texte, en mode graphique, etc. Pour réaliser une classe concrète qui implante une montre on doit alors spécifier une classe concrète d’affichage qui hérite de la classe Afficheur et une classe concrète de montre qui hérite de la classe Montre — Définir la classe abstraite Afficheur public abstract class Afficheur { public abstract void afficher ( int h , int m , int s ); public abstract void afficher ( String s ); /* methode pour afficher l ’ heure */ } — Définir la classe abstraite Montre import java . text .*; abstract class Montre extends Thread { private int s ,m , h ; protected Afficheur affiche ; protected boolean am ; private boolean mode24 ; private boolean on ; public Montre (){ this . s =0; this . m =0; this . h =0; } 1 Département R & T - 1er année [email protected] public void setH ( int heure ) throws Exception { if (( heure >=0) && ( heure <=23)){ this . h = heure ; } else { throw new Exception ( " illegale ␣ heure ␣ " ); } } public void setM ( int min ) throws Exception { if (( min >=0) && ( min <=60)){ this . m = min ; } else { throw new Exception ( " illegale ␣ Value ␣ " ); } } public void setS ( int sec ) throws Exception { if (( sec >=0) && ( sec <=60)){ this . s = sec ; } else { throw new Exception ( " illegale ␣ heure ␣ " ); } } public String toString (){ NumberFormat f = new DecimalFormat ( " 00 " ); return ( f . format ( h )+ " : " + f . format ( m )+ " : " + f . format ( s )); } public Afficheur getAfficheur (){ return this . affiche ; } public void run (){ while ( this . on ){ try { Thread . sleep (1000); } catch ( Exception e ){} this . s = this . s +1; if ( this . s ==60){ this . s =0; this . m = this . m +1; if ( this . m ==60){ this . m =0; this . h = this . h +1; 2 Département R & T - 1er année [email protected] if ( this . h ==24){ this . h =0; } } } this . affiche . afficher ( this .h , this .m , this . s ); } } } — Proposer une classe concrète TextAfficheur qui permet d’afficher l’heure en mode texte. import java . io .*; import java . text .*; class TextAfficheur extends Afficheur { public void afficher ( int h , int m , int s ){ NumberFormat f = new DecimalFormat ( " 00 " ); String str = f . format ( h )+ " : " + f . format ( m )+ " : " + f . format ( s ) System . out . println ( str ); } } — Proposer une classe concrète TextMontre qui permet de réaliser une Montre en mode texte. Tester. import java . text .*; class TextMontre extends Montre { public TextMontre (){ super (); this . affiche = new TextAfficheur (); } public static void main ( String [] args ){ TextMontre m = new TextMontre (); m . start (); } } — Proposer une classe concrète GAfficheur qui permet d’afficher l’heure en mode graphique. import java . awt .*; import javax . swing .*; import java . text .*; 3 Département R & T - 1er année [email protected] public class GAfficheur extends Afficheur { private JPanel panel ; private JLabel label ; public GAfficheur (){ this . panel = new JPanel (); this . label = new JLabel (); this . panel . add ( this . label ); } public JPanel getPanel (){ return this . panel ; } public void afficher ( int h , int m , int s ){ NumberFormat f = new DecimalFormat ( " 00 " ); String str = f . format ( h )+ " : " + f . format ( m )+ " : " + f . format ( s ) this . label . setText ( str ); } public void afficher ( String str ){ this . label . setText ( str ); } } — Proposer une classe concrète GMontre qui réalise une montre en mode graphique. Tester import java . awt .*; import javax . swing .*; class GMontre extends Montre { public GMontre (){ super (); this . affiche = new GAfficheur (); } public static void main ( String [] args ){ JFrame f = new JFrame ( " GMontre " ); GMontre m = new GMontre (); GAfficheur a = ( GAfficheur ) m . getAfficheur (); f . setContentPane ( a . getPanel ()); f . setVisible ( true ); m . start (); } } 4 Département R & T - 1er année [email protected] — Modifier la montre graphique pour ajouter un buttons qui permet de sélectionner le mode d’affichage : mode 12 ou 24. 5