Bonjour,

après avoir lu pas mal de tutoriaux et testé des tonnes de solutions, je viens chercher de l'aide car il y a un truc bête que je n'ai pas dû comprendre dans toutes ces docs...

Mon problème : je souhaite dessiner des objets graphiques (des points par exemple) sur un Jpanel (qui à terme comportera une image en arrière plan, mais chaque chose en son temps).

Pour le moment j'en suis réduit à tester des bouts de code pour comprendre ce qu'il se passe...

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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
 
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
 
public class testVisu {
 
	private ArrayList<GraphPoint> graphPoints = new ArrayList<GraphPoint>();  //  @jve:decl-index=0:
 
	private JFrame jFrame = null; // @jve:decl-index=0:visual-constraint="288,144"
 
	private JPanel jContentPane = null;
 
	private JPanel jPanel = null; // @jve:decl-index=0:visual-constraint="203,157"
 
	/**
         * This method initializes jFrame
         * 
         * @return javax.swing.JFrame
         */
	private JFrame getJFrame() {
		if (jFrame == null) {
			jFrame = new JFrame();
			jFrame.setSize(new Dimension(400, 400));
			jFrame.setContentPane(getJContentPane());
 
			jFrame.setVisible(true);
 
			jFrame.addWindowListener(new java.awt.event.WindowAdapter() {
				public void windowClosing(java.awt.event.WindowEvent e) {
					jFrame.dispose();
				}
			});
		}
		return jFrame;
	}
 
	/**
         * This method initializes jContentPane
         * 
         * @return javax.swing.JPanel
         */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
 
			jContentPane = new JPanel();
			jContentPane.setLayout(new FlowLayout());
			jContentPane.setSize(new Dimension(350, 350));
			jContentPane.add(getJPanel());
 
		}
		return jContentPane;
	}
 
	/**
         * This method initializes jPanel
         * 
         * @return javax.swing.JPanel
         */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			 jPanel.setBackground(Color.yellow);
			 jPanel.setLayout(new FlowLayout());
			jPanel.setPreferredSize(new Dimension(300, 300));
 
		}
		return jPanel;
	}
 
 
 
	private void addPoint(int _x, int _y, Color color) {
		GraphPoint pt1 = new GraphPoint(_x, _y, color);
		graphPoints.add(pt1);
		getJPanel().add(pt1);
	}
 
	private class GraphPoint extends JComponent {
 
		Ellipse2D.Double point = null;
 
		Color color = null;
 
		public GraphPoint(int _x, int _y, Color _color) {
 
			color = _color;
			point = new Ellipse2D.Double(_x, _y, 20, 20);
 
		}
 
		public void move(int _x, int _y) {
 
			point.x = _x;
			point.y = _y;
 
		}
 
 
 
		protected void paintComponent(Graphics g) {
			Graphics2D g2d = (Graphics2D) g;
 
 
			g2d.setColor(color);
			g2d.fill(point);
		}
 
	}
	public static void main(String[] args) {
		testVisu app = new testVisu();
		app.getJFrame();
//fonctionne
//		 Ellipse2D.Double point = new Ellipse2D.Double(150, 150, 20, 20);
//		 Graphics2D g2d = (Graphics2D)app.getJPanel().getGraphics().create();
//		 g2d.setColor(Color.BLUE);
//		 g2d.fill(point);
 
		//ajouter un GraphPoint
		app.addPoint(100, 100, Color.RED);
 
		JButton button =new JButton();
		button.setPreferredSize(new Dimension(100,20));
		button.setText("toto");
		app.jPanel.add(button);
 
//		app.jPanel.repaint();
//		DriverPoint pt2 = app.new DriverPoint(200, 100, Color.blue);
//		app.getJPanel().add(pt2);
		// app.getJPanel().repaint();
	}
}
ce code affiche un panel, ajoute un cercle rouge rempli et un bouton.

J'ai 2 questions :
- pourquoi le cercle rouge n'apparait pas (en utilisant l'objet Graphpoint) ?
- pourquoi le bouton n'apparait que quand la fenêtre est redimensionnée (ou réduite) ? (le composant est dessiné correctement dans ces 2 cas, mais pourquoi pas dès le départ)