Bonjour à tous,
Me revoilà encore avec un problème!

J'ai ajouté un JScrollPane a mon JPanel (zoneDeDessin) malheureusement je n'arrive plus a dessiner sur le JPanel. Par contre je peux changer la couleur de fond du JPanel

Merci d'avance pour votre aide
JFrame
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
public class MainWindows extends JFrame{
 //private Container contenu = new Container();
 
    @SuppressWarnings("LeakingThisInConstructor")
  public MainWindows()
    {
 
        this.setTitle("EasyPaint"); // Titre
	this.setSize(900, 600); // Dimension
	this.setLocationRelativeTo(null); // Position Centrale
	//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Fermeture
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
 
        // On initialise le menu
	this.initMenu();
        this.initItem();
        // Idem pour la barre d'outils
        this.initToolBar();
        this.addMouseListener(zoneDessin);
        this.addMouseMotionListener(zoneDessin);      
        JScrollPane Scroll = new JScrollPane(zoneDessin);
        this.Rules(Scroll);
        this.getContentPane().add(Scroll,FlowLayout.CENTER);
        this.getContentPane().add(ZoneInfo,BorderLayout.SOUTH);
        // On positionne notre zone de dessin
 
	fen=this;
	this.setVisible(true);
 
        this.addWindowListener( new WindowAdapter()
        { public void windowClosing(WindowEvent e) {
                fen.fermer();
			}
		});       
}
 
    private Rule columnView;
    private Rule rowView;
    private void Rules(JScrollPane ScrollP)
    {
        //Create the row and column headers.
        columnView = new Rule(Rule.HORIZONTAL, true);
        rowView = new Rule(Rule.VERTICAL, true);
        columnView.setPreferredHeight(30);
        rowView.setPreferredWidth(30);
        Border cadre = BorderFactory.createBevelBorder(0,Color.GREEN,Color.GREEN);
        rowView.setBorder(cadre);
        columnView.setBorder(cadre);
        ScrollP.setColumnHeaderView(columnView);
        ScrollP.setRowHeaderView(rowView);
 
        //Create the corners.
        JPanel buttonCorner = new JPanel();
        JToggleButton isMetric =  new JToggleButton("cm", true);
        isMetric.setFont(new Font("SansSerif", Font.PLAIN, 6));
        isMetric.setMargin(new Insets(2,2,2,2));
        //isMetric.addItemListener(this);
        buttonCorner.add(isMetric);
        ScrollP.setCorner(JScrollPane.UPPER_LEFT_CORNER,buttonCorner);
    }
Zone de dessin (JPANEL)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
 
package paint;
 
import java.awt.Color;
import java.awt.event.ItemListener;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.Graphics;
import java.awt.Point;
import java.io.File;
import java.awt.Dimension;
import java.util.*;
import java.awt.Image;
import java.io.File;
import java.awt.Cursor;
import java.awt.BorderLayout;
 
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.awt.Rectangle;
 
import javax.swing.JMenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
 
//import java.awt.Rectangle;
/**
 *
 * @author Aurelie
 */
public class ZoneDeDessin extends JPanel implements MouseListener, MouseMotionListener {
 
    public int type;
    public boolean select = false;
    public boolean rot = false;
    private boolean imgvue = false;
    private boolean del = false;
    public Point p1 = new Point();
    public Point p2 = new Point();
    private Color Fcouleur = Color.WHITE;
    private Color Ocouleur = Color.BLACK;
    public boolean in = true;
    private Dimension dim = new Dimension();
    public boolean first = true;
    public int angle;
    public int depart;
    public Image image = null;
    StairsInfos st = new StairsInfos();
    private int j = 0;
    public IMoveDrawing LastOneSelc;
 
 
 
// Créaction d'une liste pour dessiner les différents composants
    private List drawing = new LinkedList();
 
 
// Constructeur
    @SuppressWarnings("OverridableMethodCallInConstructor")
        public ZoneDeDessin() {
        this.setSize(300, 500);
        this.setCursor(java.awt.Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        //JScrollPane Scroll = new JScrollPane(this);     
        this.repaint();
        this.setVisible(true);
 
    }
 
 
 
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
 
        /*if (image != null) // Si l'image existe, ...
        {
            if (this.imgvue) {
                Dimension v = this.getSize();
                g.drawImage(image, 20, 20, v.width*2, v.height*2, this); // ... on la dessine
 
            }
        }*/
        for (Iterator it = drawing.iterator(); it.hasNext();) {
            IDrawing d = (IDrawing) it.next();
            d.draw(g, 2);
 
        }
        this.setBackground(Fcouleur);
        this.Dessiner(g);
 
 
    }
 
    public void DelImg() {
        this.imgvue = false;
        this.repaint();
    }
 
    public void MontImg() {
        this.imgvue = true;
        this.repaint();
 
    }
 
 
 
    public void addDrawing(IDrawing d) {
        drawing.add(d);
        this.repaint();
    }
 
 
    public void Defbackground(Color coul) {
        Fcouleur = coul;
        this.repaint();
 
    }
 
    public void DefCouleur(Color coul) {
        Ocouleur = coul;
    }
 
 
 
    /**
     * Méthode appelée lorsque l'on relâche le clic de souris
     */
    public void mouseReleased(MouseEvent event) {
        if (select) {
            select = false;
 
        }
        if (!del) {
 
            switch (type)
            {
                case 3:
                     {
                dim.height = p2.y - p1.y;
                dim.width = p2.x - p1.x;
                IDrawing line = new LineDrawing(Ocouleur, p1, dim);
                this.addDrawing(line);
 
            } case 1: {
                dim.height = p2.x - p1.x;
                dim.width = p2.y - p1.y;
                IDrawing Rec = new RectangleDrawing(Ocouleur, p1, dim);
                this.addDrawing(Rec);
                break;
            } case 5: {
                dim.height = p2.x - p1.x;
                dim.width = p2.y - p1.y;
                IDrawing Arc = new ArcDrawing(Ocouleur, p1, dim, depart, angle);
                this.addDrawing(Arc);
                break;
            } case 2: {
                dim.height = p2.x - p1.x;
                dim.width = p2.y - p1.y;
                IDrawing cercle = new CercleDrawing(Ocouleur, p1, dim);
                this.addDrawing(cercle);
                break;
            } case 6: {
                dim.height = st.ht;
                dim.width = st.lm;
                IDrawing porte = new StairsDrawing(Ocouleur, p1, dim, st.nbm);
                this.addDrawing(porte);
                break;
            } case 7: {
                dim.height = 180;
                dim.width = 180;
                IDrawing porte = new DoorDrawing(Ocouleur, p1, dim);
                this.addDrawing(porte);
                break;
            }
        }
    }
    }
 
    public void mouseMoved(MouseEvent event) {
    }
    public void Dessiner(Graphics g) {
        if (!del) {
         this.setCursor(java.awt.Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
            switch (type)
            {
                case 3:
                {
                    g.drawLine(p1.x, p1.y, p2.x, p2.y);
                    break;
                }
                case 1: {
                dim.height = p2.x - p1.x;
                dim.width = p2.y - p1.y;
                g.drawRect(p1.x, p1.y, dim.height, dim.width);
                break;
                }
                case 5: {
                dim.height = p2.x - p1.x;
                dim.width = p2.y - p1.y;
                g.drawArc(p1.x, p1.y, dim.height, dim.width, depart, angle);
                break;
                }
                case 2: {
                dim.height = p2.x - p1.x;
                dim.width = p2.y - p1.y;
                g.drawOval(p1.x, p1.y, dim.width, dim.height);
                break;
                //CercleDrawing(Color color, Point pos, Dimension dim);
                }
            }
 
        }
 
    }
}
Dessin d'un rectangle
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
package paint;
 
import java.awt.*;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
/**
 *
 * @author Aurelie
 */
public class RectangleDrawing  extends FormDrawing
{
      public  RectangleDrawing(Color color, Point pos, Dimension dim)
    {
        super(color,pos,dim);
    }
 
    @Override
    public void draw(Graphics g, int ep)
    {
        if(select)
            ep=5;
        else
            ep=1;
        Graphics2D g2 =(Graphics2D) g;
        g2.setStroke(new BasicStroke(ep));
        GeneralPath path = new GeneralPath();
        g2.setColor(color);
 
        if ((rect.width < 0) || (rect.height < 0)) {
	    return;
	}
 
	if (rect.height == 0 || rect.width == 0) {
	    path.moveTo(rect.x,rect.y);
            path.lineTo(rect.x+rect.height, rect.y + rect.width);
	} else {
 
        path.moveTo(rect.x,rect.y);
        path.lineTo(rect.x+rect.height-ep,rect.y);
        path.moveTo(rect.x+rect.height,rect.y);
        path.lineTo(rect.x+rect.height,rect.y+rect.width-ep);
        path.moveTo(rect.x+rect.height,rect.y+rect.width);
        path.lineTo(rect.x+ep,rect.y+rect.width);
        path.moveTo(rect.x,rect.y+rect.width);
        path.lineTo(rect.x,rect.y+ep);
       /*
        AffineTransform orig = g2.getTransform();
        AffineTransform rat = new AffineTransform();
        //rat.rotate(Math.PI);
        rat.translate(200,200);
        //System.out.print("\nAlpha: "+this.alpha);
        g2.setTransform(rat);
        //g2.setTransform(orig);*/
       }
 
        g2.draw(path);
 
    }
}