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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
import ...;
/**
* Cette classe permet de dessiner un camembert
* @author keum
*
*/
public class PanelCamembert extends JPanel implements ComponentListener, MouseInputListener, Observer{
/**
*
*/
private static final long serialVersionUID = 1L;
/** fenetre principale recuperee **/
MainHermesFrame hermesFrame;
/** donnees hermes recuperee de la fenetre principale **/
HermesData hermesData_;
private static final double PI_2 = 2 * Math.PI;
/** Shared color array.
*/
private static final Color[] TABLE_COLOR = new Color[12];
/** Static initializer.
*/
static {
TABLE_COLOR[0] = new Color(255, 0, 0);
TABLE_COLOR[1] = new Color(0, 255, 0);
TABLE_COLOR[2] = new Color(0,0,255);
TABLE_COLOR[3] = Color.gray;
TABLE_COLOR[4] = Color.yellow;
TABLE_COLOR[5] = Color.orange;
TABLE_COLOR[6] = Color.pink;
TABLE_COLOR[7] = Color.darkGray;
TABLE_COLOR[8] = new Color(255,255,19);
TABLE_COLOR[9] = new Color(255,25,255);
TABLE_COLOR[10] = new Color(120,12,120);
TABLE_COLOR[11] = new Color(78,65,12);
}
private static final Stroke DEFAULT_STROKE = new BasicStroke(1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
private static final Stroke SELECTED_STROKE = new BasicStroke(3f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
private static final Font DEFAULT_FONT = new Font("Dialog", Font.PLAIN, 10);
private Arc2D selectedArc = null;
private java.util.List<Arc2D> shapeList;
private Map<Arc2D, Number> arcToValueMap = new HashMap<Arc2D, Number>();
private Budget budget;
//le nombre de budgets dans notre vecteur
private int montantTotal = 0;
/** Creates a new instance.
* @param values Initial values.
*/
public PanelCamembert(MainHermesFrame hd) {
super();
initialisation(hd);
}
private void initialisation(MainHermesFrame hd){
//recuperation des donnees
hermesData_=hd.hermesData_;
//ajouter le panel en temps qu'observer
hd.hermesData_.addObservers(this);
//recuperation de la fenetre principale pour faire suivre les events d un niveau
hermesFrame=hd;
budget = hermesData_.getBudget(HermesData.getMois(), HermesData.getAnnee());
for (int i=0;i< budget.getListeCategorie().size();i++){
montantTotal += budget.retourneCategorie(i).getMontantPrevisionnel();
}
// Le montant du bugdet i du vecteur
double montantBudget=0;
shapeList = new ArrayList<Arc2D>(getNbBudget());
for (int i = 0; i < budget.getListeCategorie().size(); i++) {
montantBudget = budget.retourneCategorie(i).getMontantPrevisionnel();
Arc2D arc = new Arc2D.Double();
arc.setArcType(Arc2D.PIE);
shapeList.add(arc);
arcToValueMap.put(arc, montantBudget);
System.out.println(arcToValueMap.toString());
}
addComponentListener(this);
addMouseListener(this);
addMouseMotionListener(this);
}
private int getNbBudget(){
return budget.getListeCategorie().size();
}
private void calculateShape() {
Dimension size = getSize();
Insets insets = getInsets();
// Get past (optional) panel border.
int width = (size.width - (insets.left + insets.right))/1;
int height = (size.height - (insets.top + insets.bottom))/1;
double diameter = Math.min(width, height) - 5;
// Don't bother if too small to draw.
if (diameter > 0) {
double theta = 0;
double extend = 0;
for (int i = 0; i < budget.getListeCategorie().size(); i++) {
double montantBudget = budget.retourneCategorie(i).getMontantPrevisionnel();
System.out.println("Montant Budget = "+ montantBudget+"\n");
Arc2D arc = shapeList.get(i);
arc.setFrame(insets.left + (width - diameter) / 2.0, insets.top + (height - diameter) / 2.0, diameter, diameter);
extend = 360 * montantBudget / montantTotal;
arc.setAngleStart(theta);
arc.setAngleExtent(extend);
theta += extend;
}
}
}
/**
* {@inheritDoc}
*/
@Override protected void paintComponent(Graphics g){
super.paintComponent(g);
Dimension size = getSize();
Insets insets = getInsets();
// Get past (optional) panel border.
int width = (size.width - (insets.left + insets.right))/1;
int height = (size.height - (insets.top + insets.bottom))/1;
double diameter = Math.min(width, height) - 5;
if (diameter > 0) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
for (int i = 0; i < budget.getListeCategorie().size(); i++) {
Arc2D arc = shapeList.get(i);
Color color = TABLE_COLOR[i];
g2d.setPaint(color);
g2d.fill(arc);
if (arc != selectedArc) {
g2d.setStroke(DEFAULT_STROKE);
g2d.setPaint(getForeground());
g2d.draw(arc);
}
}
// Draw selection.
if (selectedArc != null) {
System.out.println("On a selectionné un arc\n");
g2d.setStroke(SELECTED_STROKE);
g2d.setPaint(getForeground().brighter().brighter());
g2d.draw(selectedArc);
g2d.setPaintMode();
}
// Draw labels.
double labelPosition = 2 / 3.0 * diameter / 2.0;
g2d.setPaint(getForeground());
double montantBudget = 0;
for (int i = 0; i <budget.getListeCategorie().size(); i++) {
Arc2D arc = shapeList.get(i);
montantBudget = budget.retourneCategorie(i).getMontantPrevisionnel();
double theta = arc.getAngleStart() + arc.getAngleExtent() / 2.0;
theta = PI_2 * theta / 360.0;
double x = insets.left + width / 2.0 + labelPosition * Math.cos(theta);
double y = insets.top + height / 2.0 + labelPosition * Math.sin( -theta);
Font font = DEFAULT_FONT;
if (arc == selectedArc) {
font = font.deriveFont(14f);
}
String label = String.valueOf(montantBudget);
Rectangle2D stringBounds = font.getStringBounds(label, g2d.getFontRenderContext());
LineMetrics lineMetrics = font.getLineMetrics(label, g2d.getFontRenderContext());
g2d.setFont(font);
g2d.setXORMode(Color.WHITE);
g2d.drawString(label, (float) (x - stringBounds.getWidth() / 2.0), (float) (y + lineMetrics.getAscent() / 2.0));
g2d.setPaintMode();
}
}
}
/**{@inheritDoc}
*/
public void componentResized(ComponentEvent event) {
System.out.println("on est dans componentResized\n");
calculateShape();
repaint();
}
/**{@inheritDoc}
*/
public void mouseMoved(MouseEvent event) {
selectedArc = null;
String tooltip = null;
for (Arc2D arc : shapeList) {
if (arc.contains(event.getX(), event.getY())) {
selectedArc = arc;
Number montantBudget = arcToValueMap.get(arc);
System.out.println("mouseMoved montantBudget = "+montantBudget);
tooltip = String.valueOf(montantBudget);
break;
}
}
setToolTipText(tooltip);
repaint();
}
public void componentHidden(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
public void componentMoved(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
public void componentShown(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
}
/**methode appelee lors de modification du modele **/
public void update(Observable arg0, Object arg1) {
repaint();
}
} |
Partager