Bonjour,
Je souhaite pouvoir dessiner dans un JPanel en changeant changer la taille de mon "crayon" en cliquant sur les boutons small et large. Voici où j'en suis:
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
public class Paint extends JDialog {
 
	PanelPaint p = new PanelPaint(); 
	private JPanel panCenter = new JPanel();
	private JPanel panWest = new JPanel();
	private JButton buttonSmall = new JButton("small");
	private JButton buttonLarge = new JButton("large");
 
	public Paint(){
		setSize(400,400);
		setLocationRelativeTo(null);
 
		//panel CENTER
		p.setBackground(Color.WHITE);
		add(p, BorderLayout.CENTER);
 
		//panl WEST
		panWest.add(buttonSmall);
		panWest.add(buttonLarge);
		add(panWest, BorderLayout.WEST);
 
		//functionality 
		buttonSmall.addActionListener(e->p.setCrayonSize(1));
		buttonLarge.addActionListener(e->p.setCrayonSize(8));
 
	}
	class sizeCrayon implements ActionListener{
		private final int valeur;
 
		public sizeCrayon (int valeur){
			this.valeur=valeur;
		}
 
		public void actionPerformed(ActionEvent e) {
			p.setCrayonSize(valeur);
		}
	}
 
	public static void main(String[] args) {
 
		Paint p = new Paint();
		p.setVisible(true);
	}
}
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
public class PanelPaint extends JPanel{
	private int pointCount=0;
	private int currentValeur=4;
	private int valeur;
 
	//array of 10000java.awt.Point references
	private DrawingPoint []points = new DrawingPoint [10000];
 
	public PanelPaint(){
		addMouseMotionListener(
				new MouseMotionAdapter(){
					public void mouseDragged(MouseEvent event){
						if(pointCount<points.length){
							points[ pointCount ] = new DrawingPoint (event.getPoint(), valeur); // find point
							++pointCount; // increment number of points in array
							repaint(); // repaint JFrame
						} // end if
					} // end method mouseDragged
				}
				);
	}
 
	public class DrawingPoint {
		public int x;
		public int y;
		public int valeur;
		public DrawingPoint(Point point, int valeur) {
			this.x=point.x;
			this.y=point.y;
			this.valeur=valeur;
		}
	}
 
	// draw ovals in a 4-by-4 bounding box at specified locations on window
	public void paintComponent( Graphics g )
	{
		super.paintComponent( g ); // clears drawing area
 
		// draw all points in array
		for ( int i = 0; i < pointCount; i++ ){
		g.fillOval( points[i].x-2, points[i].y-2, currentValeur, currentValeur );}
	} // end method paintComponent
 
	public void setCrayonSize(int valeur){
		currentValeur=valeur;
	}
} // end class PaintPanel
Le problème est que si je clique sur large (par exemple), tous mon dessin sera agrandi, alors que je voudrais pouvoir dessiner des lignes de tailles diverses.
Merci