9 Replies - 5083 Views - Last Post: 09 May 2011 - 04:14 PM Rate Topic: -----

#1 custurd122000  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 139
  • Joined: 05-March 09

pie chart applet

Posted 09 May 2011 - 03:04 PM

Write an applet that displays a pie chart representing four categories of investment risks. Supply four labled TextFields in which the user can enter dollars values for his or her high risk, medium risk, low risk, and no risk investments. When the user presses the Enter key from within any Textfield, display a pie chart that show the relative size of each investment category. Use a different color for each slice of the pie. Use legends to describe the color, text to show the percentage and the chart title. On the applet, place all the inputs on the right hand side and the output on the left hand side
Hint: To determine the size of any pie slice, you must first determine its percentage of the total. Then use that percentage to determine how many of the 360 available degrees you will allot to that pie slice.


The applet appears with a blinking pie chart
heres the code
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Applet extends JApplet implements ActionListener
{
 
private static final long serialVersionUID = 1L;
private JLabel greeting = new JLabel("Investment Analysis");
private JLabel info = new JLabel("Enter dollars amounts : ");
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 JButton instructions = new JButton("Enter");
 
 
public void init ()
{
getContentPane().setLayout(new FlowLayout());
greeting.setFont(new Font("Helvetica", Font.BOLD | Font.ITALIC,15));
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.MAGENTA);
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.RED);
field4.addActionListener(this);
getContentPane().add(field4);
instructions.setFont(new Font("Arial", Font.BOLD, 10));
instructions.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
repaint();
actionPerformed(e);
}
});
getContentPane().add(instructions, BorderLayout.SOUTH);
}
 
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);
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 * total;
double bDegree = b * total;
double cDegree = c * total;
double dDegree = d * total;
System.out.println(aDegree);
System.out.println(bDegree);
System.out.println(cDegree);
System.out.println(dDegree);
g.setColor(Color.CYAN);
g.fillArc(200, 250, 100, 100, 100, (int) aDegree);
g.setColor(Color.PINK);
g.fillArc(200, 250, 100, 100, 100, 216);
g.setColor(Color.MAGENTA);
g.fillArc(200, 250, 100, 100, 100, 108);
g.setColor(Color.RED);
g.fillArc(200, 250, 100, 100, 100, 36);
repaint();
}
}





these are the errors i get, im not sure how to post what output of the actual applet but it has the 4 text fields and you put
50 in each one and press enter and then 50.0 is showed continuously on the bottom of the console until you terminate the program
Exception in thread "AWT-EventQueue-1" java.lang.NumberFormatException: empty String
	at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:994)
	at java.lang.Double.parseDouble(Double.java:510)
	at graph.paint(graph.java:82)
	at sun.awt.RepaintArea.paintComponent(RepaintArea.java:276)
	at sun.awt.RepaintArea.paint(RepaintArea.java:241)
	at apple.awt.ComponentModel.handleEvent(ComponentModel.java:263)
	at java.awt.Component.dispatchEventImpl(Component.java:4790)
	at java.awt.Container.dispatchEventImpl(Container.java:2143)
	at java.awt.Component.dispatchEvent(Component.java:4544)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:635)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)



50.0
50.0
50.0
50.0



This post has been edited by custurd122000: 09 May 2011 - 03:05 PM


Is This A Good Question/Topic? 0
  • +

Replies To: pie chart applet

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10194
  • View blog
  • Posts: 37,645
  • Joined: 27-December 08

Re: pie chart applet

Posted 09 May 2011 - 03:13 PM

Your problem is that you are parsing the contents of the JTextFields before the user enters any values. Don't do this in paint(). Do this in the actionPerformed() method, then invoke repaint().

Also, don't recursively invoke actionPerformed(). There is no reason to. That simply eats up resources.
Was This Post Helpful? 0
  • +
  • -

#3 custurd122000  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 139
  • Joined: 05-March 09

Re: pie chart applet

Posted 09 May 2011 - 03:21 PM

so i delete the data from in the paint method
and just leave

super.paint(g);

repaint();



in the paint method?

also what do you mean exactly by recursively invoking actionPerformed() ?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10194
  • View blog
  • Posts: 37,645
  • Joined: 27-December 08

Re: pie chart applet

Posted 09 May 2011 - 03:25 PM

I wouldn't even repaint() in your paint() method, as there is no data being updated at that point. Just draw the pie if the submit button has been clicked (you can use a boolean variable, flagging it in the actionPerformed() method).

In terms of the recursion, since your actionPerformed() method tied to the JButton is in an anonymous listener, which is an inner class, this call actionPerformed(e); will invoke the actionPerformed() method tied to the JButton, not your custom method in the outer class.
Was This Post Helpful? 0
  • +
  • -

#5 custurd122000  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 139
  • Joined: 05-March 09

Re: pie chart applet

Posted 09 May 2011 - 03:38 PM

The only thing that happens is theres no longer any chart at all just the 4 text fields
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10194
  • View blog
  • Posts: 37,645
  • Joined: 27-December 08

Re: pie chart applet

Posted 09 May 2011 - 03:40 PM

Post your revised code.
Was This Post Helpful? 0
  • +
  • -

#7 custurd122000  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 139
  • Joined: 05-March 09

Re: pie chart applet

Posted 09 May 2011 - 03:43 PM


 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class graph extends JApplet implements ActionListener
{

private static final long serialVersionUID = 1L;
private JLabel greeting = new JLabel("Investment Analysis");
private JLabel info = new JLabel("Enter dollars amounts : ");
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 JButton instructions = new JButton("Enter");

public void init ()
{
getContentPane().setLayout(new FlowLayout());
greeting.setFont(new Font("Helvetica", Font.BOLD | Font.ITALIC,15));
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.MAGENTA);
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.RED);
field4.addActionListener(this);
getContentPane().add(field4);


instructions.setFont(new Font("Arial", Font.BOLD, 10));
instructions.addActionListener(new ActionListener(){
	
	
public void actionPerformed(ActionEvent e) {
repaint();
//actionPerformed(e);
}
});

getContentPane().add(instructions, BorderLayout.SOUTH);
}

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);

//repaint();
}




Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10194
  • View blog
  • Posts: 37,645
  • Joined: 27-December 08

Re: pie chart applet

Posted 09 May 2011 - 03:45 PM

I never suggested removing all of your code to draw the Graph from your paint() method. I suggested not invoking repaint() in paint(), and not parsing your JTextField Strings in paint(). You still want to draw in paint().
Was This Post Helpful? 0
  • +
  • -

#9 custurd122000  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 139
  • Joined: 05-March 09

Re: pie chart applet

Posted 09 May 2011 - 03:52 PM

oh sorry you're absolutely right i accidently deleted too much instead of just deleting the data, but when im drawing
i get an error on this line
g.fillArc(200, 250, 100, 100, 100, (int) aDegree);
because aDegree is being defined in a different method
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10194
  • View blog
  • Posts: 37,645
  • Joined: 27-December 08

Re: pie chart applet

Posted 09 May 2011 - 04:14 PM

So define it as an instance variable rather than in another method.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1