Advertisement
Guest User

Untitled

a guest
Jun 5th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 8.96 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6.  
  7. package disque;
  8.  
  9. import java.awt.Color;
  10. import java.awt.Dimension;
  11. import java.awt.Graphics;
  12. import java.awt.Graphics2D;
  13. import java.awt.RenderingHints;
  14. import javax.swing.JPanel;
  15.  
  16. /**
  17. *
  18. * @author laurillau
  19. */
  20. public class DisqueHoraire extends JPanel {
  21.    
  22.     private final String [] MOIS =  { "Janvier", "Février", "Mars", "Avril",
  23.                                       "Mai", "Juin", "Juillet", "Août",
  24.                                       "Septembre", "Octobre", "Novembre",
  25.                                       "Décembre" };
  26.    
  27.     private final int [] NB_JOURS = { 31, 28, 31, 30, 31, 30,
  28.                                       31, 31, 30, 31, 30, 31 };
  29.  
  30.     private static final int RAYON_JOUR    = 50;
  31.     private static final int RAYON_MOIS    = RAYON_JOUR + 100;
  32.     private static final int RAYON_HEURES  = RAYON_MOIS + 100;
  33.     private static final int RAYON_CONTOUR = RAYON_HEURES + 80;
  34.    
  35.     private static final int CENTRE_X      = RAYON_CONTOUR + 20;
  36.     private static final int CENTRE_Y      = RAYON_CONTOUR + 20;
  37.    
  38.     private boolean bissextile = false;
  39.     private int mois = 4, jour = 4, heure = 4, minutes = 4;
  40.     private Circle c1;
  41.     private Circle c2;
  42.     private Circle c3;
  43.     private Circle c4;
  44.     public DisqueHoraire() {
  45.         setBackground(Color.white);
  46.         setDoubleBuffered(true);
  47.         setPreferredSize(new Dimension(2 * CENTRE_X, 2 * CENTRE_Y));
  48.        
  49.         // Créer 4 objets Circle chargés de dessiner les cercles concentriques
  50.        
  51.         // A FAIRE
  52.         c1 = new Circle(CENTRE_X,CENTRE_Y,RAYON_CONTOUR,Color.yellow);
  53.         c2 = new Circle(CENTRE_X,CENTRE_Y,RAYON_HEURES,Color.green);
  54.         c3 = new Circle(CENTRE_X,CENTRE_Y,RAYON_MOIS,Color.blue);
  55.         c4 = new Circle(CENTRE_X,CENTRE_Y,RAYON_JOUR,Color.red);
  56.        
  57.     }
  58.    
  59.     public void paintComponent(Graphics g) {
  60.         ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  61.                                          RenderingHints.VALUE_ANTIALIAS_ON);
  62.  
  63.         // Dessiner le curseur
  64.         afficherCurseur(g);
  65.         c1.draw(g);
  66.         c2.draw(g);
  67.         c3.draw(g);
  68.         c4.draw(g);
  69.              
  70.        
  71.        
  72.         // Dessiner les sections pour les mois            
  73.         afficherMois(g);            
  74.  
  75.         // Dessiner les sections pour les jours
  76.         afficherJours(g);
  77.                
  78.         // Dessiner les sections pour les heures
  79.         afficherHeures(g);
  80.     }
  81.    
  82.     private void afficherMois(Graphics g) {
  83.         TextLabel label;
  84.         int x1, y1, x2, y2;
  85.        
  86.         for(int i = 0; i < 12; i++) {
  87.            
  88.             // Calcul de l'angle en degré d'abord pour simplifier (mais non nécessaire)
  89.             //
  90.             // La méthode toRadians permet de convertir la valeur en degré
  91.             // vers une valeur en radian, ce qui est nécessaire pour appliquer
  92.             // les fonctions cosinus et sinus
  93.             double angle = Math.toRadians((i * 360.0) / 12 + 360/24);
  94.            
  95.             // Dessiner les sections
  96.             g.setColor(Color.BLACK);
  97.             x1=(int)(CENTRE_X+RAYON_MOIS*Math.cos(angle));
  98.             y1=(int)(CENTRE_Y+RAYON_MOIS*Math.sin(angle));
  99.             x2=(int)(CENTRE_X+RAYON_HEURES*Math.cos(angle));
  100.             y2=(int)(CENTRE_Y+RAYON_HEURES*Math.sin(angle));
  101.             // A FAIRE
  102.             g.drawLine(x1,y1,x2,y2);
  103.             // Afficher les mois
  104.             g.setColor(i == 0 ? Color.RED : Color.BLACK);
  105.            
  106.             // A FAIRe
  107.             int m=(mois+i)/12;
  108.             label = new TextLabel(MOIS[(mois+i)%12],x1,y1,angle);
  109.             label.draw(g,m ,m);
  110.         }            
  111.     }
  112.    
  113.     private void afficherJours(Graphics g) {    
  114.         // A FAIRE
  115.         TextLabel label;
  116.         int x1, y1, x2, y2;
  117.        
  118.         for(int i = 0; i < (mois==1&& bissextile ?NB_JOURS[(mois)]+1 : NB_JOURS[(mois)]); i++) {
  119.             // Calcul de l'angle en degré d'abord pour simplifier (mais non nécessaire)
  120.             //
  121.             // La méthode toRadians permet de convertir la valeur en degré
  122.             // vers une valeur en radian, ce qui est nécessaire pour appliquer
  123.             // les fonctions cosinus et sinus
  124.             double angle = Math.toRadians((i * 360.0) / (mois==1&& bissextile ?NB_JOURS[(mois)]+1 : NB_JOURS[(mois)]) + 360/60);
  125.            
  126.             // Dessiner les sections
  127.             g.setColor(Color.BLACK);
  128.             x1=(int)(CENTRE_X+RAYON_JOUR*Math.cos(angle));
  129.             y1=(int)(CENTRE_Y+RAYON_JOUR*Math.sin(angle));
  130.             x2=(int)(CENTRE_X+RAYON_MOIS*Math.cos(angle));
  131.             y2=(int)(CENTRE_Y+RAYON_MOIS*Math.sin(angle));
  132.             // A FAIRE
  133.             g.drawLine(x1,y1,x2,y2);
  134.             // Afficher les mois
  135.             g.setColor(i == 0 ? Color.RED : Color.BLACK);
  136.            
  137.             // A FAIRe
  138.             int m=(jour+i)/(mois==1&& bissextile ?NB_JOURS[(mois)]+1 : NB_JOURS[(mois)]);
  139.             label = new TextLabel("       "+Integer.toString((jour+i)<=(mois==1&& bissextile ?NB_JOURS[(mois)]+1 : NB_JOURS[(mois)]) ?(jour+i):(jour+i)%((mois==1&& bissextile ?NB_JOURS[(mois)]+1 : NB_JOURS[(mois)]))),x1,y1,angle);
  140.             label.draw(g,m ,m);
  141.         }
  142.     }
  143.    
  144.     private void afficherHeures(Graphics g) {        
  145.         TextLabel label;
  146.         int x1, y1, x2, y2;
  147.         String j;
  148.         String h =String.valueOf(heure);
  149.         for(int i = 0; i < 48; i++) {
  150.             // Calcul de l'angle en degré d'abord pour simplifier (mais non nécessaire)
  151.             //
  152.             // La méthode toRadians permet de convertir la valeur en degré
  153.             // vers une valeur en radian, ce qui est nécessaire pour appliquer
  154.             // les fonctions cosinus et sinus
  155.             double angle = Math.toRadians((i * 360.0) / 48 + 360/96);
  156.            
  157.             // Dessiner les sections
  158.             g.setColor(Color.BLACK);
  159.             x1=(int)(CENTRE_X+RAYON_HEURES*Math.cos(angle));
  160.             y1=(int)(CENTRE_Y+RAYON_HEURES*Math.sin(angle));
  161.             x2=(int)(CENTRE_X+RAYON_CONTOUR*Math.cos(angle));
  162.             y2=(int)(CENTRE_Y+RAYON_CONTOUR*Math.sin(angle));
  163.             // A FAIRE
  164.             g.drawLine(x1,y1,x2,y2);
  165.             // Afficher les mois
  166.             g.setColor(i == 0 ? Color.RED : Color.BLACK);
  167.            
  168.             // A FAIRe
  169.            
  170.          
  171.             int m=(heure+i)/48;
  172.  
  173.             if(minutes<30){
  174.              j=(i%2==1 ? "30" : "00");  
  175.             }
  176.             else{
  177.              j=(i%2==1 ? "00" : "30");  
  178.             }
  179.            
  180.             label = new TextLabel(h+'h'+j,x1,y1,angle);
  181.             label.draw(g,m ,m);
  182.             if(minutes<30){
  183.              h=(String.valueOf((heure+(i+1)/2)%24));  
  184.             }
  185.             else{
  186.               h=(String.valueOf((heure+1+i/2)%24));
  187.             }
  188.         }    
  189.     }
  190.    
  191.     private void afficherCurseur(Graphics g) {
  192.        
  193.         // Section pour les heures
  194.         g.setColor(new Color(192,192,192,127));
  195.         g.fillArc(20, 20,
  196.                   2 * RAYON_CONTOUR,
  197.                   2 * RAYON_CONTOUR,
  198.                   (int) (-360/48.0 - 3.75), (3 * 360) / 48);
  199.        
  200.         g.setColor(Color.WHITE);
  201.         g.fillOval(20 + (RAYON_CONTOUR - RAYON_HEURES) ,
  202.                    20 + (RAYON_CONTOUR - RAYON_HEURES),
  203.                    2 * RAYON_HEURES,
  204.                    2 * RAYON_HEURES);
  205.        
  206.         // Section pour le mois
  207.         g.setColor(new Color(192,192,192,127));
  208.         g.fillArc(20 + (RAYON_CONTOUR - RAYON_HEURES),
  209.                   20 + (RAYON_CONTOUR - RAYON_HEURES),
  210.                   2 * RAYON_HEURES,
  211.                   2 * RAYON_HEURES, -15, 360/12);
  212.        
  213.         g.setColor(Color.WHITE);
  214.         g.fillOval(20 + (RAYON_CONTOUR - RAYON_MOIS) ,
  215.                    20 + (RAYON_CONTOUR - RAYON_MOIS),
  216.                    2 * RAYON_MOIS,
  217.                    2 * RAYON_MOIS);
  218.  
  219.         // Section pour le jour
  220.         int nbJours = NB_JOURS[mois] + ((mois == 1) && bissextile ? 1 : 0);
  221.         g.setColor(new Color(192,192,192,127));
  222.         g.fillArc(20 + (RAYON_CONTOUR - RAYON_MOIS),
  223.                   20 + (RAYON_CONTOUR - RAYON_MOIS),
  224.                   2 * RAYON_MOIS,
  225.                   2 * RAYON_MOIS, -180/nbJours, 360/nbJours);
  226.        
  227.         g.setColor(Color.WHITE);
  228.         g.fillOval(20 + (RAYON_CONTOUR - RAYON_JOUR) ,
  229.                    20 + (RAYON_CONTOUR - RAYON_JOUR),
  230.                    2 * RAYON_JOUR,
  231.                    2 * RAYON_JOUR);            
  232.     }
  233.    
  234.     public void setDate(int mois, int jour, int heure, int minutes, boolean bis) {
  235.         this.mois = mois;
  236.         this.jour = jour;
  237.         this.heure = heure;
  238.         this.minutes = minutes;
  239.         this.bissextile = bis;        
  240.     }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement