I need help in drawing a 2D piechart in Java using the four data amounts that a
ID: 655329 • Letter: I
Question
I need help in drawing a 2D piechart in Java using the four data amounts that a user enters. To get each of the slices I need to use the "fillarc" method. I've already made a Barchart that works but I cannot figure out the Pie chart. I would prefer if your answer could be applied within my code when you submit it but it's not needed. Thanks! This is the code that I have so far:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class BarChart extends JFrame implements ActionListener
{
JLabel title = new JLabel("Enter 4 dollar amounts for your investment portfolio: ");
JLabel first= new JLabel("First Value:");
JTextField firstIn = new JTextField("", 5);
JLabel second = new JLabel("Second Value:");
JTextField secondIn = new JTextField("", 5);
JLabel third = new JLabel("Third Value:");
JTextField thirdIn = new JTextField("", 5);
JLabel fourth = new JLabel("Fourth Value:");
JTextField fourthIn = new JTextField("", 5);
JButton button1 = new JButton("Pie Chart");
JButton button2 = new JButton("Bar Chart");
JButton button3 = new JButton("Clear");
DecimalFormat df = new DecimalFormat();
Container con = getContentPane();
FlowLayout flow = new FlowLayout();
JPanel topPanel = new JPanel(new GridLayout(3,0)); // to hold top 3 panels (see next 3 statements)
JPanel promptPanel = new JPanel(new FlowLayout()); // for topmost text
JPanel inputPanel = new JPanel (new FlowLayout()); // for labels and text fields
JPanel buttonPanel = new JPanel(new GridLayout(1,3)); // to hold the 3 buttons
JPanel centerPanel = new JPanel();
IAPieChart[] pieValue = {new IAPieChart(5, Color.green),
new IAPieChart(4, Color.orange),
new IAPieChart(3, Color.blue),
new IAPieChart(2, Color.red)
// in your constructor below, add components to the correct panels, then add the
// topmost 3 panels to the topPanel, then add the topPanel to the NORTH.
boolean barOn = false;
boolean pieOn = false;
public BarChart()
{
con.setLayout(new BorderLayout());
first.setForeground(Color.red);
second.setForeground(Color.orange);
third.setForeground(Color.blue);
fourth.setForeground(Color.green);
topPanel.add(promptPanel);
topPanel.add(inputPanel);
promptPanel.add(title);
topPanel.add(buttonPanel);
inputPanel.add(first);
inputPanel.add(firstIn);
inputPanel.add(second);
inputPanel.add(secondIn);
inputPanel.add(third);
inputPanel.add(thirdIn);
inputPanel.add(fourth);
inputPanel.add(fourthIn);
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
con.add(topPanel, BorderLayout.NORTH);
con.add(centerPanel, BorderLayout.CENTER);
firstIn.requestFocus();
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == button2)
{
barOn=true;
repaint();
}
topPanel.repaint();
}
public void paint(Graphics gr)
{
super.paint(gr);
df.setMaximumFractionDigits(1);
df.setMinimumFractionDigits(1);
gr.setColor(Color.white);
gr.fillRect(0,0,getWidth(),getHeight());
first.repaint();
firstIn.repaint();
second.repaint();
secondIn.repaint();
third.repaint();
thirdIn.repaint();
fourth.repaint();
fourthIn.repaint();
button1.repaint();
button2.repaint();
button3.repaint();
title.repaint();
if(barOn)
{
int barWidth=(int)((getWidth() - 20.0)/4-10);
int maxBarHeight = getHeight() - 200;
gr.setColor(Color.black);
gr.drawLine(5, getHeight()-15,getWidth()-5,getHeight()-15);
double in1 = Double.parseDouble(firstIn.getText());
double in2 = Double.parseDouble(secondIn.getText());
double in3 = Double.parseDouble(thirdIn.getText());
double in4 = Double.parseDouble(fourthIn.getText());
double max=in1;
if (in2>max) {max=in2;}
if (in3>max) {max=in3;}
if (in4>max) {max=in4;}
int x=15;
double total=in1+in2+in3+in4;
gr.setColor(Color.red);
int newHeight=(int)(maxBarHeight*in1/max);
int y = getHeight()-15-newHeight;S
gr.fillRect(x,y,barWidth,newHeight);
gr.drawString(df.format((in1/total*100))+"%", (x+(barWidth/2)-17), y - 7);
gr.setColor(Color.orange);
x += barWidth + 10;
newHeight=(int)(maxBarHeight*in2/max);
y = getHeight()-15-newHeight;
gr.fillRect(x,y,barWidth,newHeight);
gr.drawString(df.format((in2/total*100))+"%", (x+(barWidth/2)-17), y - 7);
gr.setColor(Color.blue);
x += barWidth + 10;
newHeight=(int)(maxBarHeight*in3/max);
y = getHeight()-15-newHeight;
gr.fillRect(x,y,barWidth,newHeight);
gr.drawString(df.format((in3/total*100))+"%", (x+(barWidth/2)-17), y - 7);
gr.setColor(Color.green);
x += barWidth + 10;
newHeight=(int)(maxBarHeight*in4/max);
y = getHeight()-15-newHeight;
gr.fillRect(x,y,barWidth,newHeight);
gr.drawString(df.format((in4/total*100))+"%", (x+(barWidth/2)-17), y - 7);
gr.setColor(Color.blue);
gr.setFont(new Font("Arial", Font.ITALIC, 16));
gr.drawString("Your investment portfolio",(getWidth()/2-35),getHeight()-centerPanel.getHeight()+15);
barOn=false;
}
}
public static void main(String[] args)
{
BarChart f = new BarChart();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(700,400);
f.setVisible(true);
f.setLocationRelativeTo(null);
}
}
Explanation / Answer
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;
import javax.swing.JFrame;
class Slice {
double value;
Color color;
public Slice(double value, Color color) {
this.value = value;
this.color = color;
}
}
class MyComponent extends JComponent {
Slice[] slices = { new Slice(5, Color.black), new Slice(33, Color.green),
new Slice(20, Color.yellow), new Slice(15, Color.red) };
MyComponent() {
}
public void paint(Graphics g) {
drawPie((Graphics2D) g, getBounds(), slices);
}
void drawPie(Graphics2D g, Rectangle area, Slice[] slices) {
double total = 0.0D;
for (int i = 0; i < slices.length; i++) {
total += slices[i].value;
}
double curValue = 0.0D;
int startAngle = 0;
for (int i = 0; i < slices.length; i++) {
startAngle = (int) (curValue * 360 / total);
int arcAngle = (int) (slices[i].value * 360 / total);
g.setColor(slices[i].color);
g.fillArc(area.x, area.y, area.width, area.height, startAngle, arcAngle);
curValue += slices[i].value;
}
}
}
public class PieChart {
public static void main(String[] argv) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MyComponent());
frame.setSize(700, 400);
frame.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.