import java.applet.Applet;
import java.awt.*;
import java.awt.geom.*;
import java.util.*;

public class BarChart extends Applet {   
	
	//costanti tipiche del grafico: numero di campioni e distanza tra gruppi di campioni
	public	final	int		NUMSAMPLES= 25;
	public	final	int		SPACE=5;
		
	//variabile per dimensionare il disegno
	private	double	MAXX;
    private	double	MAXY;
    private	double	BARWIDTH;
	private	double	X0;
	private	double	Y0;
	private	int		MAXELEM;
	
	//array per contenere le barre da graficare
	private	Rectangle2D.Double[] 	set1= new Rectangle2D.Double[NUMSAMPLES];
	private	Rectangle2D.Double[] 	set2= new Rectangle2D.Double[NUMSAMPLES];
	private	Rectangle2D.Double[] 	set3= new Rectangle2D.Double[NUMSAMPLES];
	
	public void paint(Graphics g)    { 
  		Graphics2D g2=(Graphics2D)g;
  		MAXX=getWidth();
        MAXY=getHeight();
        BARWIDTH=(MAXX-20-SPACE*(NUMSAMPLES-1))/3/NUMSAMPLES;
		X0=10;
		Y0=MAXY-10;
		MAXELEM=(int)(MAXY-20);
		Rectangle2D.Double frame=new Rectangle2D.Double(X0,Y0-MAXELEM,MAXX-20,MAXY-20);
        
        CreateRandomSets(set1, set2, set3);        
		g2.draw(frame);
		DrawSet(set1,Color.blue, g2);
		DrawSet(set2,Color.red, g2);
		DrawSet(set3,Color.black, g2);
	}
 	
 	//funzione ausiliaria di paint per creare 3 insiemi di dati
 	private void CreateRandomSets(Rectangle2D.Double[] s1, Rectangle2D.Double[] s2, Rectangle2D.Double[] s3)    { 
 		
 		Random generator=new Random();         
        int 	randomSample;
        
        for(int i=0; i<NUMSAMPLES; i++)	{
			randomSample=generator.nextInt(MAXELEM);
			s1[i]=new Rectangle2D.Double(X0+i*(SPACE+3*BARWIDTH), Y0-randomSample,BARWIDTH,randomSample);
			randomSample=generator.nextInt(MAXELEM);
			s2[i]=new Rectangle2D.Double(X0+BARWIDTH+i*(SPACE+3*BARWIDTH), Y0-randomSample,BARWIDTH,randomSample);
			randomSample=generator.nextInt(MAXELEM);
			s3[i]=new Rectangle2D.Double(X0+2*BARWIDTH+i*(SPACE+3*BARWIDTH), Y0-randomSample,BARWIDTH,randomSample);
		} 
	}
	
	//funzione ausiliaria di paint per disegnare un insieme di dati con un colore specifico
	private void DrawSet(Rectangle2D.Double[] set, Color col,Graphics2D graph)	{
		graph.setColor(col);
		for(int i=0; i<NUMSAMPLES; i++)				
			graph.fill(set[i]);	
	}
	
}
        

 


Bacheca di Informatica Grafica

Forum studenti:

by FdA
ultima modifica: 09/02/2004 13.23