Affichage JComponent dans un JPanel
Bonjour,
j'ai un petit problème surement tout bête, mais j'arrive pas à le résoudre.
donc j'ai fait une classe Ligne qui est juste un drawline personnalisé.
voila le code :
Code:
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
| public class Ligne extends JComponent{
/**
*
*/
private static final long serialVersionUID = -5366088572063284316L;
Point p1;
Point p2;
protected Color couleurTrait = Color.black;
Ligne(Point p1, Point p2){
super();
this.p1 = p1;
this.p2 = p2;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(couleurTrait);
Graphics2D gr = (Graphics2D) g;
gr.draw(new Line2D.Double(p1.getCX(),p1.getCY(),p2.getCX(),p2.getCY()));
} |
et mon autre classe, qui fait office de fenêtre, avec un JPanel.
Code:
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
|
public class MapGraphique extends JFrame{
/**
*
*/
private static final long serialVersionUID = 3230617590777220387L;
JPanel map;
public MapGraphique(){
this.setSize(500,500);
map = new JPanel();
this.setTitle("Simulation");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
map.setSize(this.getHeight(), this.getWidth());
this.getContentPane().add(map);
}
void init(MapCalcul mapCalcul){
Point p1 = new Point(0,0);
Point p2 = new Point(500,500);
Ligne l = new Ligne(p1, p2);
map.add(l);
map.setBackground(Color.white);
map.repaint();
map.setVisible(true);
this.setVisible(true);
}
public static void main(String args[]) {
MapGraphique mapGraphique = new MapGraphique();
MapCalcul mapCalcul = new MapCalcul();
mapGraphique.init(mapCalcul);
}
} |
Le but de ce code, c'est d'afficher des lignes, pour dessiné une matrice, mais je remarque que la méthode paintComponent de ligne est bien appelé (si je met un Print dedans) , mais elle ne dessine pas les lignes.
Pour quel raison? j'avoue que le Graphique en Java me prend un peu la tête.
Merci de votre réponse.
Edit : Resolu, il me fallait un map.setLayout(new BorderLayout());