Bonjours à tous ,

J'ai rencontré un problème avec ce code,

Le menubar s'affiche deux fois, et les lignes dessinées ne s'affichent que sur le quart de la page.

Nom : prob.JPG
Affichages : 264
Taille : 29,1 Ko


Voici le code :

La classe Panel :
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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
 
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
 
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
 
import java.awt.Graphics;
 
import javax.swing.JPanel;
 
import java.awt.Color;
 
public class Panel extends JPanel {
 
	private String colorName = "Tout";
	private Color color = Color.RED;
	private String forme = "Cercle";
	private int taille = 30;
	private int x = 0, y = 0;
	private boolean allow = false;
	private boolean listen = false;
 
	@Override
	public void paintComponent(Graphics g) {
		// TODO Auto-generated method stub
		super.paintComponents(g);
 
		if (allow == true) {
			if (colorName == "Rouge")
				g.setColor(Color.RED);
			else if (colorName == "Bleu")
				g.setColor(Color.BLUE);
			else if (colorName == "Tout") {
				int r = (int) ((Math.random()) * 256);
				int v = (int) ((Math.random()) * 256);
				int b = (int) ((Math.random()) * 256);
 
				color = new Color(r, v, b);
				g.setColor(color);
			}
			if (forme == "Cercle")
				g.fillOval(x, y, taille, taille);
			if (forme == "Carre")
				g.fillRect(x, y, taille, taille);
 
		}
		if (listen == true) {
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, this.getWidth(), this.getHeight());
			if (colorName == "Rouge")
				g.setColor(Color.RED);
			else if (colorName == "Bleu")
				g.setColor(Color.BLUE);
			else if (colorName == "Tout") {
				int r = (int) ((Math.random()) * 256);
				int v = (int) ((Math.random()) * 256);
				int b = (int) (Math.random() * 256);
 
				color = new Color(r, v, b);
				g.setColor(color);
				if (forme == "Rond")
					g.drawOval(x, y, taille, taille);
				if (forme == "Carre")
					g.drawRect(x, y, taille, taille);
			}
		}
		repaint();
	}
 
	public String getColorName() {
		return colorName;
	}
 
	public void setColorName(String colorName) {
		this.colorName = colorName;
 
	}
 
	public boolean isAllow() {
		return allow;
	}
 
	public void setAllow(boolean allow) {
		this.allow = allow;
	}
 
	public String getForme() {
		return forme;
	}
 
	public void setForme(String forme) {
		this.forme = forme;
 
	}
 
	public int getTaille() {
		return taille;
	}
 
	public void setTaille(int taille) {
		this.taille = taille;
	}
 
	public int getX() {
		return x;
	}
 
	public void setX(int x) {
		this.x = x;
	}
 
	public int getY() {
		return y;
	}
 
	public void setY(int y) {
		this.y = y;
	}
 
	public boolean isListen() {
		return listen;
	}
 
	public void setListen(boolean listen) {
		this.listen = listen;
	}
 
}
La classe Frame :
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
 
public class Frame extends JFrame{
    JMenuBar mb = new JMenuBar();
    JMenu fichier = new JMenu("Fichier");
    JMenu edition = new JMenu("Edition");
 
	Panel p = new Panel();
	public Frame() {
 
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setSize(400,400);
		this.setTitle("Test");
		this.addMouseListener(new MouseAdapter(){
 
 
 
 
			@Override
			public void mousePressed(MouseEvent e) {
				int a = e.getX() - (p.getTaille()/2);
				int b = e.getY() - (p.getTaille());
 
				p.setX(a);
				p.setY(b);
				p.setAllow(true);
				p.setListen(false);
 
			}
 
 
 
		});
		this.addMouseMotionListener(new MouseMotionListener() {
 
			@Override
			public void mouseMoved(MouseEvent e) {
 
			}
 
			@Override
			public void mouseDragged(MouseEvent e) {
 
				int a = e.getX() - (p.getTaille()/2);
				int b = e.getY() - (p.getTaille());
 
				p.setX(a);
				p.setY(b);
				p.setAllow(true);
				p.setListen(false);
			}
		});
		mb.add(fichier);
		mb.add(edition);
		this.setJMenuBar(mb);
	    this.setLayout(new BorderLayout());
		this.add(p, BorderLayout.CENTER);		
 
 
		this.setVisible(true);
	}
	public static void main(String[] args) {
		new Frame();
	}
}
Pouvez-vous m'aider svp?

Un grand merci d'avance !