📄 cw2c.java.org
字号:
/**
* 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
{
String title;
public MainWindow()
{
setLayout(null);
setSize(400,400);
setBackground(Color.gray);
setTitle("Pie Chart");
doStuff();
} //end of mainwindow
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;
Integer myInteger;
double datavalue;
//instantiate RandomAeccessFile. myData file is opened for reading
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
// use the readLine method to read a line into the string line
//until the end of the file
while((line=inFile.readLine())!=null)
{
if( lineNo == 0 )
{
title=line;
}// store the first line into title
else
{
myInteger=Integer.valueOf(line); //convert string to Integer
datavalue=myInteger.doubleValue(); //get the int value of data
sum=sum+datavalue; //do sum calculation
data.addElement(myInteger); //cast Integer to vector object
}// store the rest of file into vector data
lineNo++; //track line number to seperate the title input from vector input
}//end of while loop
}// end of try
catch (IOException e)
{
System.out.println("An i/o error has occured ["+e+"]");
}// end of catch exception
System.out.println("Title:"+" "+title);// print out title
System.out.println(" "+"Data"+" "+"Angle(degree)");//print out line of text
// instatiate new vector angle and delacare new variables
Integer tmpValue; // store temporary int variable
double value; // primitive int value
double angleDouble; // primitive anlge int value
int angleInt;
Double myD;
Vector angle=new Vector(); // vector to contain angle
for(int n=0;n<data.size();n++)
{
tmpValue=(Integer)data.elementAt(n); //cast object from vector to Integer
value=tmpValue.doubleValue(); //convert Integer to int
angleDouble=Math.round(360*value/sum); //caculate the angle
myD=new Double(angleDouble);
angleInt=myD.intValue();
angle.addElement(new Integer(angleInt));/*convert int angle to Integer
cast Integer to vector object*/
}// end of for loop
//print out data in two vectors
for(int n=0;n<angle.size();n++)
{
System.out.println(" "+data.elementAt(n)+" "+angle.elementAt(n));
}//end of for loop
}
public void paint(Graphics g)
{
super.paint(g);
// draw a circle
// draw a yellow circle
g.setColor(Color.yellow);
g.drawOval(100,60,100,100); // NB draw two concentric circles (to make it thick)
g.drawOval(101,61,98,98);
// 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);
}//end of paint
}//end of mainwindow class
class cw2c
{
public static void main(String args[])
{
MainWindow myWindow=new MainWindow();
myWindow.show();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -