Graphics
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.
Write the following classes:
class JFrmeExt
Write a class JFrameExt that extends JFrame and provides the following:
class Main
Write a class Main that contains the method main(). In the main method, provide the following:
Use the following test data in millions of dollars:
North: 20.0
South: 25.0
East: 15.0
West: 30.0
Midwest 10.0
See image:
//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);
}
}