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
|
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.*;
public class Graph extends JPanel{
//le type de graphique que l'on souhaite
public static final int VALUES_ONLY = 0;
public static final int VALUES_AND_STRING = 1;
public static final int VALUES_STRING_AND_COLOR = 2;
public static final int VALUES_STRING_COLOR_AND_LADDER = 3;
public static final int VALUES_AND_COLOR = 4;
public static final int VALUES_AND_LADDER = 5;
//les proprietées
private int CURRENT_TYPE = 0;
private Object[] VALUES = {1 , 3 , 5 , 10} ;
private Object[][] _VALUES = null ;
private Object[][] LADDER = null ;
public Graph(){
super();
setOpaque ( true );
setBackground(Color.WHITE);
setSize(800,600);
setBounds(0,0,800,600);
setLayout(null);
}
public void createGraph(){
repaint();
}
@Override
public void paint(Graphics g){
//pour avoir le fonds blanc
g.setColor(Color.WHITE);
g.fillRect(0, 0, getSize().width, getSize().height);
//on place la légende avant d'ajouter les valeurs
try{
if(CURRENT_TYPE == VALUES_STRING_COLOR_AND_LADDER || CURRENT_TYPE == VALUES_AND_LADDER ){
for ( int i = 0 ; i < LADDER.length ; i++){
try{
g.setColor(Color.RED);
g.drawLine(0, getSize().height-(Integer)LADDER[i][0],getSize().width , getSize().height-(Integer)LADDER[i][0]);
g.setColor(Color.BLACK);
g.drawString( LADDER[i][1].toString(), 10,getSize().height-(Integer)LADDER[i][0]);
}
catch(Exception e){System.out.println("!");}
}
}
}
catch (NullPointerException e){
System.err.println(
"Missing type Exception_ Veuillez renseigner le type de graphique");
e.printStackTrace();
return ;
}
//on dessine apres les valeurs en fonction de la demande
if ( CURRENT_TYPE == VALUES_ONLY || CURRENT_TYPE == VALUES_AND_LADDER){
//recuperation de la valeur maximale des stats a traiter
int max = getMaxValue(true);
//on recupere le rapport proportionel valeur / hauteur du graph
float rapport = (float)((float)(getSize().height-50 )/ (float)max);
//calcul de la position x des batons
int sizeX = ((getSize().width)-((VALUES.length*20)+20))/VALUES.length;
//convertion du graphics en grahics2D pour l'effet de dégradé
Graphics2D g2 = (Graphics2D)g;
for ( int i = 0 ; i < VALUES.length ; i ++ ){
int locationY = (getSize().height-(int)(rapport * (Integer)VALUES[i]));
int locationX = (i*sizeX)+(i*20)+20;
g2.setPaint(new GradientPaint(0,0,Color.BLUE,getSize().width,getSize().height,Color.GREEN,true));
g2.fillRect(
locationX
, locationY
, sizeX, 600 );
g.setColor(Color.BLACK);
g.drawRect(locationX
, locationY
, sizeX, 600 );
}
}
if (CURRENT_TYPE == VALUES_AND_STRING){
//recuperation de la valeur maximale des stats a traiter
int max = getMaxValue(false);
//on recupere le rapport proportionel valeur / hauteur du graph
float rapport = (float)((float)(getSize().height-50 )/ (float)max);
//calcul de la position x des batons
int sizeX = ((getSize().width)-((_VALUES.length*20)+20))/_VALUES.length;
//convertion du graphics en grahics2D pour l'effet de dégradé
Graphics2D g2 = (Graphics2D)g;
for ( int i = 0 ; i < _VALUES.length ; i ++ ){
int locationY = (getSize().height-(int)(rapport * (Integer)_VALUES[i][0]));
int locationX = (i*sizeX)+(i*20)+20;
g2.setPaint(new GradientPaint(0,0,Color.BLUE,getSize().width,getSize().height,Color.GREEN,true));
g2.fillRect(
locationX
, locationY
, sizeX, 600 );
g.setColor(Color.BLACK);
g.drawRect(locationX
, locationY
, sizeX, 600 );
g.drawString(_VALUES[i][1].toString(), locationX, locationY-10);
}
}
if(CURRENT_TYPE == VALUES_STRING_AND_COLOR || CURRENT_TYPE == VALUES_STRING_COLOR_AND_LADDER ){
//recuperation de la valeur maximale des stats a traiter
int max = getMaxValue(false);
//on recupere le rapport proportionel valeur / hauteur du graph
float rapport = (float)((float)(getSize().height-50 )/ (float)max);
//calcul de la position x des batons
int sizeX = ((getSize().width)-((_VALUES.length*20)+20))/_VALUES.length;
for ( int i = 0 ; i < _VALUES.length ; i ++ ){
int locationY = (getSize().height-(int)(rapport * (Integer)_VALUES[i][0]));
int locationX = (i*sizeX)+(i*20)+20;
g.setColor((Color)_VALUES[i][2]);
g.fillRect(locationX
, locationY
, sizeX, 600 );
g.setColor(Color.BLACK);
g.drawRect(locationX
, locationY
, sizeX, 600 );
g.drawString(_VALUES[i][1].toString(), locationX, locationY-10);
}
}
if(CURRENT_TYPE == VALUES_AND_COLOR){
//recuperation de la valeur maximale des stats a traiter
int max = getMaxValue(false);
//on recupere le rapport proportionel valeur / hauteur du graph
float rapport = (float)((float)(getSize().height-50 )/ (float)max);
//calcul de la position x des batons
int sizeX = ((getSize().width)-((_VALUES.length*20)+20))/_VALUES.length;
for ( int i = 0 ; i < _VALUES.length ; i ++ ){
int locationY = (getSize().height-(int)(rapport * (Integer)_VALUES[i][0]));
int locationX = (i*sizeX)+(i*20)+20;
g.setColor((Color)_VALUES[i][1]);
g.fillRect(locationX
, locationY
, sizeX, 600 );
g.setColor(Color.BLACK);
g.drawRect(locationX
, locationY
, sizeX, 600 );
}
}
}
private int getMaxValue(boolean isSingleTable){
if (isSingleTable){
int max = 0 ;
for ( int i = 0 ; i < VALUES.length ; i ++ ){
if( (Integer)VALUES[i] > max){
max = (Integer)VALUES[i];
}
}
return max ;
}
else{
int max = 0 ;
for ( int i = 0 ; i < _VALUES.length ; i ++ ){
if( (Integer)_VALUES[i][0] > max){
max = (Integer)_VALUES[i][0];
}
}
return max ;
}
}
//les getters
public int getType(){
return CURRENT_TYPE ;
}
public Object[] getValues(){
return VALUES ;
}
public Object[][]getMiltipleValues(){
return _VALUES;
}
public Object[] getLadder(){
return LADDER;
}
//les setters
public void setType(int type){
CURRENT_TYPE = type ;
}
public void setValues( Object [] values){
VALUES = values ;
}
public void setValues( Object [][] values){
_VALUES = values ;
}
public void setLadder(Object [][] ladder){
LADDER = ladder ;
}
} |
Partager