📄 cw2c.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;
/*------------------------------------------------------------------------
*mainwindow method to set up layout,size and backgroud of screen
*constructor doStuff method
*--------------------------------------------------------------------------*/
public MainWindow()
{
setLayout(null);
setSize(400,400);
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;
//instantiate RandomAeccessFile. myData file is opened for reading
try
{
RandomAccessFile inFile=new RandomAccessFile(filename,"r");
String line;
int lineNo=0;
while((line=inFile.readLine())!=null)
{
if( lineNo == 0 )
{
title=line;
}
else
{
//trim leading spaces from line
String newline=line.trim();
myDouble=Double.valueOf(line);
datavalue=myDouble.doubleValue();
sum=sum+datavalue;
data.addElement(myDouble);
}
lineNo++;
}
}
catch (IOException e)
{
System.out.println("An i/o error has occured ["+e+"]");
}// end of catch exception
//print out title and line of words
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;
Vector angle=new Vector();
//calculate the angle and wrap into the 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));
}
//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);
// 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.blue);
g.setFont(new Font("SansSerif", Font.BOLD, 16));
g.drawString(title,105,45);
}
}//end of mainwindow class
/*---------------------------------------------------------------------------------
*main program
*call Mainwindow class
*instantiate the mainwindow class
*-----------------------------------------------------------------------------------*/
class cw2c
{
public static void main(String args[])
{
MainWindow myWindow=new MainWindow();
myWindow.show();
}
}//end of main class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -