Hello!
Je cherche un moyen d'afficher un polygone selon le deuxième code ci-dessous.
J'ai une JFrame, avec un JPanel comme contentPane.
Ce premier code m'affiche un polygone :
Pourquoi la manière ci-dessous ne fonctionne pas? Quand est-ce que l'instance Graphics s'instancie?
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 class PolyGone extends JComponent { public void paint(Graphics g) { int[] x = new int[]{10,15,0,20}; int[] y = new int[]{10,40,60,80}; g.drawPolygon (x, y, x.length); } } public class DrawPolygon { public static void main(String[] a) { JFrame jframe = new JFrame(); jframe.setSize(500, 500); PolyGone poly = new PolyGone(); jframe.getContentPane().add(poly); jframe.setVisible(true); } }
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 class PolyGone extends JComponent { public void paint(Graphics g) { int[] x = new int[]{10,15,0,20}; int[] y = new int[]{10,40,60,80}; g.drawPolygon (x, y, x.length); } } public class DrawPolygon { public static void main(String[] a) { JFrame jframe = new JFrame(); jframe.setSize(500, 500); //J'aimerais qqchose comme ça: JPanel panel = new JPanel(); jframe.setContentPane(panel); PolyGone poly = new PolyGone(); panel.add(poly); //Plustot que ; /* PolyGone poly = new PolyGone(); jframe.getContentPane().add(poly); */ jframe.setVisible(true); } }
ça fait quelques jours que je suis dessusvous auriez une idée?
Merci, eldorico
Partager