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
| import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Histogramme extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private static int LARGEUR = 600 ;
private static int HAUTEUR = 400 ;
public ArrayList<Baton2D> mesbatons = new ArrayList<Baton2D>() ;
RepereOrtho repere = new RepereOrtho(5,HAUTEUR-45,LARGEUR,HAUTEUR);
public void paint(Graphics g) {
super.paint(g);
repere.afficher(g);
System.out.println("Dans paint "+mesbatons.size()); // là il indique 0, le tableau est vide ... du coup il n'affiche rien
for (Baton2D baton : mesbatons) {
System.out.println("dans paint pour chaque baton "+baton.getTaille()+" "+baton.getLargeur()+" "+baton.getCouleur());
baton.afficher(g) ;
}
}
public void histogramme() {
Baton2D baton1 = new Baton2D(12,10,Color.RED) ;
mesbatons.add(baton1) ;
Baton2D baton2 = new Baton2D(10,10,Color.GREEN) ;
mesbatons.add(baton2) ;
Baton2D baton3 = new Baton2D(17,10,Color.BLUE) ;
mesbatons.add(baton3) ;
Baton2D baton4 = new Baton2D(5,10,Color.YELLOW) ;
mesbatons.add(baton4) ;
Baton2D baton5 = new Baton2D(13,10,Color.MAGENTA) ;
mesbatons.add(baton5) ;
Baton2D baton6 = new Baton2D(13,10,Color.CYAN) ;
mesbatons.add(baton6) ;
System.out.println("Dans histogramme "+mesbatons.size());
}
public static void main(String[] args) {
JFrame mafenetre = new JFrame("Histogramme") ;
JPanel panneau = new JPanel();
mafenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mafenetre.add(panneau) ;
mafenetre.setLocation(200, 200);
mafenetre.setSize(LARGEUR,HAUTEUR);
Histogramme histo = new Histogramme() ;
mafenetre.setBackground(Color.WHITE) ;
//panneau.setBackground(Color.WHITE) ;
mafenetre.add( histo, BorderLayout.CENTER );
panneau.setBackground(Color.WHITE) ;
mafenetre.setResizable(false);
mafenetre.setVisible(true);
}
}
class RepereOrtho {
private int Xorig,Yorig ;
private int larg,haut ;
RepereOrtho(int Xo,int Yo, int largeur,int hauteur){
this.Xorig = Xo ;
this.Yorig = Yo ;
this.larg = largeur ;
this.haut = hauteur ;
}
void afficher(Graphics g) {
g.setColor(Color.BLACK);
g.drawLine(Xorig, Yorig, Xorig, 5);
g.drawLine(Xorig, Yorig, larg-10, Yorig);
System.out.println("dans afficher de repere "+Xorig+" "+Yorig+" "+larg+" "+haut);
//g.drawLine(10, 10, 100, 100) ;
//System.out.println("ici");
}
}
class Baton2D {
private int taille, largeur ;
private Color couleur ;
Baton2D (int taille, int largeur,Color couleur) {
this.taille = taille ;
this.largeur = largeur ;
this.couleur = couleur ;
}
int getTaille() {
return this.taille ;
}
int getLargeur() {
return this.largeur ;
}
Color getCouleur() {
return this.couleur ;
}
void afficher(Graphics g) {
g.setColor(couleur);
System.out.println("dans afficher de baton "+taille+" "+largeur+" "+couleur);
g.fillRect(0, 0, 10, 50) ;
}
} |
Partager