Advertisement
|
This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
pie
Posted by Tom on December 16, 2001 at 7:44 AM
if you still need a pie chart applet heres one i worte also does bar chrat., doesn't use swing so it'll work in IE 5 there are not many notes so if you need ant explaining send me an e-mail there are 3 classes you need and a Msg box class for error handling. **************************************************************** import java.awt.*; import java.applet.*; import java.awt.event.*;
public class Main extends Applet implements ActionListener{ private Data data = new Data(); private Chart chart = new Chart(data); private Msg msg = new Msg(); private Image toMem;//the 'type' for the off screen buffer private Button ok ;//ok button for Msg Box private Button clear;//reset button for charts private Image Matt;//picy of Matt on Msg Box private TextField value ;//text Box for input public void init() { setLayout(null) ; Matt = getImage ( getDocumentBase(), "matt.jpg"); value = new TextField(10) ; value.setBounds(360,40,70,20); add(value) ; value.addActionListener(this); ok = new Button("Ok") ; ok.setBounds(315,400,70,20); add(ok) ; ok.setVisible(false); ok.addActionListener(new ActionListener (){//what happens when ok button press public void actionPerformed(ActionEvent e) { data.state1=false; ok.setVisible(false); value.setVisible(true); clear.setVisible(true); repaint(); } }); clear=new Button("Clear Charts"); clear.setBounds(45,350,70,20); add(clear); clear.addActionListener(new ActionListener (){//whats happens when clear button press public void actionPerformed(ActionEvent e) { for(int i=0; i data.fruit[i]=0; data.big=0; } } }); } public void update(Graphics g){ //written by Wayne McKenzie //make the off screen buffer the same size as the applet toMem = createImage(700,700); //use the graphics object of the image object Graphics gos = toMem.getGraphics(); //call the paint method below and draw to the //off screen buffer(toMem) using it's graphics object(gos) paint(gos); //dump the contents of the off screen buffer //to the screen g.drawImage(toMem,0,0,null); } public void paint(Graphics g) { setBackground(Color.white); if (data.state1==false){//valid input data.display(g) ; chart.display(g) ; repaint(); } else{//invalid input msg.display(g); ok.setVisible(true); value.setVisible(false); clear.setVisible(false); g.drawImage( Matt, 420,340,this); repaint(); } } public void actionPerformed(ActionEvent event) { String newValue = (value.getText()) ; data.setValue(newValue) ; data.enter(); repaint() ; } } **************************************************************** import java.awt.*; public class Data { public int big=0;//highest number ofvalues private int yaxis=200;// y axis of graph private int yunit=0;// size of units for y axis public boolean state1=false;//used for msg box when invalid input is entered public int[] fruit = new int[6] ;//array that save everything public String newValue ; // value to be transferred to array private String value;//input from user // Set the new value to update the array public void setValue(String value) { newValue = value; } public void enter (){ if (newValue.equalsIgnoreCase("APPLES")){ fruit[0]++; } else if (newValue.equalsIgnoreCase("PEARS")){ fruit[1]++; } else if (newValue.equalsIgnoreCase("PINEAPPLES")){ fruit[2]++; } else if (newValue.equalsIgnoreCase("BANANAS")){ fruit[3]++; } else if (newValue.equalsIgnoreCase("GRAPES")){ fruit[4]++; } else if (newValue.equalsIgnoreCase("RHUBARB")){ fruit[5]++; } else { state1=true; } } //find the highest value public void high (){ for(int i=0; i if (big <=fruit[i]){ big=fruit[i]; } } } //finds the sum public double addValue() { double sum = 0; for(int i=0 ; i sum = sum + (double)(fruit[i]); } return sum ; } //units to be used when drawing the chart public int unit(){ try{ yunit=(yaxis / big); } catch(ArithmeticException e){} return yunit; } public void display(Graphics g) { Font font = new Font("ComicSansMs",Font.BOLD,14); g.setFont(font); g.drawString("Enter You Favourite fruit from the list below",45,55); g.drawString("APPLES = "+fruit[0],45,90); g.drawString("PEARS = "+fruit[1],45,110); g.drawString("PINEAPPLES = "+fruit[2],45,130); g.drawString("BANANAS = "+fruit[3],45,150); g.drawString("GRAPES = "+fruit[4],45,170); g.drawString("RHUBARB = "+fruit[5],45,190); } }**************************************************************** import java.awt.*; public class Chart { private Data data; public Chart(Data indata){ data = indata; } public void display(Graphics g) { data.high(); data.addValue(); g.setColor(Color.black); g.fillRect(275,77,3,206); //y axis g.fillRect(270,275,305,3); //x axis //x axis dividers g.fillRect(325,275,3,8); g.fillRect(375,275,3,8); g.fillRect(425,275,3,8); g.fillRect(475,275,3,8); g.fillRect(525,275,3,8); g.fillRect(575,275,3,8); //x axis lables Font font = new Font("ComicSansMs",Font.BOLD,8); g.setFont(font); g.drawString("APPLES",280,290); g.drawString("PEARS",330,290); g.drawString("PINE-",380,290); g.drawString("APPLES",380,298); g.drawString("BANANAS",430,290); g.drawString("GRAPES",480,290); g.drawString("RHUBARB",530,290); //y axis dividers for(int i=0; i g.fillRect(270,275-(data.unit()*i),5,3); //y axis lables Font font1 = new Font("ComicSansMs",Font.BOLD,10); g.setFont(font1); g.drawString(""+data.big,260,77); g.drawString(""+i,260,275-(data.unit()*i)); } //Both charts double percent; int slice=0; double angle =3.6; try{ percent = 100/data.addValue()* angle; slice=((int)(percent)); } catch(ArithmeticException e){} //graph * 2 for apples g.setColor(Color.red); g.fillRect((278),275-(data.unit()*data.fruit[0]),47,(data.unit()*data.fruit[0])); g.fillArc(200,350,300,300,90,-(data.fruit[0]*slice)); //pears g.setColor(Color.yellow); g.fillRect((328),275-(data.unit()*data.fruit[1]),47,(data.unit()*data.fruit[1])); g.fillArc(200,350,300,300,(90-(data.fruit[0]*slice)),-(data.fruit[1]*slice)); //Pineapples g.setColor(Color.green); g.fillRect((378),275-(data.unit()*data.fruit[2]),47,(data.unit()*data.fruit[2])); g.fillArc(200,350,300,300,(90-(data.fruit[0]*slice)-(data.fruit[1]*slice)),-(data.fruit[2]*slice)); //bananas g.setColor(Color.cyan); g.fillRect((428),275-(data.unit()*data.fruit[3]),47,(data.unit()*data.fruit[3])); g.fillArc(200,350,300,300,(90-(data.fruit[0]*slice)-(data.fruit[1]*slice)-(data.fruit[2]*slice)),-(data.fruit[3]*slice)); //grapes g.setColor(Color.blue); g.fillRect((478),275-(data.unit()*data.fruit[4]),47,(data.unit()*data.fruit[4])); g.fillArc(200,350,300,300,(90-(data.fruit[0]*slice)-(data.fruit[1]*slice)-(data.fruit[2]*slice)-(data.fruit[3]*slice)), -(data.fruit[4]*slice)); //rhubarb g.setColor(Color.magenta); g.fillRect((528),275-(data.unit()*data.fruit[5]),47,(data.unit()*data.fruit[5])); g.fillArc(200,350,300,300,(90-(data.fruit[0]*slice)-(data.fruit[1]*slice)-(data.fruit[2]*slice)-(data.fruit[3]*slice) -(data.fruit[4]*slice)),-(data.fruit[5]*slice)); } }***************************************************************** import java.awt.*; public class Msg { public void display(Graphics g){ g.setColor(Color.gray); g.fillRect(198,298,354,154); g.setColor(Color.lightGray); g.fillRect(200,300,350,150); g.setColor(Color.blue); g.fillRect(205,305,340,25); g.setColor(Color.black); Font font = new Font("ComicSansMs",Font.PLAIN,14); g.setFont(font); g.drawString("ERROR",210,325); g.drawString("That is not a valid entry! ",215,350); } } ****************************************************************
**************************************************************** you'll need a picture in the classes for the Msg box good luck!
Replies:
|