Bonjour, j'ai mes deux ComboBox et mon bouton qui s'affichent bien. Quand je fais un clique sur mon bouton affiche, aucune figure ne s'affiche. J'ai cré un nouveau panel, pour accueillir le dessin, que j'ai appelé "pano", mais ca ne marche toujours pas.
Voici mon code:
Je vous donne aussi mon code de ma classe Rectangle:Code:
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.lang.String; class LFigure extends JComboBox implements ActionListener { JPanel ardoise; LFigure(JPanel ardoise) { String[] libelleFigure={"Rectangle","Circle"}; JComboBox Lfigure=new JComboBox(); for (int i=0;i<libelleFigure.length;i++) {Lfigure.addItem(libelleFigure[i]);} ardoise.add(Lfigure); addActionListener(this); } public String getFigure() { return (this.getActionCommand()); } } class LCouleur extends JComboBox implements ActionListener { JPanel ardoise; LCouleur(JPanel ardoise) { String[] libelleCouleurs={"Bleue", "Rouge", "Jaune", "Vert"}; this.setBackground(Color.lightGray); JComboBox Lcouleur=new JComboBox(); for (int i=0;i<libelleCouleurs.length;i++) {Lcouleur.addItem(libelleCouleurs[i]);} ardoise.add(Lcouleur); addActionListener(this); } public String getCouleur() { return (this.getActionCommand()); } } class Gui1 extends JPanel implements ActionListener { JPanel ardoise = new JPanel(); JPanel lesboutons = new JPanel(); LFigure Lfigure = new LFigure(ardoise); LCouleur Lcouleur = new LCouleur(ardoise); JButton affiche; JPanel pano = new JPanel(); Gui1() { setLayout(new BorderLayout(500,500)); ardoise.setSize(new Dimension(200,150)); affiche=new JButton("Afficher"); affiche.addActionListener(this); ardoise.add(affiche); add("North",ardoise); add("Center",pano); } public void actionPerformed(ActionEvent ev) { Graphics g = pano.getGraphics(); if (ev.getSource()==affiche) { if (Lcouleur.getCouleur().equals("Rouge")) g.setColor(Color.red); if (Lcouleur.getCouleur().equals("Bleue")) g.setColor(Color.blue); if (Lcouleur.getCouleur().equals("Jaune")) g.setColor(Color.yellow); if (Lcouleur.getCouleur().equals("Vert")) g.setColor(Color.green); if (Lfigure.getFigure().equals("Rectangle")) {Rectangle r1 = new Rectangle("Rectangle"); r1.draw(g);} if (Lfigure.getFigure().equals("Circle")) {Circle r2 = new Circle("Circle"); r2.draw(g);} } } public static void main(String[] argv) { JFrame monCadre = new JFrame(); monCadre.setContentPane(new Gui1()); monCadre.pack(); monCadre.setVisible(true); monCadre.show(); } }
nom fichier: Rectangle.java
Je vous remercie d'avance,Code:
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60 import java.awt.*; import java.awt.Point; public class Rectangle extends Figuresimple{ Point position; int hauteur,longueur; Color couleur; public Rectangle(String n){ super(n); position=new Point(); hauteur=2; longueur=2; nom="Rectangle"; } public Rectangle(String n, Point pos,int w,int h){ super(n); position=pos; hauteur=h; longueur=w; } public Rectangle(Color c,String n, Point pos,int w,int h){ super(n,c); position=pos; hauteur=h; longueur=w; } public void setLocation(Point p){ position=p; } public void setDimension(int w,int h){ hauteur=h; longueur=w; } public Point getPosition(){ return position; } public int getHauteur(){ return hauteur; } public int getLongueur(){ return longueur; } public void draw(Graphics g){ g.drawRect((int) position.getX(),(int) position.getY(),longueur,hauteur); } public boolean contains(int x,int y){ int a=(int) position.getX(); int b=(int) position.getY(); if ((x<(a+longueur))&&(x>a)&&(y<a)&&(y>(a-y))) return true; else return false; } public double perimetre(){ return (2*(longueur+hauteur)); } public double aire(){ return (longueur*hauteur); } }
diditin