Horloge analogique

ccjb5532 Messages postés 35 Date d'inscription   Statut Membre Dernière intervention   -  
ccjb5532 Messages postés 35 Date d'inscription   Statut Membre Dernière intervention   -

bonjour , alors voila j'ai un probleme pour crée une horloge analogique dans une fenetre graphique et qui permet d'afficher l'heure apres que j'ai rempli de champs (heure et minutes ) les aiguilles doivent etre dirigées vers l'heure indiquée
merci
ps : je suis vraiment debutant java
A voir également:

1 réponse

Utilisateur anonyme
 
Cela fonctionne pour moi mais utilise des outils tels que les Thread.

public class AnalogTimer extends JFrame {
    public int hour;
    public int min;
    public int sec;
    ClockDial cd;

    public AnalogTimer() {
        setSize(510, 530);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        cd = new ClockDial(this);
        getContentPane().add(cd);
        Date curr = new Date();
        String time = curr.toString();
        hour = Integer.parseInt(time.substring(11, 13));
        min = Integer.parseInt(time.substring(14, 16));
        sec = Integer.parseInt(time.substring(17, 19));
        ClockEngine.setPriority(ClockEngine.getPriority() + 3);
        ClockEngine.start();
    }

    public static void main(String args[]) {
        new AnalogTimer().setVisible(true);
    }

    Thread ClockEngine = new Thread() {
        int newsec, newmin;

        public void run() {
            while (true) {
                newsec = (sec + 1) % 60;
                newmin = (min + (sec + 1) / 60) % 60;
                hour = (hour + (min + (sec + 1) / 60) / 60) % 12;
                sec = newsec;
                min = newmin;

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                }

                cd.repaint();
            }
        }
    };
}


class ClockDial extends JPanel {

    AnalogTimer parent;

    public ClockDial(AnalogTimer pt) {
        setSize(520, 530);
        parent = pt;
    }

    @Override
    public void paintComponent(Graphics g) {
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.WHITE);
        g.fillOval(5, 5, 480, 480);
        g.setColor(Color.BLACK);
        g.fillOval(10, 10, 470, 470);
        g.setColor(Color.WHITE);
        g.fillOval(237, 237, 15, 15);
        g.setFont(g.getFont().deriveFont(Font.BOLD, 32));

        for (int i = 1; i <= 12; i++)
            g.drawString(Integer.toString(i), 240 - (i / 12) * 11 + (int) (210 * Math.sin(i * Math.PI / 6)), 253 - (int) (210 * Math.cos(i * Math.PI / 6)));

        double minsecdeg = (double) Math.PI / 30;
        double hrdeg = (double) Math.PI / 6;
        int tx, ty;
        int xpoints[] = new int[3];
        int ypoints[] = new int[3];

        //secondes
        tx = 245 + (int) (210 * Math.sin(parent.sec * minsecdeg));
        ty = 245 - (int) (210 * Math.cos(parent.sec * minsecdeg));
        g.drawLine(245, 245, tx, ty);

        //minutes
        tx = 245 + (int) (190 * Math.sin(parent.min * minsecdeg));
        ty = 245 - (int) (190 * Math.cos(parent.min * minsecdeg));
        xpoints[0] = 245;
        xpoints[1] = tx + 2;
        xpoints[2] = tx - 2;
        ypoints[0] = 245;
        ypoints[1] = ty + 2;
        ypoints[2] = ty - 2;
        g.fillPolygon(xpoints, ypoints, 3);

        //heures
        tx = 245 + (int) (160 * Math.sin(parent.hour * hrdeg + parent.min * Math.PI / 360));
        ty = 245 - (int) (160 * Math.cos(parent.hour * hrdeg + parent.min * Math.PI / 360));
        xpoints[1] = tx + 4;
        xpoints[2] = tx - 4;
        ypoints[1] = ty - 4;
        ypoints[2] = ty + 4;
        g.fillPolygon(xpoints, ypoints, 3);
    }
}
0
Utilisateur anonyme
 
Bonjour,
il est possible que la question soit un exercice, il y a une politique à respecter concernant les devoirs:
https://www.commentcamarche.net/infos/25899-demander-de-l-aide-pour-vos-exercices-sur-ccm/
et
http://codes-sources.commentcamarche.net/forum/affich-1557761-bar-sujet-de-pfe-tp-et-autres-devoirs-scolaires#top

Là tu lui jettes en pâture un code avec des Thread, alors qu'il a bien précisé être vraiment débutant. S'il s'agit bien d'un devoir, son prof saura tout de suite que ça n'est pas de lui.
Et s'il ne s'agit pas d'un exercice, un tel code sans contexte ni commentaires sera difficilement compréhensible.

Je ne cherche pas à te démotiver à aider, mais à t'aider à mieux aider
0
ccjb5532 Messages postés 35 Date d'inscription   Statut Membre Dernière intervention   2
 
merci , j'ai esseyé mais il y a tjr des erreurs
0