J'arrive pas a afficher correctement l'horloge dans mon JPanel

emimene Messages postés 31 Date d'inscription samedi 24 octobre 2009 Statut Membre Dernière intervention 22 mars 2012 - 12 janv. 2012 à 20:55
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 - 20 janv. 2012 à 07:56
salut,je veux mettre une horloge dans un JPanel dans ma classe main, mais j'arrive pas a afficher completement l'horloge!! il affiche une tout petit carré
----------------------------------------------------------------------
[b]classe main:
/bJPanel pppp=new JPanel(); JDesktopPane jdp;
.
.
.
pppp.setBounds(new Rectangle(85, 320, 250, 200));
ClockApp cc=new ClockApp();
pppp.add(cc);
jdp.add(pppp)
------------------------
[b]classe ClockApp:
/b
import java.awt.*;
import javax.swing.event.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.lang.Math.*;
import java.util.GregorianCalendar;

public class ClockApp extends JPanel
{
ClockApp()
{
calend=new GregorianCalendar();
h=calend.get(GregorianCalendar.HOUR_OF_DAY);
min=calend.get(GregorianCalendar.MINUTE);
sec=calend.get(GregorianCalendar.SECOND);
System.out.println("jjjjjjjjjjjj");
clockPanel=new ClockPanel();
clockPanel.setSize(400,400);
clockPanel.show();
clockThread=new ClockThread(this);
clockThread.start();

}/* Fin constructeur*/

class ClockPanel extends JPanel
{
public void paintComponent (Graphics g)
{
super.paintComponent(g);

xc=getWidth()/2;
yc=getHeight()/2;
int rayon=Math.min(xc,yc)*80/100;

font= new Font("Times New Roman",0,15);
g.setFont(font);

for(int i=1;i<=12;i++)
{
double angle=i*Math.PI/6.0-Math.PI/2.0;
double x=xc+rayon*Math.cos(angle);
double y=yc+rayon*Math.sin(angle);
g.drawString(" "+i,(int)x,(int)y);
}

/* gestion et affichage de aiguilles*/

double anglesec=(sec*((Math.PI)/30.0)-(Math.PI/2.0));
int xsf=xc+(int)(0.7*rayon*Math.cos(anglesec));
int ysf=yc+(int)(0.7*rayon*Math.sin(anglesec));
g.setColor(Color.red);
g.drawLine(xc,yc,xsf,ysf);

double anglemin=(min*((Math.PI)/30.0)-(Math.PI/2.0));
int xmf=xc+(int)(0.6*rayon*Math.cos(anglemin));
int ymf=yc+(int)(0.6*rayon*Math.sin(anglemin));
g.setColor(Color.yellow);
g.drawLine(xc,yc,xmf,ymf);
double angleheure=(h*((2*Math.PI)/12.0)-(Math.PI/2.0));
int xhf=xc+(int)(0.4*rayon*Math.cos(angleheure));
int yhf=yc+(int)(0.4*rayon*Math.sin(angleheure));
g.setColor(Color.blue);
g.drawLine(xc,yc,xhf,yhf);


}/* fin de paintComponent */

}/* fin de classe ClockPanel */

public void increment()
{
sec=sec+1;
if(sec>60)
{
min=min+1;
sec=1;
if(min>60)
{
min=1;
h=h+1;
if(h>12)
{
h=1;
}
}
}
clockPanel.repaint();
}

private GregorianCalendar calend;
private int h,min,sec,xc,yc;
private ClockPanel clockPanel;
private ClockThread clockThread;
private Font font;

}/* fin de classe ClockApp*/

class ClockThread extends Thread
{
ClockThread(ClockApp horloge)
{
this.horloge=horloge;
}
public void run()
{
while(true)
{
horloge.increment();
try
{
sleep(1000);
}
catch(InterruptedException e)
{
}

}
}
private ClockApp horloge;
}

2 réponses

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
13 janv. 2012 à 07:39
Bonjour,

As tu bien écrit quelque part jdp.setLayout(null); ?
Essayes de mettre un pppp.setLayout(new GridLayout(1, 0));.

Et sinon, essayes aussi d'utiliser des noms de variables plus parlants :)
0
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
20 janv. 2012 à 07:56
Pas de réponse ??
0
Rejoignez-nous