Question

In: Computer Science

JAVA - Write a program that prompts the user (at the command line) for 4 positive...

JAVA - Write a program that prompts the user (at the command line) for 4 positive integers, then draws a pie chart in a window. Convert the numbers to percentages of the numbers’ total sum; color each segment differently; use Arc2D. No text fields (other than the window title) are required. Provide a driver in a separate source file to test your class.

Please use the following:

java.lang.Object

java.awt.Graphics

java.awt.Graphics2D

Take note of the following:

setPaint

setStroke

fill

// imports

public class PieChartPanel extends JPanel {

// attributes

// constructor

public void paintComponent(Graphics g) {

super.paintComponent (g);

Graphics2D g2d = ( Graphics2D ) g;

...

}

There should be a paintComponent method that calls its parent

Solutions

Expert Solution

CODE:

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 to store the value and color mapping of the
// pie segment(slices)
class Segment {
   double value;
   Color color;

   // constructor
   public Segment(double value, Color color) {
       this.value = value;
       this.color = color;
   }
}

class pieChartComponent extends JComponent {

   private static final long serialVersionUID = 1L;
   Segment[] Segments;

   // Parameterized constructor
   // create 4 segments of the pie chart
   pieChartComponent(int v1, int v2, int v3, int v4) {
       Segments = new Segment[] { new Segment(v1, Color.black), new Segment(v2, Color.green), new Segment(v3, Color.yellow),
               new Segment(v4, Color.red) };
   }

   // function responsible for calling the worker method drawPie
   public void paint(Graphics g) {
       drawPie((Graphics2D) g, getBounds(), Segments);
   }

   // worker function for creating the percentage wise slices of the pie chart
  
   void drawPie(Graphics2D g, Rectangle area, Segment[] Segments) {
       double total = 0.0D;
       // fin the total of the all the inputs provided by the user
       for (int i = 0; i < Segments.length; i++) {
           total += Segments[i].value;
       }
      
       // Initialization
       double curValue = 0.0D;
       int strtAngle = 0;
       // iterate till all the segments are covered
       for (int i = 0; i < Segments.length; i++) {
           // compute start angle, with percentage
           strtAngle = (int) (curValue * 360 / total);
           // find the area angle of the segment
           int arcAngle = (int) (Segments[i].value * 360 / total);

           g.setColor(Segments[i].color);
           g.fillArc(area.x, area.y, area.width, area.height, strtAngle, arcAngle);
           curValue += Segments[i].value;
       }
   }
}

public class Graphic_Pie2D {
   public static void main(String[] argv) {

       System.out.println("Pleae provide 4 values, to create the pie chart");
       Scanner input = new Scanner(System.in);
       int v1, v2, v3, v4;
       v1 = input.nextInt();
       v2 = input.nextInt();
       v3 = input.nextInt();
       v4 = input.nextInt();
      
       // create a JFrame with title
       JFrame frame = new JFrame("Pie Chart");
       frame.getContentPane().add(new pieChartComponent(v1,v2,v3,v4));
       frame.setSize(500, 300);
       frame.setVisible(true);

   }
}

RESULT:


Related Solutions

In Java: Write a program that prompts the user to enter a positive number until the...
In Java: Write a program that prompts the user to enter a positive number until the user input a negative number. In the end, the program outputs the sum of all the positive numbers. You should use do-while loop (not while, not for). You cannot use break or if statement in the lab. Hint: if the program adds the last negative number to your sum, you can subtract the last number from the sum after the loop.
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
Write a Java program (name it InputSum) that prompts the user to enter positive integer numbers...
Write a Java program (name it InputSum) that prompts the user to enter positive integer numbers using a sentinel while loop. The program should accept integer inputs until the user enters the value -1 (negative one is the sentinel value that stops the while loop). After the user enters -1, the program should display the entered numbers followed by their sum as shown below. Notice that -1 is not part of the output. The program should ignore any other negative...
Write a Java program which reads a positive integer from the command line, then displays the...
Write a Java program which reads a positive integer from the command line, then displays the sum of all even values up to and including the value provided, followed by the sum of all odd values up to and including the value provided. validate that the command line argument is an integer greater than 0 to validate the type, you can use Integer.parseInt() with a try/catch for NumberFormatException use one or more for loops to perform the even and odd...
JAVA Program Write a program that prompts the user for data until the user wishes to...
JAVA Program Write a program that prompts the user for data until the user wishes to stop (you must have a while loop) (You must read in at least 8 to 10 sets of voter data using dialog boxes) The data to read in is: The registration of the voter (Democrat, Republican or other) The gender of the voter The Presidential candidate the voter is choosing (Trump or Biden) Which candidate has done better to manage the economy? (Trump or...
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Write a program in C that reads prompts the user for a positive integer and then...
Write a program in C that reads prompts the user for a positive integer and then prints out that number in base 16, base 8 and base 2.
Write a program which: Prompts the user for a positive integer >= 0 Validates the user...
Write a program which: Prompts the user for a positive integer >= 0 Validates the user input to ensure it is a positive integer >= 0 Allocate (dynamically) an array big enough for the data. Load the array with random numbers ranging in value from1 to 100 Display the elements of the array (unsorted) Display the elements of the array (sorted) Display the average Display the median Display the mode, if none, display appropriate message RESTRICTIONS No global variables No...
Write a program in JAVA that prompts the user for a lower bound and an upper...
Write a program in JAVA that prompts the user for a lower bound and an upper bound. Use a loop to output all of the even integers within the range inputted by the user on a single line.
JAVA: Write a program that prompts the user to input a type of medication and the...
JAVA: Write a program that prompts the user to input a type of medication and the output will be a list of side effects that can occur from that medication.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT