Pie chart prompt the user at the command line for 4 positive
Pie chart: prompt the user (at the command line) for 4 positive integers, thendraw a pie chart in a window. Convert the numbers to percentages of thenumbers’ total sum; color each segment differently; use Arc2D. No text fields(other than the window title) are required. Provide a driver in a separatesource file to test your class.
Can someone please write this for me in Java? Thank you.
Solution
import java.awt.Color;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.Rectangle;
 import java.util.Scanner;
 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 {
 Scanner reader = new Scanner(System.in); // Reading from System.in
//System.out.println(\"Enter a number: \");
 int a = reader.nextInt();
 int b = reader.nextInt();
 int c = reader.nextInt();
 int d = reader.nextInt();
 Slice[] slices = { new Slice(a, Color.black),
 new Slice(b, Color.green),
 new Slice(c, Color.yellow), new Slice(d, 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 Main {
 public static void main(String[] argv) {
System.out.println(\"Enter 4 numbers: \");
JFrame frame = new JFrame();
 frame.getContentPane().add(new MyComponent());
 frame.setSize(300, 200);
 frame.setVisible(true);
 }
 }


