Topic:

Graphics

Description

Write a program that inputs sales amounts from five regions of a company and displays a pie chart as a percentage of sales. See Implementation, Testing, Picture and Sample Code sections below.

Implementation

Write the following classes:

class JFrmeExt

Write a class JFrameExt that extends JFrame and provides the following:

  • Five private double variables for holding sales amounts from five regions: North, South, East, West, and Midwest.
  • A public constructor with five parameters that initializes the sales amounts for the five regions.
  • The method paint() that overrides the paint method in the parent class. In the paint method, convert the sales amounts into fraction of the total and display them as a pie chart as shown below. Also show a legend at the bottom of the pie chart.

class Main

Write a class Main that contains the method main(). In the main method, provide the following:

  • Prompt the user to input sales amounts for the five regions as double values.
  • Create a JFrameExt object and pass it the sales amounts of the five regions.
  • Set the size of the JFrameExt object to 500,650.
  • Set the JFrameExt object to be visible.

Testing

Use the following test data in millions of dollars:

North: 20.0
South: 25.0
East: 15.0
West: 30.0
Midwest 10.0

Picture

See image:

Sample Code

//Class JFrameExt

import java.awt.Color;
import java.awt.Graphics;
import java.awt.HeadlessException;

import javax.swing.JFrame;


public class JFrameExt extends JFrame {
private double north;
private double south;
private double east;
private double west;
private double midwest;
public JFrameExt(double north, double south, double east, double west,
double midwest) throws HeadlessException {
super();
this.north = north;
this.south = south;
this.east = east;
this.west = west;
this.midwest = midwest;
}
@Override
public void paint (Graphics g){
//clean the surface of all drawings
super.paint(g);

double total=north+south+east+west+midwest;

//draw arc for north region
int startAngle=0;
int arcLength=(int)(north/total*360.0);
g.setColor(Color.red);
g.fillArc(100,100,300,300,startAngle,arcLength);

//draw arc for south region
startAngle=startAngle+arcLength;
arcLength=(int)(south/total*360.0);
g.setColor(Color.green);
g.fillArc(100,100,300,300,startAngle,arcLength);

//draw arc for east region





//draw arc for west region






//draw arc for midwest region





//draw the legend for north region

int xRect=100;
int rectWidth=50;
int horSpacing=10;
int vertSpacing=5;

int yRect=500;
int rectHeight=10;
g.setColor(Color.red);
g.fillRect (xRect,yRect,rectWidth,rectHeight);

int xString=xRect+rectWidth+horSpacing;
int yString=yRect+rectHeight;
String theString="North - 20.0";
g.drawString (theString,xString,yString);

//draw the legend for south region
//x-values stay the same
//y-values change
yRect=yRect+rectHeight+vertSpacing;
rectHeight=10;
g.setColor(Color.green);
g.fillRect (xRect,yRect,rectWidth,rectHeight);

//xString=xRect+rectWidth+horSpacing;
yString=yRect+rectHeight;
theString="South - 25.0";
g.drawString (theString,xString,yString);

//draw the legend for east region


//draw the legend for west region


//draw the legend for east region


}
}

//Class Main
//Input the sales amount from the user.
//In the code below, sales amounts are hard-coded for convenience

public class Main {
public static void main(String[] args) {
JFrameExt f = new JFrameExt(20,25,15,30,10);
f.setSize(500,650);
f.setVisible(true);

}

}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.