Hello everyone !
I am writing a little code in which the user sets up fuses for a firework on a Frame with different buttons on it, and then when he presses launch the animation a new window pops up and the firework is drawn on it, with a method called peindre from which I know it works already (I’ve tested it carefully).
When I compile the class used for this frame, a window with the wanted buttons appear and everything goes well until I press launch the animation, in which case only a black window appears. The first thing peindre does (the code is underneath) is paint the whole window black, and then it launches Threads for each fuse that wait a certain time and then draw the fuses and erase them, this worked when I tested it directly. But here I can’t see any of the fuses appear ! I can when I create one myself in the main though, so it’s really a problem of my program not getting the information the user types in right.
//ATTRIBUTES
public TextField duree, tpsExplosion, altitude, avancement, puissEtincelles ;
public double dureed, tpsExplosiond, xf, yf, puissEtincellesd ;
public boolean bicoloreb ;
public Choice choixForme, choixCouleur ;
public String forme ;
public Color couleur ;
public Button plus ;
public Button animation ;
public Checkbox bicolore ;
public FenetreAnimeeSansCanvas fenetreAnimee ;
public FenetreNouvelleFusee fenetreNouvelleFusee ;
int numero = 0 ;
public String choixFormes, choixCouleurs;
static final long serialVersionUID=1L;
//CONSTRUCTOR
public AnimationSansCanvas() {
super("AnimationSansCanvas") ;
altitude = new TextField("Altitude de l'explosion") ;
avancement = new TextField("Position de l'explosion sur l'axe des abscisses");
duree = new TextField("Durée de l'affichage de la fusée") ;
tpsExplosion = new TextField("Moment de l'explosion (par rapport au début de l'animation)");
choixForme = new Choice();
choixForme.add("Choix de la forme :");
choixForme.add("Étoile");
choixForme.add("Croix");
choixForme.add("Point");
choixCouleur = new Choice();
choixCouleur.add("Choix de la couleur :");
choixCouleur.add("Rouge");
choixCouleur.add("Vert");
choixCouleur.add("Bleu");
choixCouleur.add("Rose");
choixCouleur.add("Jaune");
choixCouleur.add("Orange");
puissEtincelles = new TextField("Puissance de 8 du nombre d'étincelles");
plus = new Button("Ajouter une fusée") ;
animation = new Button ("Lancer l'animation") ;
bicolore = new Checkbox("Fusée bicolore (la deuxième moitié sera rouge)", false) ;
setLayout(new FlowLayout());
this.add(altitude) ;
this.add(avancement) ;
this.add(duree);
this.add(tpsExplosion);
this.add(choixForme);
this.add(choixCouleur);
this.add(puissEtincelles);
this.add(plus);
this.add(animation);
this.add(bicolore);
altitude.addActionListener(this);
avancement.addActionListener(this);
duree.addActionListener(this);
tpsExplosion.addActionListener(this);
puissEtincelles.addActionListener(this);
plus.addActionListener(this);
animation.addActionListener(this);
choixCouleur.addItemListener(this);
choixForme.addItemListener(this);
bicolore.addItemListener(this);
addWindowListener(new EcouteurPourFermetureFenetre()); //pour tout arrêter en cas de fermeture de fenêtre
setResizable(true);
fenetreAnimee = new FenetreAnimeeSansCanvas() ;
fenetreAnimee.setVisible(true);
}
//METHODES
public void actionPerformed(ActionEvent evt){
if (evt.getSource()==choixForme){
forme = choixForme.getSelectedItem();
}
if (evt.getSource()==bicolore){
bicoloreb = true;
}
if (evt.getSource()==altitude){
yf = Double.valueOf(altitude.getText());
}
if (evt.getSource() == avancement){
xf = Double.valueOf(avancement.getText());
}
if (evt.getSource() == duree){
dureed = Double.valueOf(duree.getText());
}
if (evt.getSource() == tpsExplosion){
tpsExplosiond = Double.valueOf(tpsExplosion.getText());
}
if (evt.getSource() == puissEtincelles){
puissEtincellesd = Double.valueOf(puissEtincelles.getText());
}
if (evt.getSource() == plus){
numero += 1 ;
fenetreNouvelleFusee = new FenetreNouvelleFusee() ;
fenetreNouvelleFusee.numero = numero ;
fenetreNouvelleFusee.setVisible(true);
fenetreAnimee.fusees.add(fenetreNouvelleFusee.fus) ;
}
if (evt.getSource() == animation){
numero += 1 ;
Fusee fuse = new Fusee(numero) ;
fuse.couleur = couleur ;
fuse.puissEtincelles = (int)puissEtincellesd ;
fuse.xf = xf ;
fuse.yf = yf ;
fuse.forme = forme ;
fuse.duree = dureed ;
fuse.tpsDepart = tpsExplosiond ;
fuse.puissEtincelles = (int)puissEtincellesd ;
fenetreAnimee.fusees.add(fuse) ; //adds the last fuse to the LinkedList of the animation window
fenetreAnimee.setVisible(true); //the window is created so getGraphics should return something ?!
fenetreAnimee.peindre(fenetreAnimee.getGraphics()); //the window is painted (black background + fuses)
}
}
public void itemStateChanged(ItemEvent ivt){
if (ivt.getSource()==choixCouleur){
if (choixCouleur.getSelectedItem()=="Rose"){
couleur = Color.pink ;
}
if (choixCouleur.getSelectedItem()=="Bleu"){
couleur = Color.blue ;
}
if (choixCouleur.getSelectedItem()=="Vert"){
couleur = Color.green ;
}
if (choixCouleur.getSelectedItem()=="Rouge"){
couleur = Color.red ;
}
if (choixCouleur.getSelectedItem()=="Jaune") {
couleur = Color.yellow ;
}
if (choixCouleur.getSelectedItem()=="Orange") {
couleur = Color.orange ;
}
}
}
//MAIN
public static void main(String [] abs){
AnimationSansCanvas anim = new AnimationSansCanvas();
anim.setLocation(100, 100);
anim.setSize(600, 450);
anim.setVisible(true);
}
}
I must be doing something wrong in getting the information the user gives but I can’t see my mistake ! When I add a fuse in the main myself it draws it fine, it just doesn’t seem to get the info that I type into the frame. It would be great if someone could tell me what’s wrong with my program
And here’s the code for peindre ! (the threads just count time passing, then draw the fuses and then erases them again when their time is over)
public void peindre(Graphics gr){
gr.setColor(Color.black);
gr.fillRect(0, 0, w, h);
for (Fusee fus : fusees) {
fus.g = gr ;
fus.anim.start();
}
}