Graphics
Le
theBrave
Bonjour,
En vue de réaliser une animation gaphique, j'aimerais qu'un thread
dessine directement sur un autre composant, un JPanel par exemple.
Mais lors de l'initialisation de l'instance de SimpleTransition,
destinationGraphics est toujours null.
Est-ce que vous avez une iddée pour me depanner ?
public class TransitionTesterComponent extends JPanel {
Transition transitionATester;
public TransitionTesterComponent(){
setPreferredSize(new Dimension(640,480));
transitionATester=new SimpleTransition(5000, new ImageFichier(), new
ImageFichier(), this.getGraphics());
transitionATester.start();
}
}
public class SimpleTransition extends Transition {
public SimpleTransition(int dureeTotale, ImageFichier image1,
ImageFichier image2,
Graphics destinationGraphics) {
super(dureeTotale, image1, image2, destinationGraphics);
try {
imageDebut=javax.imageio.ImageIO.read(new File("a.png"));
imageFin=javax.imageio.ImageIO.read(new File("b.png"));
} catch (IOException e) {
System.out.println("Files not found");
}
}
public void run()
}
public abstract class Transition implements Runnable {
// Variables d'instance
// A propos de la transition - graphismes
int dureeTotale;
BufferedImage imageDebut,imageFin,imageCourante;
boolean isReady;
// A propos de la transition - gestion du temps
Thread timer;
Graphics destinationGraphics;
//JComponent hostComponent;
ImageFichier imageFichierDebut, imageFichierFin;
// Constructeur
public Transition(int dureeTotale, ImageFichier image1, ImageFichier
image2,
Graphics destinationGraphics) {
// Les BufferedImages ne sont pas prets
isReady=false;
this.dureeTotale=dureeTotale;
// Assigne les ImageFichier
imageFichierDebut=image1;
imageFichierFin=image2;
this.destinationGraphics=destinationGraphics;
}
public void preload()
public void start()
public abstract void run();
}
En vue de réaliser une animation gaphique, j'aimerais qu'un thread
dessine directement sur un autre composant, un JPanel par exemple.
Mais lors de l'initialisation de l'instance de SimpleTransition,
destinationGraphics est toujours null.
Est-ce que vous avez une iddée pour me depanner ?
public class TransitionTesterComponent extends JPanel {
Transition transitionATester;
public TransitionTesterComponent(){
setPreferredSize(new Dimension(640,480));
transitionATester=new SimpleTransition(5000, new ImageFichier(), new
ImageFichier(), this.getGraphics());
transitionATester.start();
}
}
public class SimpleTransition extends Transition {
public SimpleTransition(int dureeTotale, ImageFichier image1,
ImageFichier image2,
Graphics destinationGraphics) {
super(dureeTotale, image1, image2, destinationGraphics);
try {
imageDebut=javax.imageio.ImageIO.read(new File("a.png"));
imageFin=javax.imageio.ImageIO.read(new File("b.png"));
} catch (IOException e) {
System.out.println("Files not found");
}
}
public void run()
}
public abstract class Transition implements Runnable {
// Variables d'instance
// A propos de la transition - graphismes
int dureeTotale;
BufferedImage imageDebut,imageFin,imageCourante;
boolean isReady;
// A propos de la transition - gestion du temps
Thread timer;
Graphics destinationGraphics;
//JComponent hostComponent;
ImageFichier imageFichierDebut, imageFichierFin;
// Constructeur
public Transition(int dureeTotale, ImageFichier image1, ImageFichier
image2,
Graphics destinationGraphics) {
// Les BufferedImages ne sont pas prets
isReady=false;
this.dureeTotale=dureeTotale;
// Assigne les ImageFichier
imageFichierDebut=image1;
imageFichierFin=image2;
this.destinationGraphics=destinationGraphics;
}
public void preload()
public void start()
public abstract void run();
}

Poser une question


Je pense que c'est parce que, au moment où tu fais ton getGraphics (dans le
constructeur), ton contexte graphique n'existe pas. Il faut d'abord placer
ton panneau quelque part - sur un écran, ou n'importe où ailleurs de
graphique.
Cela se fait par l'intermédiaire d'une JFrame. Met ton panneau dans une
JFrame, et à mon avis cela devrait aller mieux après.
--
Hervé AGNOUX
http://www.diaam-informatique.com
J'ai sorti l'appel a getGraphics hors du constructeur et cela marche
maintenant.
Merci beaucoup!