• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Applets...pie chart from user inpiut

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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 ]
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic