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
| 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 = 800 ;
private static int HAUTEUR = 600 ;
public ArrayList<Baton2D> mesbatons = new ArrayList<Baton2D>() ;
RepereOrtho repere = new RepereOrtho(5,HAUTEUR-45,LARGEUR,HAUTEUR);
public void paint(Graphics g) {
int nouvX = 10 ;
super.paint(g);
repere.afficher(g);
for (Baton2D baton : mesbatons) {
baton.afficher(g,nouvX,HAUTEUR-45) ;
nouvX = nouvX + 4 * baton.getLargeur() + 10 ;
}
}
public 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(8,10,Color.CYAN) ;
mesbatons.add(baton6) ;
}
public static void main(String[] args) {
JFrame mafenetre = new JFrame("Histogramme") ;
JPanel panneau = new JPanel();
mafenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mafenetre.setLocation(200, 200);
mafenetre.setSize(LARGEUR,HAUTEUR);
Histogramme histo = new Histogramme() ;
histo.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); // Trait vertical
g.drawLine(Xorig, Yorig, larg-10, Yorig); // Trait Horizontal
}
}
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,int _x,int hauteur_fenetre) {
int hauteur = taille * 20 ;
int y_corner = hauteur_fenetre - 5 - hauteur ;
g.setColor(couleur);
g.fillRect(_x, y_corner, 4*largeur, hauteur) ;
g.setColor(Color.BLACK);
g.drawString(String.valueOf(taille), _x+10, y_corner-5) ;
}
} |
Partager