Salut à tous!

Je suis en train de créer un Panel de dessin qui permet de dessiner des lignes, rectangles, ellipses et dessin libre.

Ces formes sont dans une classe à part et implémentent une interface "Dessin".


Mais je ne comprends pas comment écrire le code de ma méthode "draw".




Mon programme principal :
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
134
135
136
137
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
 
class MyFrame extends JFrame{
	private JPanel myPanel = new MyPanel();
	public MyFrame(int w, int h, String titre){
		super();
		setTitle(titre);
		setSize(w,h);
		Container contenu = getContentPane();
		contenu.add(myPanel);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
}
 
 
class MyPanel extends JPanel implements MouseListener,MouseMotionListener,ActionListener{
    private int x,y,xDeb,yDeb,xFin,yFin;
    private Collection figures = new ArrayList();
    private Collection figurecourante;
    private JPanel northPanel = new JPanel();
    private JPanel centerPanel = new JPanel();
    private JPanel southPanel = new JPanel();
    private JButton btEfface = new JButton("Effacer");
    private ButtonGroup choixFonction = new ButtonGroup();
    private JRadioButtonMenuItem radioRect = new JRadioButtonMenuItem("Rectangle");
    private JRadioButtonMenuItem radioOval = new JRadioButtonMenuItem("Ellipse");
    private JRadioButtonMenuItem radioFree = new JRadioButtonMenuItem("Libre", true);
    private JRadioButtonMenuItem radioLine = new JRadioButtonMenuItem("Ligne");
    private JRadioButtonMenuItem radioPolygon = new JRadioButtonMenuItem("Polygone");
 
 
 
 
	//Constructeur
	public MyPanel(){
	    setLayout(new BorderLayout());
	    add(northPanel, BorderLayout.NORTH);
	    add(centerPanel, BorderLayout.CENTER);
	    add(southPanel, BorderLayout.SOUTH);
	    addMouseListener(this);
	    addMouseMotionListener(this);
	    northPanel.add(btEfface);
	    btEfface.addActionListener(this);
	    choixFonction.add(radioRect);
	    choixFonction.add(radioOval);
	    choixFonction.add(radioFree);
	    choixFonction.add(radioLine);
	    choixFonction.add(radioPolygon);
	    radioRect.addActionListener(this);
	    radioOval.addActionListener(this);
	    radioFree.addActionListener(this);
	    radioLine.addActionListener(this);
	    radioPolygon.addActionListener(this);
	    southPanel.add(radioRect);
	    southPanel.add(radioOval);
	    southPanel.add(radioFree);
	    southPanel.add(radioLine);
	    southPanel.add(radioPolygon);
    }
 
	private void dessinerDynamique (int x, int y){
	    this.x = x;
	    this.y = y;
	    Graphics g = getGraphics ();
	    g.setXORMode(getBackground());
	    if ( xDeb != x && yDeb != y){
			if (radioRect.isSelected()){
				g.drawRect(Math.min(xDeb, x), Math.min(yDeb, y), Math.abs(xDeb - x), Math.abs(yDeb - y));
			}
			else if (radioOval.isSelected()){
				g.drawOval(Math.min(xDeb, x), Math.min(yDeb, y), Math.abs(xDeb - x), Math.abs(yDeb - y));
			}
			else g.drawLine(x,y,xFin,yFin);
		}
    }
 
 
    public void paintComponent(Graphics g){
	    Collection figurecourante;
	    super.paintComponent(g);
 
 
// /!\.......... Ici le code concerné ........../!\
	    Iterator it = figures.iterator();
	    while (it.hasNext()){
			Figure fig = (Figure) it.next();
			fig.draw(g);
	    }
 
 
    }
 
    public void mousePressed(MouseEvent e){
	    xDeb = e.getX();
	    yDeb = e.getY();
	    figurecourante = new ArrayList();
	    figurecourante.add(new Point(xDeb,yDeb));
	    x = xDeb;
	    y = yDeb;
    }
 
	public void mouseDragged(MouseEvent e){
		xFin = e.getX();
		yFin = e.getY();
		dessinerDynamique(x,y);
		dessinerDynamique(xFin,yFin);
		x = xFin;
		y = yFin;
		if (radioFree.isSelected()){
			figurecourante.add(new Point(x,y));
		}
	}
 
	public void mouseReleased(MouseEvent e){
		figures.add(figurecourante);
	}
 
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
	public void mouseMoved(MouseEvent e){}
	public void mouseClicked(MouseEvent e){}
	public void actionPerformed(ActionEvent e){
		figures.clear();
		this.repaint();
	}
}
 
//**********************************************************
public class TPDessin{
	public static void main(String[] args){
        JFrame frame = new MyFrame(600,400,"Dessin sur le panel");
        frame.show();
    }
}

Mon interface Dessin et sa méthode draw() :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
import java.awt.*;
public interface Dessin{
	public void draw(Graphics g);
	}

Et ma classe Figure qui implémente l'interface Dessin et qui comprend les différentes formes de dessin :
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
import java.awt.*;
import java.util.*;
 
class Figure implements Dessin{
	public void draw(Graphics g){
	}
}
 
class RectanglOval extends Figure implements Dessin{
	private int x,y,w,h;
	private boolean rect;
	private Point p1,p2;
	private Collection points = new ArrayList();
	private int[] wh = new int[2];
 
 
 
// /!\.......... Ici le code concerné ........../!\
	public void draw(Graphics g){
		if (rect){
 
			Iterator it = points.iterator();
			p1 = (Point) it.next();
			p2 = (Point) it.next();
			x = ( (int) calcRectOrigin(p1, p2).getX());
			y = ( (int) calcRectOrigin(p1, p2).getY());
			this.wh = calcRectWH(p1, p2);
			w = wh[0];
			h = wh[1];
			drawRect(x,y,w,h);
 
		}
	}
 
 
 
	//Constructeur
    public RectanglOval(boolean rect){
		this.rect=rect;
	}
 
	public Point calcRectOrigin(Point p1, Point p2){
		Point res = new Point();
		res.x = Math.min(p1.x, p2.x);
		res.y = Math.min(p1.y, p2.y);
		return res;
	}
 
	public int[] calcRectWH(Point p1, Point p2){
		w = Math.abs(p1.x - p2.x);
		h = Math.abs(p1.y - p2.y);
		wh[0] = w;
		wh[1] = h;
		return wh;
	}
 
 
 
}
 
 
class PolyLine extends Figure implements Dessin{
	private int xDeb,yDeb,x,y;
	private boolean polygon;
	private Collection polyLine = new ArrayList();
 
	public PolyLine(int xDeb, int yDeb, boolean polygon){
		this.xDeb = xDeb;
		this.yDeb = yDeb;
		this.polygon = polygon;
	}
	public void add(Point p){polyLine.add(p);}
 
 
// /!\.......... Ici le code concerné ........../!\
	public void draw(Graphics g){
		if (polygon){
			Iterator it = polyLine.iterator();
			Point pOrigin = (Point) it.next();
			Point p1 = pOrigin;
			Point p2 = (Point) it.next();
			while (it.hasNext()){
				g.drawLine((int) p1.getX(), (int) p1.getY(), (int) p2.getX(), (int) p2.getY());
				p2 = (Point) it.next();
				p1 = p2;
			}
			g.drawLine((int) pOrigin.getX(), (int) pOrigin.getY(), (int) p2.getX(), (int) p2.getY());
		}
		else {
			Iterator it = polyLine.iterator();
			Point p1 = (Point) it.next();
			while (it.hasNext()){
				Point p2 = (Point) it.next();
				g.drawLine((int) p1.getX(), (int) p1.getY(), (int) p2.getX(), (int) p2.getY());
				p1 = p2;
			}
		}
 
	}
}
Comment faire pour qu'en appelant la méthode draw() mes dessins soient restitués sur le panel?