?? cw2d.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();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -