📄 cw2d.java.bak
字号:
/**
* 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;
Vector angle;
public MainWindow()
{
System.out.println("Entered MainWindow");
setLayout(null);
setSize(300,300);
setBackground(Color.gray);
setTitle("Pie Chart");
angle = new Vector();
doStuff();
} //end of mainwindow
private 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
*convert string line into Integer and cast Integer to vector object
* use lineNo to track line number for seperate the title input from vector input
*catch I/o error
---------------------------------------------------------------*/
while((line=inFile.readLine())!=null)
{
if( lineNo == 0 )
{
title=line;
}
else
{
myInteger=Integer.valueOf(line);
datavalue=myInteger.doubleValue();
sum=sum+datavalue;
data.addElement(myInteger);
}
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
System.out.println("Title:"+" "+title);
System.out.println(" "+"Data"+" "+"Angle(degree)"); //print out line of text
/*---------------------------------------------------------------------------------
* instatiate new vector angle and delacare new variables
* get number out of the vector data
* convert int number to double for calculation
* cast the result double number to Double and put into vector angle
---------------------------------------------------------------------------------*/
Integer tmpValue;
double value;
double angleDouble;
int angleInt;
Double myD;
for(int n=0;n<data.size();n++)
{
tmpValue=(Integer)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(" "+data.elementAt(n)+" "+angle.elementAt(n));
}//end of for loop
}// end of doStuff
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);
/*---------------------------------------------------------------------
*fill circle according to angle
split the circle and fill the color
*use for loop to change startArc and arcAngle
*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
class cw2d
{
public static void main(String args[])
{
MainWindow myWindow=new MainWindow();
myWindow.show();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -