Question

In: Computer Science

I only need to update this JAVA program to be able to : 1 ) Exit...

I only need to update this JAVA program to be able to :

1 ) Exit cleanly after creating the chart

2 ) change the pie chart to a perfect circle rather than an oval

3 ) provide a separate class driver

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

}
}

here's the assignment for reference :

Pie chart: prompt the user (at the command line) for 4 positive integers, then draw 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.

Solutions

Expert Solution

/********** Need to change this file ***********/

/**************** Graphic_Pie2D.java **********************/

import java.util.Scanner;
import javax.swing.JFrame;

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, 500);
        frame.setVisible(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

/************** setSize sets the dimensions, of frame, if you want a circle height and width (both parametrs) must be equals **************/

/*********** setDefaultCloseOperation sets whats happen when you click the top right red cross ************/

/*************** Output ***************/

/*********** If you have any doubt, you may ask in comments ***************/

/***************** Thanks for reading ******************/


Related Solutions

I want to update this JAVA program to : Exit cleanly, and change the pie chart...
I want to update this JAVA program to : Exit cleanly, and change the pie chart to circle rather than an oval, and provide a separate driver please and thank you 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;...
I need to update this java program to take input about each employee from a file...
I need to update this java program to take input about each employee from a file and write their information and salary/pay information to a file. use input file payroll.txt Kevin Yang 60 20 Trey Adams 30 15 Rick Johnson 45 10 Cynthia Wheeler 55 11.50 Sarah Davis 24 10 Muhammad Rabish 66 12 Dale Allen 11 18 Andrew Jimenez 80 15 import java.util.Scanner; public class SalaryCalcLoop { double Rpay = 0, Opay = 0; void calPay(double hours, double rate)...
I need to update this program to follow the these requirements please and thank you :...
I need to update this program to follow the these requirements please and thank you : Do not use packages Every class but the main routine class must have a toString method . The toString method for Quadrilateral should display its fields: 4 Points. All instance variables should be declared explicitly private . Area incorrect : enter points no: 1 1 3 enter points no: 2 6 3 enter points no: 3 0 0 enter points no: 4 5 0...
Java homework problem: I need the code to be able to have a message if I...
Java homework problem: I need the code to be able to have a message if I type in a letter instead of a number. For example, " Please input only numbers". Then, I should be able to go back and type a number. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class LoginGui {    static JFrame frame = new JFrame("JFrame Example");    public static void main(String s[]) {        JPanel panel...
A C PROGRAM *Edit/Update I do not need the file loading script, but I am not...
A C PROGRAM *Edit/Update I do not need the file loading script, but I am not against it being included in the answer* I must read off of an excel file (comma separated) to input five different things for a book, all comma separated, there are 360 books in the file. The book title, author, ISBN number, number of pages, and finally the year it was published. Now some of the ISBN or Pg numbers may be missing and will...
I was able to get questions 1 - 4 answer and only need question 5 and...
I was able to get questions 1 - 4 answer and only need question 5 and its sub-parts. Asymmetric Information and Separating Equilibrium A population has two equal-sized members of "healthy" and "unhealthy" individuals. Members of each type have the same, identical, utility function: U = 20Y0.5 (i.e. 20 x Y raised to the 0.5 power), where Y is annual income.                            Assume each individual, in either group, has disposable income (after normal expenses) of $19,000 a year. If in...
Make a java program of Mickey I have the starter program but I need to add...
Make a java program of Mickey I have the starter program but I need to add eyes and a smile to it. import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import javax.swing.JFrame; public class Mickey extends Canvas { public static void main(String[] args) { JFrame frame = new JFrame("Mickey Mouse"); Canvas canvas = new Mickey(); canvas.setSize(400, 400); canvas.setBackground(Color.white); frame.add(canvas); frame.pack(); frame.setVisible(true); } public void paint(Graphics g) { Rectangle bb = new Rectangle(100, 100, 200, 200); mickey(g, bb); } public void...
using java program I need only the tracing in detail, using my name "Nada Hamdan" Part-A:...
using java program I need only the tracing in detail, using my name "Nada Hamdan" Part-A: Consider first ten letters of your name and for each letter encode with an integer number. For example: If “Ahmed Naser” are the first ten letters from your name, then the array of integers will be {0, 7, 12, 4, 3,13,0,18,4,17}. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22...
Using java, I need to make a program that reverses a file. The program will read...
Using java, I need to make a program that reverses a file. The program will read the text file character by character, push the characters into a stack, then pop the characters into a new text file (with each character in revers order) EX: Hello World                       eyB       Bye            becomes    dlroW olleH I have the Stack and the Linked List part of this taken care of, I'm just having trouble connecting the stack and the text file together and...
i need a pseudocode for a program in java that will convert dollars into euros and...
i need a pseudocode for a program in java that will convert dollars into euros and japanese yen using the print and prinln methods an if, if -else, statement a switch statement a while statement utilizes the file class uses the random class and random number generator and uses at least three methods other than main
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT