|
Trouver une ressource
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
GRAPHIQUE EN BÂTON
Information sur la source
Description
Une classe permettant de créer des graphiques en bâton , plusieurs mises en forme sont disponnibles ( avec ou sans repères/légendes , couleurs déradée ou unies , couleurs différentes pour chaque bâton ou non , valeures inscrites au dessu des batons ou pas ....) , ce n'est rien d'extraordinaire mais ca montre quand meme l'utilisation de la methode paint(Graphics g) et la converstion en Graphics2D. Certainement à ameliorer quand j'aurais plus de temps .
Source
- /*
- * @author Tlaloc
- */
- import java.awt.Color;
- import java.awt.GradientPaint;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import javax.swing.JPanel;
-
-
- 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 = null ;
- 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 ;
- }
- }
/*
* @author Tlaloc
*/
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
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 = null ;
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 ;
}
}
Sources du même auteur
Sources de la même categorie
Sources en rapport avec celle ci
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
JAVA2D et ECRAN TACTILE [ par MICHELGERARD ]
Je developpe une application graphique destinée à servir de "croquis" dans les relevés topographiques et donc à fonctionner sur des tablettes graphiqu
Mon graphique s'efface dès qu'une fenetre passe dessus!! [ par joshua91 ]
Salut à tous. Comment faire pour qu'un graphique (méthode paint()) d'un Canvas soit persistant?? Mon nuage de point est comme gommé d&
affichage d'un Graphics [ par julienlll ]
je suis en train de faire un visualiseur de courbes.J'ai crée une interface et dans un de ses cadres, j'y ai mis un objet Panneau_Graphique qui extend
raffraichissement d'objet graphique sur un panel [ par MrNo ]
J'ai un probleme de rafraichisement sur une applet java aprés une action je veux redessiner des objets sous paint mais il reste des residus de ce qui
Comment dessiner un graphique avec des valeurs d'un tableau? [ par afura2004 ]
Salut, J'aimerais savoir comment je peux faire pour dessiner un graphique avec des valeurs déjà existant dans un graphique. J'ai déjà dessiné un plan
refresh java2D [ par Twinuts ]
Bonjour a tous :) ,Est il possible de repeindre une applet ou une application java2D contenant des polygones en évitant le massacre de repaint(); et
vi en java sans interface graphique [ par jules8491 ]
salut,je recherche un editeur de texte du style vi mais developpe en java et sans interface graphique, en gros les seuls classes dont j'ai acces sont
interface graphique java [ par Syl20estbon ]
Bonjour,Voila j'ai un double probleme:en fait je voudrais récuperer un evenement souris donc a l'interieur de la classe qui fait le dessin j'ai mis un
classe graphique [ par carhartt62 ]
Bonjour, Etant novice en java, je vous demande de l'aide :Mon projet de fin d'année est bassé sur une applet java qui doit envoyer et revevoir des inf
Méthode paint [ par deltiti ]
Je réalise actuellement un prog de simulation de cours de change en java pour mon mémoire. Mais je rencontre un petit problème...En effet j'utilise to
|
Téléchargements
Logiciels à télécharger sur le même thème :
|