Bonjour,
Je connais certaines bases d'AWT/SWING depuis un certain temps et j'ai essayé d'approfondir le côté graphique.
Voici donc le code simplifié des deux classes sur lesquelles je m'entraîne.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 import java.awt.*; import javax.swing.*; public class TestJFrame extends JFrame { private TestJPanel testJPanel; public TestJFrame() { setTitle("TestJFrame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); testJPanel = new TestJPanel(400,300); JPanel panneauAffichage = new JPanel(new GridLayout(1,1)); panneauAffichage.add(testJPanel); getContentPane().add(panneauAffichage); pack(); setVisible(true); } public static void main(String[] args) { TestJFrame test = new TestJFrame(); } }Mon problème est que lorsque j'exécute la classe TestJFrame, la fenetre s'affiche bien mais pas le texte "Panneau Test" !
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; public class TestJPanel extends JPanel implements ActionListener { private int tailleX; private int tailleY; private JPanel panneauGraphique; private Button boutonRepaint; public TestJPanel(int tailleX,int tailleY) { this.tailleX = tailleX; this.tailleY = tailleY; panneauGraphique = new JPanel(); panneauGraphique.setPreferredSize(new Dimension(tailleX,tailleY)); boutonRepaint = new Button("Repaint"); boutonRepaint.addActionListener(this); setLayout(new BorderLayout()); add(panneauGraphique); add(boutonRepaint,BorderLayout.SOUTH); } public void actionPerformed(ActionEvent e) { repaint(); } public void paint(Graphics graphique) { super.paint(graphique); Graphics2D graphiquePanneau = (Graphics2D) panneauGraphique.getGraphics(); Rectangle2D rectangleTexte = graphiquePanneau.getFont().getStringBounds("Panneau Test",graphiquePanneau.getFontRenderContext()); Point pointMilieuGraphe = new Point(tailleX,tailleY); graphiquePanneau.setColor(Color.BLACK); graphiquePanneau.drawString("Panneau Test",tailleX/2-(int)rectangleTexte.getWidth()/2,tailleY/2-(int)rectangleTexte.getHeight()/2); graphiquePanneau.dispose(); } }
Appuyer sur le bouton "Repaint" ne permet pas non plus l'affichage du texte.
En fait, pour que le texte s'affiche, il faut cacher la fenetre sur les côtés de l'écran puis la remonter...
J'ai déjà essayé de remplacer paint par paintComponent et d'ajouter ou non l'appel à la méthode surchargée : toujours pas de texte !
Je ne comprends pas ce qui ne va pas dans mon code, j'ai vraiment besoin de votre aide.
Merci.
Partager