⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cw2d.java

📁 samll awt programs for the beginner.
💻 JAVA
字号:
/**
* Author: Jing Han
* Date; 12/11/03
* Module: G6DICp
* Title: ICP Coursework 2
* SCSiT Username: jxh23m
*/


import java.io.*;
import java.awt.*;
import java.util.*;


class MainWindow extends Frame
{
    //declare class variable
    String title;
    Vector angle;
    
   /*----------------------------------------------------------------------------
    *mainwindow method to set up layout,size and backgroud of screen
    *constructor doStuff method
    *-----------------------------------------------------------------------------*/
    public MainWindow()
    {
      	setLayout(null);
     	setSize(300,300);
     	setBackground(Color.gray);
     	setTitle("Pie Chart");
     	doStuff();
		
     } //end of mainwindow   

   /*-------------------------------------------------------------------------------------
    *doStuff method: read data from file, calculate the angle and store angle into vector
    *print out angle and data in the file
    *------------------------------------------------------------------------------------*/
    public void doStuff()
    {
        //prompt for filename
        System.out.print("Enter data file:"+"  ");
        String filename=TButils.readln();
        
        //declare varialbe title and instantiate vector
        title = "";
        Vector data=new Vector();
        double sum=0;
        Double myDouble;
        double datavalue; 
	
  	 //readLine method to read file and store data into vector    
        try 
        {
            RandomAccessFile inFile=new RandomAccessFile(filename,"r");
            String line; //a string to contain the line read
            int lineNo=0;//initialise the line number of file
         
            while((line=inFile.readLine())!=null) 
            {
               
                if( lineNo == 0 )
                {
                    title=line;
                 }
                 
                 else
                 {
                 		//trim leading space
                        String newline=line.trim();       
						myDouble=Double.valueOf(line);    
                        datavalue=myDouble.doubleValue();   
                        sum=sum+datavalue;        
                    	data.addElement(myDouble); 
                  }
          
            
                lineNo++; 
       
            }//end of while loop 
         }// end of try 
         catch (IOException e)
         {
            System.out.println("An i/o error has occured ["+e+"]");
         }// end of catch exception

      
         //print out title and line of text
         System.out.println("Title:"+"  "+title);			     	 	 
         System.out.println("     "+"Data"+"     "+"Angle(degree)");	

       /*--------------------------------------------------------------------------------
        *  instatiate new vector angle and delacare new variables
        * get number out of the vector data 
        * convert int number to double for calculation angle
        * cast the result double angle to Double and put into vector angle
        ---------------------------------------------------------------------------------*/
        Double tmpValue;     		  
 		double value;           	  
		double angleDouble;      	  
		int angleInt;
		Double myD;
		angle = new Vector(); 	 
	
        for(int n=0;n<data.size();n++)
        {
			tmpValue=(Double)data.elementAt(n);	
        	value=tmpValue.doubleValue();				
         	angleDouble=Math.round(360*value/sum);	
         	myD=new Double(angleDouble);
			angleInt=myD.intValue();
	        angle.addElement(new Integer(angleInt));	
                 
         
         }// end of for loop 
     
         //print out data in two vectors
     	for(int n=0;n<angle.size();n++)
     	{
        	System.out.println("      "+((Double)data.elementAt(n)).intValue()+"       "+angle.elementAt(n));
     	}//end of for loop 
     	
			   	
    }// end of doStuff

    
    /*-----------------------------------------------------------------------
     *override the paint method and name as super.paint
     *-----------------------------------------------------------------------*/
    public void paint(Graphics g)
    {
    	
        super.paint(g);
        
        
        // write the message in white SansSerif bold 16
        g.setColor(Color.white);            
        g.setFont(new Font("SansSerif", Font.BOLD, 16));
        g.drawString(title,105,45);
        
   /*---------------------------------------------------------------------
	 *split circle according to angle 
	 *fill the color using g.fillArc method
	 *use for loop to change startArc and arcAngle
	 *use Math.random method to get random color int number
	 *---------------------------------------------------------------------*/
        int startAngle=0;
        int tmpArc;
        int a;
        int b;
        int c;
        Double tempA;
        Double tempB;
        Double tempC;
        
        
        for(int n=0;n<angle.size();n++)
        {
		 	tempA = new Double(Math.random()*255);
         	 a = tempA.intValue();
        	 tempB = new Double(Math.random()*255);
        	 b = tempB.intValue();
        	 tempC = new Double(Math.random()*255);
         	 c = tempC.intValue();
           	tmpArc=((Integer)angle.elementAt(n)).intValue();
        	g.setColor(new Color(a,b,c));
        	g.fillArc(100,60,100,100,startAngle,tmpArc);
           	startAngle=tmpArc+startAngle;
            
        }
        
    }//end of paint
    
    
}//end of mainwindow class

/*-----------------------------------------------------------------------
  *main program
  *call Mainwindow class
  *instantiate the mainwindow class 
*-------------------------------------------------------------------------*/
class cw2d
{
    public static void main(String args[])
    {
    	MainWindow myWindow=new MainWindow();
     	myWindow.show();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -