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
|
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
public class AppletCamembert extends Applet {
public void init() {
setSize(500, 300);
if (getParameter("TITLE") != (null))
this.title = getParameter("TITLE");
else
this.title = "No title";
if (getParameter("DONNEES") != (null)){
System.out.println (title + "\n" + getParameter("DONNEES"));
this.donnees = new Donnees(getParameter("DONNEES"));
} else
empty = true;
}
public void paint(Graphics g) {
float angle = 0, anglebis, anglemid;
String value;
int size = 20;
g.setFont(new Font("Tahoma", 1, size));
g.drawString(title, 300, 80);
if (!empty) {
g.drawArc(20, 30, 250, 250, 0, 360);
for (int i = 0; i < donnees.valeurs.length; i++)
somme += donnees.valeurs[i];
g.setFont(new Font("Tahoma", 0, 20));
for (int i = 0; i < donnees.valeurs.length; i++) {
float pourcentage = donnees.valeurs[i] / this.somme;
anglebis = (float) (angle + pourcentage * 360);
g.setColor(new Color((float) Math.random(), (float) Math
.random(), (float) Math.random()));
g.fillRect(330, 100 + 20 * i, 10, 10);
g.fillArc(20, 30, 250, 250, 180 - Math.round(angle), -Math
.round(pourcentage * 360));
angle = anglebis;
}
angle = 0;
for (int i = 0; i < donnees.valeurs.length; i++) {
float pourcentage = donnees.valeurs[i] / this.somme;
anglebis = (float) (angle + pourcentage * 360);
anglemid = (anglebis + angle) / 2;
g.setColor(Color.BLACK);
g.drawString(donnees.legendes[i], 360, 110 + 20 * i);
value = Float.toString(donnees.valeurs[i]) + "%";
if (pourcentage >= 0.1)
g.drawString(value, 120 - (int) (75 * Math.cos(Math
.toRadians((double) anglemid))),
170 - (int) (75 * (Math.sin(Math
.toRadians((double) anglemid)))));
angle = anglebis;
}
}
else {
g.setColor(Color.RED);
g.setFont(new Font("Tahoma", 1, 30));
g.drawString("No statistic found", 100, 200);
}
}
public void stop () {
title = null;
donnees = null;
}
private String title;
private Donnees donnees;
private float somme;
private boolean empty;
private static final long serialVersionUID = 1L;
} |
Partager