I see that the link I posted does not work
/**
* File: PieBarGraph.java
* This program takes user input and creates a pie and/or bar chart
* Copyright (c) 2002-2003 Advanced Applications Total Applications Works.
* (AATAW) All Rights Reserved.
*
* AATAW grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to AATAW.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. AATAW AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL AATAW OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE,
PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or in
* the design, construction, operation or maintenance of any nuclear
* facility. Licensee represents and warrants that it will not use or
* redistribute the Software for such purposes.
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.image.*;
import java.util.*;
import java.io.*;
import DrawPanel;
public class PieBarGraph extends JApplet {
private JButton pieButton, barButton, helpButton,
clearButton, aboutButton, exitButton;
private JPanel buttonPanel;
private JPanel textPanel;
private ButtonHandler buttonHandler ;
private JTextField text1, text2, text3, text4;
private TextFieldHandler textHandler ;
private JLabel label1;
private DrawPanel drawingArea;
private Container c ;
private int width = 500, height = 400;
private JFrame app ;
private
String strVal;
public Graphics gVal;
boolean buttonPressed = false;
/** Initialization method */
public void init() {
drawingArea = new DrawPanel( width, height );
setupText() ;
setupButtons();
c = getContentPane();
c.add( textPanel, BorderLayout.NORTH );
c.add( buttonPanel, BorderLayout.SOUTH );
c.add( drawingArea, BorderLayout.CENTER );
}
/** Allocate and set up the text entry fields*/
public void setupText() {
label1 = new JLabel("Enter 4 numbers.");
textPanel = new JPanel();
text1 = new JTextField(5);
text2 = new JTextField(5);
text3 = new JTextField(5);
text4 = new JTextField(5);
textHandler = new TextFieldHandler();
textPanel.setLayout(
new GridLayout( 1, 5 ) );
textPanel.add(label1);
textPanel.add(text1);
textPanel.add(text2);
textPanel.add(text3);
textPanel.add(text4);
buttonPanel = new JPanel();
buttonPanel.setLayout(
new GridLayout( 1, 5 ) );
text1.addActionListener( textHandler );
text2.addActionListener( textHandler );
text3.addActionListener( textHandler );
text4.addActionListener( textHandler );
}
/** Allocate and set up the the JButtons */
public void setupButtons() {
buttonHandler = new ButtonHandler();
pieButton = new JButton( "Pie Chart" );
barButton = new JButton( "Bar Chart" );
helpButton = new JButton( "Help" );
aboutButton = new JButton( "About" );
helpButton.setBackground( Color.blue);
helpButton.setForeground( Color.white);
clearButton = new JButton( "Clear" );
clearButton.setBackground( Color.white);
exitButton = new JButton( "Exit" );
exitButton.setBackground( Color.red);
exitButton.setForeground( Color.white);
aboutButton.setBackground( Color.blue);
aboutButton.setForeground( Color.white);
buttonPanel.add( pieButton );
buttonPanel.add( barButton );
buttonPanel.add( helpButton );
buttonPanel.add( aboutButton );
buttonPanel.add( clearButton );
buttonPanel.add( exitButton );
pieButton.addActionListener( buttonHandler );
barButton.addActionListener( buttonHandler );
clearButton.addActionListener( buttonHandler );
helpButton.addActionListener( buttonHandler );
aboutButton.addActionListener( buttonHandler );
exitButton.addActionListener( buttonHandler );
}
/** Set the width of frame to 600 if w less than 0 */
public void setWidth( int w )
{ width = ( w >= 0 ? w : 600 ); }
/** Set the height to 600 if h less than 0 */
public void setHeight( int h )
{ height = ( h >= 0 ? h : 400 ); }
/** Make sure that the data entered is a digit */
public boolean checkDigit(String strVal) {
int strLength = 0;
boolean notDig = true;
strLength = strVal.length();
for (int ii = 0; ii < strLength; ii++) {
if (!Character.isDigit(strVal.charAt(ii)) ) {
notDig = false;
break;
}
}
return notDig;
}
/** Check if data in the text fields has changed */
public void checkFieldsChange( ActionEvent e ) {
if (!text1.getText().equals(drawingArea.strField1) ||
!text2.getText().equals(drawingArea.strField2) ||
!text3.getText().equals(drawingArea.strField3) ||
!text4.getText().equals(drawingArea.strField4) ) {
getTextValues();
}
return;
}
/** Retrieve data from the text fields */
public void getTextValues() {
drawingArea.strField1 = text1.getText() ;
drawingArea.strField2 = text2.getText() ;
drawingArea.strField3 = text3.getText() ;
drawingArea.strField4 = text4.getText() ;
drawingArea.intField1 =
Integer.parseInt(text1.getText() );
drawingArea.intField2 =
Integer.parseInt(text2.getText() );
drawingArea.intField3 =
Integer.parseInt(text3.getText() );
drawingArea.intField4 =
Integer.parseInt(text4.getText() );
return;
}
/** Ensure that the data is not null */
public boolean checkFields(
ActionEvent e, JButton choice, int i ) {
boolean checkedOK = false;
if (text1.getText().equals("") ||
text2.getText().equals("") ||
text3.getText().equals("") ||
text4.getText().equals("") ) {
drawingArea.setCurrentChoice( 3 );
drawingArea.errOne = true;
}
else if( !checkDigit(text1.getText()) ||
!checkDigit(text2.getText()) ||
!checkDigit(text3.getText()) ||
!checkDigit(text4.getText()) ) {
drawingArea.setCurrentChoice( 3 );
drawingArea.errTwo = true;
}
else {
getTextValues();
checkedOK = true;
if (!buttonPressed)
drawingArea.OKOne = true;
}
return checkedOK;
}
/** */
public void sysExit( int a ) {
System.exit( a );
}
/** Main entry point */
public static void main( String args[] )
{
int width = 790, height = 550;
Graphics gVal2;
// create window in which
applet will execute
JFrame appMain =
new JFrame( "Pie and Bar Chart" );
appMain.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
/** create one applet instance */
PieBarGraph appBar = new PieBarGraph();
appBar.setWidth( width );
appBar.setHeight( height );
/** call applet's init and start methods */
appBar.init();
appBar.start();
/** attach applet to center of window */
appMain.getContentPane().add( appBar );
/** set the window's size */
appMain.setSize( appBar.width, appBar.height );
/**
* showing the window causes all GUI components
* attached to the window to be painted
*/
appMain.show();
}
/** ButtonHandler is the event handler */
private class ButtonHandler implements ActionListener {
public void actionPerformed( ActionEvent e )
{
buttonPressed = true;
if ( e.getSource() == pieButton ) {
if ( checkFields( e, pieButton, 0 ) )
checkFieldsChange( e );
drawingArea.setCurrentChoice( 0 );
}
else if ( e.getSource() == barButton ) {
if ( checkFields( e, barButton, 1 ) )
checkFieldsChange( e );
drawingArea.setCurrentChoice( 1 );
}
else if ( e.getSource() == helpButton ) {
if ( checkFields( e, helpButton, 3 ) )
checkFieldsChange( e );
drawingArea.setCurrentChoice( 3 );
}
else if ( e.getSource() == aboutButton ) {
Runtime rt = Runtime.getRuntime();
String[] callAndArgs = { "c:\\Program Files\\Internet Explorer\\IExplore.exe" ,
"http://sumtotalz.com/TotalAppsWorks/index.html " };
try {
Process child = rt.exec(callAndArgs);
child.waitFor();
//MyPrint ("Process exit code is: " +
//child.exitValue());
}
catch(IOException e2) {
//MyPrint( "IOException starting process!");
}
catch(InterruptedException e3) {
//MyPrint( "Interrupted waiting for process!");
}
drawingArea.setCurrentChoice( 4 );
}
else if ( e.getSource() == clearButton ) {
if ( checkFields( e, clearButton, 4 ) )
checkFieldsChange( e );
drawingArea.setCurrentChoice( 4 );
}
else if ( e.getSource() == exitButton ) {
sysExit( 0 );
}
}
}
/** TextFieldHandler is the text field event handler */
private class TextFieldHandler implements ActionListener {
public void actionPerformed( ActionEvent e )
{
int i = 0;
buttonPressed = false;
drawingArea.setCurrentChoice( 3 );
checkFields( e, helpButton, i );
}
}
}
**************************************************************************
/** **********************************************
* File: DrawPanel.java
* This file extends the JPanel class.
*
* Copyright (c) 2002-2003 Advanced Applications Total Applications Works.
* (AATAW) All Rights Reserved.
*
* AATAW grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to AATAW.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. AATAW AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL AATAW OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or in
* the design, construction, operation or maintenance of any nuclear
* facility. Licensee represents and warrants that it will not use or
* redistribute the Software for such purposes.
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.image.*;
import java.awt.print.*;
import java.util.*;
import java.lang.Math;
/**
* subclass of JPanel to allow drawing in a separate area
*/
class DrawPanel extends JPanel {
private int currentChoice = -1; // don't draw first time
private int width = 100, height = 100, prevChoice = -1 ;;
private boolean initPass = true;
public boolean errOne = false;
public boolean errTwo = false, OKOne = false;
private Graphics gVal;
private JFrame myFrame ;
private PrinterJob job ;
public int intField1, intField2, intField3, intField4;
public String strField1, strField2, strField3, strField4;
public DrawPanel( int w, int h ) {
width = ( w >= 0 ? w : 100 );
height = ( h >= 0 ? h : 100 );
}
public void drawFont( Graphics g) {
/** set current font to Serif (Times), bold, 12pt
* and draw a string */
g.setFont( new Font( "Serif", Font.BOLD, 16 ) );
g.drawString( "Please follow the instructions below.", 20, 50 );
/** set current font to Monospaced (Courier),
* italic, 24pt and draw a string */
g.setFont( new Font( "Monospaced", Font.ITALIC, 16 ) );
g.drawString( "Enter a number in each entry field above and then press a chart button.", 20, 70 );
/** set current font to SansSerif (Helvetica),
* plain, 14pt and draw a string */
g.setFont( new Font( "SansSerif", Font.PLAIN, 14 ) );
g.drawString( "To repeat these instructions, press the help button below.", 20, 90 );
/** set current font to Serif (times), bold/italic,
* 18pt and draw a string */
g.setColor( Color.red );
g.setFont(
new Font( "", Font.BOLD + Font.ITALIC, 18 ) );
g.drawString( "These instructions will disappear " +
"when you request a chart. ", 20, 110 );
}
/** drawBar uses the user input to draw the bar chart */
public void drawBar( Graphics g) {
if ( ( intField1 >= 0 ) && ( intField2 >= 0 ) &&
( intField3 >= 0 ) && ( intField4 >= 0 ) ) {
/** create 2D by casting g to Graphics2D */
Graphics2D g2d = ( Graphics2D ) g;
/** Draw the x-coordinate line */
g.setColor(Color.black);
g.drawLine ( 5, 406, 450, 406);
/** draw 2D Bars in red */
g2d.setColor( Color.red );
g2d.setStroke( new BasicStroke( 10.0f ) );
g2d.draw(
new Rectangle2D.Double( 50, 400 - intField1, 5,
intField1 ) );
g.drawString("Red ", 45,
400 - intField1 - 40);
g.drawString("" + intField1, 45,
400 - intField1 - 20);
g2d.setColor( Color.green );
g2d.setStroke( new BasicStroke( 10.0f ) );
g2d.draw(
new Rectangle2D.Double( 100, 400 - intField2,
5, intField2) );
g.drawString( "Green ", 90,
400 - intField2 - 40);
g.drawString( "" + intField2, 90,
400 - intField2 - 20);
g2d.setColor( Color.yellow );
g2d.setStroke( new BasicStroke( 10.0f ) );
g2d.draw(
new Rectangle2D.Double( 150, 400 - intField3,
5, intField3 ) );
g.drawString("Yellow ", 140,
400 - intField3 - 40);
g.drawString("" + intField3, 140,
400 - intField3 - 20);
g2d.setColor( Color.blue );
g2d.setStroke( new BasicStroke( 10.0f ) );
g2d.draw(
new Rectangle2D.Double( 200, 400 - intField4,
5, intField4 ) );
g.drawString("Blue " , 190,
400 - intField4 - 40);
g.drawString("" + intField4 , 190,
400 - intField4 - 20);
}
}
/** drawPie uses the user input to draw the pie chart */
public void drawPie( Graphics g) {
double floatVal[] = {0.0, 0.0, 0.0, 0.0};
double totVal = 0.0;
Font font ;
/** create 2D by casting g to Graphics2D */
Graphics2D g2d = ( Graphics2D ) g;
totVal = (double) intField1 + + intField2 + intField3 + intField4 ;
floatVal[0] = (double) (intField1 / totVal);
floatVal[1] = (double) (intField2 / totVal);
floatVal[2] = (double) (intField3 / totVal);
floatVal[3] = (double) (intField4 / totVal);
Arrays.sort (floatVal);
/** draw 2D pie-shaped arc in white */
g2d.setPaint( Color.white );
g2d.setStroke( new BasicStroke( 6.0f ) );
g2d.fillArc( 50, 80, 300, 300, 0, (int) ( floatVal[ 3 ] * 360 ) );
g2d.fillRect( 440, 80, 40, 20 ) ;
g.setFont(
new Font( "", Font.BOLD + Font.ITALIC, 18 ) );
g2d.setPaint( Color.black );
g.drawString("- " + (java.lang.Math.round( floatVal[ 3 ] * 100.0 )) + " %",
490, 95);
g2d.setPaint( Color.green );
g2d.fillArc( 50, 80, 300, 300, (int) ( floatVal[ 3 ] * 360 ),
( int )( floatVal[ 2 ] * 360 ) );
g2d.fillRect( 440, 120, 40, 20 ) ;
g.setFont(
new Font( "", Font.BOLD + Font.ITALIC, 18 ) );
g2d.setPaint( Color.black );
g.drawString("- " + ( java.lang.Math.round( floatVal[ 2 ] * 100)) + " %",
490, 135);
g2d.setPaint( Color.blue );
g2d.fillArc( 50, 80, 300, 300,
(int)(floatVal[ 3 ] * 360) + (int)( floatVal[ 2 ] * 360 ) ,
(int)(floatVal[ 1 ] * 360 ) );
g2d.fillRect( 440, 160, 40, 20 ) ;
g.setFont(
new Font( "", Font.BOLD + Font.ITALIC, 18 ) );
g2d.setPaint( Color.black );
g.drawString("- " + ( java.lang.Math.round( floatVal[ 1 ] * 100)) + " %",
490, 175);
g2d.setPaint( Color.black );
g2d.fillArc( 50, 80, 300, 300,
(int)(floatVal[ 3 ] * 360) + (int)( floatVal[ 2 ] * 360 ) +
(int)( floatVal[ 1 ] * 360) , (int) (floatVal[ 0 ] * 360 ) + 2 ) ;
g2d.fillRect( 440, 190, 40, 20 ) ;
g.setFont(
new Font( "", Font.BOLD + Font.ITALIC, 18 ) );
g2d.setPaint( Color.black );
g.drawString("- " + ( java.lang.Math.round( floatVal[ 0 ] * 100)) + " %",
490, 205);
g2d.setStroke( new BasicStroke( 1.0f ) );
g.drawArc( 50, 80, 300, 300, 0, 360 );
return;
}
/** */
public void paintComponent( Graphics g ) {
super.paintComponent( g );
/** if this the initial pass, display the help instructions. */
if ( initPass ) {
initPass = false;
setGraphicsVal(g);
currentChoice = 3;
}
/** Display th e help instructions */
if ( currentChoice == 3 ) {
drawFont(g);
}
/** Valid numbers were not entered */
else if (errOne) {
errOne = false;
g.setFont(new Font("Serif", Font.BOLD, 24) );
g.setColor(Color.red);
g.drawString(
"Please enter a valid number in each of the fields above.", 30, 50 );
}
/** Valid numbers were not entered */
else if (errTwo) {
errTwo = false;
g.setFont(new Font("Serif", Font.BOLD, 24) );
g.setColor(Color.red);
g.drawString(
"Please enter a valid number in each of the fields above.", 30, 50 );
g.drawString(
"Alphbet letters and special characters are not numbers/digits.", 30, 90 );
}
/** Valid numbers were entered but a button was not
* pressed */
else if (OKOne) {
OKOne = false;
g.setFont(new Font("Serif", Font.BOLD, 24) );
g.setColor(Color.blue);
g.drawString(
"Please press the button for the chart you wish.", 30, 50 );
}
/** Process the data using the pressed button */
else {
switch( currentChoice ) {
case 0:
drawPie(g);
prevChoice = 0 ;
break;
case 1:
drawBar(g);
prevChoice = 1 ;
break;
case 2:
;
break;
case 3:
drawFont(g);
prevChoice = 3 ;
break;
case 4:
setCurrentChoice(-1);
prevChoice = -1 ;
break;
case 5:
;
break;
}
}
}
/** */
public void setCurrentChoice( int c ) {
currentChoice = c;
repaint();
}
/** */
public void setGraphicsVal( Graphics g ) {
gVal = g;
}
/** */
public void setFrameAddress( JFrame myFrame_1 ) {
myFrame = myFrame_1 ;
System.out.println("setFrameAddress(): myFrame_1 is " + myFrame_1 +
"\n myFrame is " + myFrame);
}
}