File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Applets...pie chart from user inpiut Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Applets...pie chart from user inpiut" Watch "Applets...pie chart from user inpiut" New topic
Author

Applets...pie chart from user inpiut

Bradley Jordan
Ranch Hand

Joined: Feb 19, 2007
Posts: 42
[code]
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyApplet extends JApplet implements ActionListener
{

private JLabel greeting = new JLabel("Investment Analysis");
private JLabel info = new JLabel("Enter dollars amounts for your investments portfolio: ");
private JLabel hrisk = new JLabel("High Risk");
private JTextField field1 = new JTextField(12);
private JLabel mrisk = new JLabel("Medium Risk");
private JTextField field2 = new JTextField(12);
private JLabel lrisk = new JLabel("Low Risk");
private JTextField field3 = new JTextField(12);
private JLabel nrisk = new JLabel("No Risk");
private JTextField field4 = new JTextField(12);
private JLabel instructions = new JLabel("Press Enter from the text box.");



public void init ()
{
getContentPane().setLayout(new FlowLayout());

greeting.setFont(new Font("Helvetica", Font.BOLD | Font.ITALIC,36));
getContentPane().add(greeting);

info.setFont(new Font("Arial", Font.BOLD,15));
getContentPane().add(info);

hrisk.setFont(new Font("Arial", Font.BOLD,10));
getContentPane().add(hrisk);

field1.setFont(new Font("Arial", Font.BOLD,10));
field1.setBackground(Color.CYAN);
getContentPane().add(field1);

mrisk.setFont(new Font("Arial", Font.BOLD,10));
getContentPane().add(mrisk);

field2.setFont(new Font("Arial", Font.BOLD,10));
field2.setBackground(Color.PINK);
getContentPane().add(field2);

lrisk.setFont(new Font("Arial", Font.BOLD,10));
getContentPane().add(lrisk);

field3.setFont(new Font("Arial", Font.BOLD,10));
field3.setBackground(Color.ORANGE);
getContentPane().add(field3);

nrisk.setFont(new Font("Arial", Font.BOLD,10));
getContentPane().add(nrisk);

field4.setFont(new Font("Arial", Font.BOLD,10));
field4.setBackground(Color.MAGENTA);
field4.addActionListener(this);
getContentPane().add(field4);

instructions.setFont(new Font("Arial", Font.BOLD, 24));
getContentPane().add(instructions);
}

public void actionPerformed (ActionEvent event)
{
double aConvetered1 = Double.parseDouble(field1.getText());
double aConvetered2 = Double.parseDouble(field2.getText());
double aConvetered3 = Double.parseDouble(field3.getText());
double aConvetered4 = Double.parseDouble(field4.getText());
double total = aConvetered1 + aConvetered2 + aConvetered3 + aConvetered4;
double a = aConvetered1/total;
double b = aConvetered2/total;
double c = aConvetered3/total;
double d = aConvetered4/total;
double aDegree = a * 360;
double bDegree = b * 360;
double cDegree = c * 360;
double dDegree = d * 360;
System.out.println(aDegree);
System.out.println(bDegree);
System.out.println(cDegree);
System.out.println(dDegree);

}
public void paint (Graphics g)
{
super.paint(g);

g.setColor(Color.CYAN);
g.fillArc(200, 250, 100, 100, 100, aDegree);
g.setColor(Color.PINK);
g.fillArc(200, 250, 100, 100, 100, 216);
g.setColor(Color.ORANGE);
g.fillArc(200, 250, 100, 100, 100, 108);
g.setColor(Color.MAGENTA);
g.fillArc(200, 250, 100, 100, 100, 36);

repaint();
}
}

I am trying to create an applet that takes input from the form and then turns into a pie chart. I am able to convert the strings to numbers but I can;t the arc to reconigize the varaible to set the angle at. Any Suggestions? I would greatly appreaite any help. I know the fourm won;t give me the answer but shoudl point me in the right direction.

Thanks,
Brad
[ March 26, 2007: Message edited by: Bradley Jordan ]
Liam Tiarnach
Ranch Hand

Joined: Aug 06, 2004
Posts: 51
The problem you are encounter is a variable scope issue.

You declared local variables in your actionPerformed() method but are trying to access them in your paint() method which is why you are getting compiling problems.


- Liam...<br />- ' He who never sleeps... '
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Applets...pie chart from user inpiut