Bonjour

Question assez bête.
Je veux dessiner sur un JPanel un rectangle à la souris.
Or, si mes valeurs width et/ou height de mon rectangle sont négatives, je n'ai pas de dessin. Comment résoudre ce problème ?
Voici mon code:
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
 
public class MyGLCanvas extends /*GLCanvas*/JPanel implements MouseListener, MouseMotionListener {
 
    private Point start,  end;
 
    public MyGLCanvas() {
        super();
        this.setBackground(Color.WHITE);
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
    }
 
    @Override
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D)g;
        g2d.setColor(Color.BLACK);
        if(start!=null && end!=null){
            g2d.clearRect(0, 0, this.getWidth(), this.getHeight());
            Rectangle r = new Rectangle();
            Point S = start;
 
            int width = end.x-start.x;
            int height = end.y-start.y;
 
            r.setLocation(S);
            r.setSize(new Dimension(width, height));
            g2d.drawRect(start.x, start.y, width, height);
        }
        else
            g2d.clearRect(0, 0, this.getWidth(), this.getHeight());
    }
 
    public void mouseClicked(MouseEvent arg0) {
    }
 
    public void mousePressed(MouseEvent arg0) {
        if(start == null)
            start = arg0.getPoint();
    }
 
    public void mouseReleased(MouseEvent arg0) {
        start = end = null;
        repaint();
    }
 
    public void mouseEntered(MouseEvent arg0) {
    }
 
    public void mouseExited(MouseEvent arg0) {
    }
 
    public void mouseDragged(MouseEvent arg0) {
        if (start != null) {
            end = arg0.getPoint();
            repaint();
        }
    }
 
    public void mouseMoved(MouseEvent arg0) {
    }
}
Merci d'avance.

@++