Slt à tous !

J'ai un petit code qui affiche une frame avec un rectangle !

Quand on deplace le rectabgle , celui-ci laisse des traces derriere lui !

Si quelqu'un peut m'aider !

Voici le 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
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
 
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Rectangle2D;
 
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
 
 
public class Rect extends JFrame {
  DrawingCanvas canvas;
  JLabel location;
 
  public Rect() {
    super();
    Container container = getContentPane();
    canvas = new DrawingCanvas();
    container.add(canvas);
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(1, 2));
    panel.add(new JLabel("x,y: ", JLabel.RIGHT));
    location = new JLabel("");
    panel.add(location);
    container.add(panel, BorderLayout.SOUTH);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    setSize(600,600);
    setVisible(true);
    }
 
    class DrawingCanvas extends JPanel {
    double x, y, w, h;
    int x1, y1, x2, y2;
    Rectangle2D rect;
    Cursor curCursor;
 
    public DrawingCanvas() {
      x = 20;
      y = 20;
      w = 100;
      h = 100;
      addMouseListener(new MyMouseListener());
      addMouseMotionListener(new MyMouseMotionListener());
    }
 
    public void paint(Graphics g) {
      Graphics2D g2D = (Graphics2D) g;
      setBackground(new Color(236, 233, 216));
      rect = new Rectangle2D.Double(x, y, w, h);
      g2D.draw(rect);
      if (curCursor != null)
        setCursor(curCursor);
    }
 
    class MyMouseListener extends MouseAdapter {
      public void mousePressed(MouseEvent e) {
        x1 = e.getX();
        y1 = e.getY();
        canvas.repaint();
      }
 
    }
 
    class MyMouseMotionListener extends MouseMotionAdapter {
      public void mouseDragged(MouseEvent e) {
        if (rect.contains(e.getX(), e.getY())) {
              x2 = e.getX();
              y2 = e.getY();
              x = x + x2 - x1;
              y = y + y2 - y1;
              x1 = x2;
              y1 = y2;
        }
        canvas.repaint();
      }
 
      public void mouseMoved(MouseEvent e) {
        if (rect != null) {
          if (rect.contains(e.getX(), e.getY())) {
            curCursor = Cursor
                .getPredefinedCursor(Cursor.HAND_CURSOR);
          } else {
            curCursor = Cursor.getDefaultCursor();
          }
        }
        canvas.repaint();
      }
    }
 
  }
}