A. Write, compile, and run a Java applet that draws a pie chart showing percenta
ID: 3543593 • Letter: A
Question
A. Write, compile, and run a Java applet that draws a pie chart showing percentage of
household income spent on various expenses. The pie chart is shown below:
(I HAVE ATTACH THE PIE CHART THAT HAS TO BE THE OUTPUT, SHOULD LOOK EXACTLY THE SAME)
Expense Percentage Color
Transportation 15% Green
Rent and Utilities 35% Blue
Miscellaneous 10% Orange
Educational 25% Red
Food 15% Cyan
The applet skeleton is shown below:
//*****************************************************************
// Imus B. Goode, INT 1210, Section LD-01
//
// PieChart.java
//
// Draws a pie chart showing percentage of household income spent on
various expenses.
//*****************************************************************
import javax.swing.JApplet;
import java.awt.*;
public class PieChart extends JApplet
{
public void paint (Graphics page)
{
// Specify program constants(degrees in a circle, applet width and height)
1// Specify corner point for the pie chart, diameter of the pie chart, starting angle for a pie
slice, and arc angle for a pie slice
// Set Background color white
// Draw rent and utilities slice
// Draw transportation slice
// Draw food slice
// Draw educational slice
// Draw miscellaneous slice
}//end paint
}//end PieChart
B. After you successfully compile your applet, save the html file of attachment 1 in the same
directory/folder as PieChart.java and PieChart.class. Open Windows Explorer to the this
directory/folder and double click on the html file - your default web browser will open and the
applet will run. Alternatively, you can open the html file in your web browser. Attachment 1 is
the source code for the html page you will open with your web browser to demonstrate your
applet. The source is shown below:
<html>
<applet code="PieChart.class" width="600" height="400">
</applet>
</html>
Explanation / Answer
// *************************************************************
// Colors.java
//
// Draw rectangles to illustrate colors and their codes in Java
// *************************************************************
import javax.swing.JApplet;
import java.awt.*;
public class Colors extends JApplet
{
public void paint (Graphics page)
{
// Declare size constants
final int PAGE_WIDTH = 600;
final int PAGE_HEIGHT = 400;
// Declare variables
int x, y; // x and y coordinates of upper left-corner of each shape
int width, height; // width and height of each shape
Color myColor = new Color (200, 100, 255);
// Set the background color and paint the screen with a white rectangle
setBackground (Color.white);
page.setColor(Color.white);
page.fillRect(0, 0, PAGE_WIDTH, PAGE_HEIGHT);
// Set the color for the rectangle
page.setColor (myColor);
// Assign the corner point and width and height then draw
x = 200;
y = 125;
width = 200;
height = 150;
page.fillRect(x, y, width, height);
}
}
Colors.html
<html>
<applet code="Colors.class" width=600 height=400>
</applet>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.