Help With Displaying Pie Chart
Hi, I've put some code together but cannot seem to get my chart to display. I hope it's something simple but being very new to this I'm having a difficult time.Thanks in advance.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import java.awt.Color;
publicclass MortgageCalc_wk5extends JFrameimplements ActionListener{
privatestaticfinallong serialVersionUID = 6;
privatestaticfinal Rectangle Rectangle =null;
//Set up panels for Frame
JPanel topPanel =new JPanel();
JPanel bottomPanel =new JPanel();
JPanel radioPanel =new JPanel();
JPanel grfxPanel =new JPanel();//new line
//Set up Layout
FlowLayout flowLayout =new FlowLayout();
BorderLayout borderLayout =new BorderLayout();
//Radio Choice Style Buttons
JRadioButton Choice1 =new JRadioButton ("User Input",true);
JRadioButton Choice2 =new JRadioButton ("User Selection",false);
//Set up JLabels, JButtons, and JTextFields
JLabel amountLabel =new JLabel("Loan Amount");
JTextField loanField =new JTextField(7);
JLabel termLabel =new JLabel("Term(Yrs)");
JTextField termField =new JTextField(4);
JLabel rateLabel =new JLabel("Rate");
JTextField rateField =new JTextField(4);
JComboBox options =new JComboBox();
JLabel optionsLabel =new JLabel();
JButton calcButton =new JButton("Calculate");
JButton resetButton =new JButton("Reset");
JButton exitButton =new JButton("Exit");
JButton grfxButton =new JButton("Display Graphics");//new line
JLabel paymentLabel =new JLabel("Monthly Payment =");
JTextField paymentField =new JTextField(6);
JLabel outputLabel =new JLabel("Loan Amoritization Chart");
JTextArea calcField =new JTextArea(10, 25);
JScrollPane scroll =new JScrollPane(calcField, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
ButtonGroup radioSelect =new ButtonGroup();
JLabel grfxLabel =new JLabel ("Graphic Chart");//new line
JTextField grfxField =new JTextField ("Graphics");// new line
//Default constructor
public MortgageCalc_wk5(){
super("Tony's Calculator - Week 5");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
//add ActionListeners
loanField.addActionListener(this);
termField.addActionListener(this);
rateField.addActionListener(this);
options.addActionListener(this);
calcButton.addActionListener(this);
grfxButton.addActionListener(this);//new line
resetButton.addActionListener(this);
exitButton.addActionListener(this);
Choice1.addActionListener(this);
Choice2.addActionListener(this);
//Top Panel Layout
topPanel.setLayout(flowLayout);
topPanel.add(amountLabel);
topPanel.add(loanField);
radioSelect.add(Choice1);
radioSelect.add(Choice2);
topPanel.add(Choice1);
topPanel.add(Choice2);
//Loan Term Options For JComboBox
options.addItem("7 years @ 5.35%");
options.addItem("15 years @ 5.5%");
options.addItem("30 years @ 5.75%");
options.setEnabled(false);
topPanel.add(options);
topPanel.add(termLabel);
topPanel.add(termField);
topPanel.add(rateLabel);
topPanel.add(rateField);
topPanel.add(paymentLabel);
topPanel.add(paymentField);
paymentField.setEditable(false);
//Was Bottom Panel Now East Panel, Calculate Reset,Exit, and Display Buttons
bottomPanel.add(calcButton);
bottomPanel.add(resetButton);
bottomPanel.add(exitButton);
bottomPanel.add(grfxButton);//new line
calcButton.setBackground(Color.blue);
resetButton.setBackground(Color.white);
exitButton.setBackground(Color.red);
calcField.setBackground(Color.orange);
grfxPanel.setBackground(Color.green);//new line
grfxPanel.add(grfxField);// new line
//add Container
Container pane = getContentPane();
pane.setLayout(borderLayout);
pane.add(topPanel, BorderLayout.NORTH);
pane.add(calcField, BorderLayout.WEST);
pane.add(grfxPanel, BorderLayout.SOUTH);//new line
pane.add(bottomPanel, BorderLayout.EAST);
calcField.setLineWrap(true);
calcField.setWrapStyleWord(true);
calcField.setEditable(false);
pane.add(scroll);
scroll.setViewportView(calcField);
pack();
grfxPanel.setBounds(Rectangle);//new line
}
//actionEvent Listeners
publicvoid actionPerformed(ActionEvent event){
Object source = event.getSource();
if (source == calcButton){
startCalculations();
}
if (source == resetButton){
reset();
}
if (source == grfxButton){//new line
ChartPanel();
}
if (source == exitButton){
end();
}
if (source == Choice1){
options.setEnabled(false);
termField.setEnabled(true);
rateField.setEnabled(true);
}
if (source == Choice2){
options.setEnabled(true);
termField.setEnabled(false);
rateField.setEnabled(false);
}
}
publicvoid ChartPanel(){//newline
JFrame grfxPanel =new JFrame();
grfxPanel.setSize(400, 300);
double[] values =newdouble[3];
String[] names =new String[3];
values[0] = 1;
names[0] ="Item 1";
values[1] = 2;
names[1] ="Item 2";
values[2] = 4;
names[2] ="Item 3";
grfxPanel.getContentPane().add(new ChartPanel(values, names,"title"));
}
publicclass ChartPanelextends JPanel{
privatestaticfinallong serialVersionUID = 6;
publicdouble[] values;
public String[] names;
public String title;
public ChartPanel(double[] v, String[] n, String t){
names = n;
values = v;
title = t;
}
publicvoid paintComponent(Graphics g){
super.paintComponent(g);
if (values ==null || values.length == 0)
return;
double minValue = 0;
double maxValue = 0;
for (int i = 0; i < values.length; i++){
if (minValue > values[i])
minValue = values[i];
if (maxValue < values[i])
maxValue = values[i];
}
Dimension d = getSize();
int clientWidth = d.width;
int clientHeight = d.height;
int barWidth = clientWidth / values.length;
Font titleFont =new Font("SansSerif", Font.BOLD, 20);
FontMetrics titleFontMetrics = g.getFontMetrics(titleFont);
Font labelFont =new Font("SansSerif", Font.PLAIN, 10);
FontMetrics labelFontMetrics = g.getFontMetrics(labelFont);
int titleWidth = titleFontMetrics.stringWidth(title);
int y = titleFontMetrics.getAscent();
int x = (clientWidth - titleWidth) / 2;
g.setFont(titleFont);
g.drawString(title, x, y);
int top = titleFontMetrics.getHeight();
int bottom = labelFontMetrics.getHeight();
if (maxValue == minValue)
return;
double scale = (clientHeight - top - bottom) / (maxValue - minValue);
y = clientHeight - labelFontMetrics.getDescent();
g.setFont(labelFont);
for (int i = 0; i < values.length; i++){
int valueX = i * barWidth + 1;
int valueY = top;
int height = (int) (values[i] * scale);
if (values[i] >= 0)
valueY += (int) ((maxValue - values[i]) * scale);
else{
valueY += (int) (maxValue * scale);
height = -height;
}
g.setColor(Color.red);
g.fillRect(valueX, valueY, barWidth - 2, height);
g.setColor(Color.black);
g.drawRect(valueX, valueY, barWidth - 2, height);
int labelWidth = labelFontMetrics.stringWidth(names[i]);
x = i * barWidth + (barWidth - labelWidth) / 2;
g.drawString(names[i], x, y);
}
}
}
//Calculation Section
void startCalculations(){
NumberFormat currency = NumberFormat.getCurrencyInstance();
//Variables
double amount = 0;
double term = 0;
double interest = 0;
double moIn = 0;
double moTerm = 0;
double payment = 0;
double newPrin = amount;
boolean Exception =false;
//Input Validation
try{
amount = Double.parseDouble(loanField.getText());
}catch (NumberFormatException e){
JOptionPane.showMessageDialog(null,"Commas, Letters, and Puncuation NOT Allowed",
"Message Dialog", JOptionPane.ERROR_MESSAGE);
loanField.setText(null);
calcField.setText(null);
}
//User's Loan Choice
if (Choice2.isSelected())
{
if(options.getSelectedIndex() == 0)
{
term=7;
interest=5.35;
}
elseif(options.getSelectedIndex() ==1)
{
term=15;
interest=5.5;
}
else
{
term=30;
interest=5.75;
}
}
else
{
//More Validation
try
{
term = Double.parseDouble(termField.getText());
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(this,"Enter amounts greater than zero","Please enter new amount",JOptionPane.ERROR_MESSAGE);
Exception=true;
termField.setText(null);
}
try
{
interest= Double.parseDouble(rateField.getText());
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(this,"Enter an Amount Greater Than Zero, % Not Allowed","Please enter Another Amount",JOptionPane.ERROR_MESSAGE);
Exception=true;
rateField.setText(null);
}
}
if ((amount <= 0 || term <= 0 || interest <= 0) && (Exception ==false)){
JOptionPane.showMessageDialog(null,"Please Enter a Number Greater Than ZERO.",
"Message Dialog", JOptionPane.ERROR_MESSAGE);
if(amount <= 0)
loanField.setText(null);
paymentField.setText(null);
calcField.setText(null);
termField.setText(null);
rateField.setText(null);
}
//Amortization Results Chart
if (amount >7){
amount = Double.parseDouble(loanField.getText());
moIn = (interest / 12) / 100;
moTerm = term * 12;
payment = amount * (moIn / (1 - java.lang.Math.pow((1 + moIn), (-moTerm))));
paymentField.setText("" + currency.format(payment));
calcField.append("Period");
calcField.append("");
calcField.append("Payment Amount");
calcField.append("");
calcField.append("Interest Paid");
calcField.append("");
calcField.append("Principle Paid");
calcField.append("");
calcField.append("Loan Balance");
calcField.append("\n");
for (int i = 1; i <= moTerm; i++){
double newIn = moIn * amount;
double reduct = payment - newIn;
newPrin = amount - reduct;
amount = newPrin;
calcField.append("" + i);
calcField.append("" + currency.format(payment));
calcField.append("" + currency.format(newIn));
calcField.append("" + currency.format(reduct));
calcField.append("" + currency.format(newPrin) +"\n");
calcField.setCaretPosition(0);
}
}
}
// Program Reset, User Can Calculate Another Amount or Choose A Different Style
void reset (){
loanField.setText(null);
paymentField.setText(null);
calcField.setText(null);
termField.setText(null);
rateField.setText(null);
}
// Exit Command
void end(){
System.exit(0);
}
// main method
publicstaticvoid main(String args[]){
MortgageCalc_wk5 calc =new MortgageCalc_wk5();
calc.pack();
calc.setVisible(true);
}
}
// end of program

