TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
- 这是这个Applet的html文件:
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <HTML>
- <BODY>
- <APPLET CODE = "PieNew.class" WIDTH = "600" HEIGHT = "500">
- <PARAM NAME = "titleFontName" VALUE = "黑体">
- <PARAM NAME = "titleFontSize" VALUE = "30">
复制代码
<PARAM NAME = "chartTitle" VALUE = "Java Web图表设计Applet & TML版">
<PARAM NAME = "bookTitle1" VALUE = "python">
<PARAM NAME = "bookSales1" VALUE = "45">
<PARAM NAME = "bookTitle2" VALUE = "java">
<PARAM NAME = "bookSales2" VALUE = "115">
<PARAM NAME = "bookTitle3" VALUE = "C#">
<PARAM NAME = "bookSales3" VALUE = "70">
<PARAM NAME = "bookTitle4" VALUE = "Perl">
<PARAM NAME = "bookSales4" VALUE = "60">
<PARAM NAME = "bookTitle5" VALUE = "PHP">
<PARAM NAME = "bookSales5" VALUE = "35">
<PARAM NAME = "subTitle" VALUE = "计算机编程类图书销售统计图">
</APPLET>
</BODY>
</HTML>
下面是源文件:
// Fig. 3.03: PieNew.java
// Java Applet Web图表实例5:
// 通过读取HTML文档中的数据来绘制书籍销售量统计饼图
import java.awt.*; // 引入 java.awt包中所有的类
import javax.swing.*; // 引入 javax.swing包中所有的类
import java.text.*; // 引入 java.text包中所有的类
public class PieNew extends JApplet
{
Image offImage;
Graphics offGraphics;
int appletWidth = 600, appletHeight = 500;
int bookSales[] = new int[5];
int totalSales = 0;
String[]bookTitle = new String[5];
String[]tempBookSales = new String[5];
String chartTitle, subTitle;
String titleFontName;
int titleFontSize = 20;
// 初始化颜色数组
Color color[] =
{
new Color(99, 99, 0), Color.GREEN, Color.YELLOW, Color.RED, Color.BLUE
};
// 初始化绘图缓冲区
public void init()
{
offImage = createImage(appletWidth, appletHeight);
offGraphics = offImage.getGraphics();
titleFontName = getParameter("titleFontName");
String tempFontSize = getParameter("titleFontSize");
if (tempFontSize != null)
{
titleFontSize = Integer.parseInt(tempFontSize);
}
chartTitle = getParameter("chartTitle");
for (int i = 0; i < 5; i++)
{
bookTitle = getParameter("bookTitle" + (i + 1));
tempBookSales = getParameter("bookSales" + (i + 1));
if (tempBookSales != null)
{
bookSales = Integer.parseInt(tempBookSales);
}
else
{
bookSales = 0;
}
totalSales += bookSales;
}
subTitle = getParameter("subTitle");
}
public void paint(Graphics g)
{
// 调用父类的 paint 方法
super.paint(g);
update(g);
} // paint 方法结束
public void update(Graphics g)
{
// 绘制标题区域
offGraphics.setColor(Color.BLACK);
offGraphics.setFont(new Font(titleFontName, Font.BOLD, titleFontSize));
offGraphics.drawString(chartTitle, 15, 30);
offGraphics.drawString(subTitle, 70, 465);
// 绘制图例
offGraphics.setFont(new Font("SansSerif", Font.PLAIN, 12));
offGraphics.drawString("编程类图书", 400, 125);
offGraphics.drawString("销售数量", 475, 125);
offGraphics.drawString("所占比例", 535, 125);
offGraphics.setColor(Color.blue);
offGraphics.drawRect(395, 95, 190, 300);
// 绘制实心圆弧
int arcStartAngle = 30, arcAngle = 0;
float proportion;
DecimalFormat twoDigits = new DecimalFormat("0.00");
for (int i = 0; i < bookTitle.length; i++)
{
arcAngle = (int)(bookSales * 360 / (float)totalSales + 0.5);
proportion = ((float)bookSales) / totalSales * 100;
offGraphics.setColor(color);
if (i < bookTitle.length - 1)
{
offGraphics.fillArc(15, 95, 350, 300, arcStartAngle, arcAngle);
}
else
{
offGraphics.fillArc(30, 93, 350, 300, arcStartAngle, arcAngle);
}
arcStartAngle += arcAngle;
offGraphics.fillRect(400, 155+i * 50, 12, 12);
offGraphics.setColor(Color.black);
offGraphics.drawString(bookTitle, 420, 167+i * 50);
offGraphics.drawString("" + bookSales, 490, 167+i * 50);
offGraphics.drawString(twoDigits.format(proportion) + "%", 540, 167
+i * 50);
}
// 输出缓冲区图像
g.drawImage(offImage, 0, 0, null);
}
} // PieNew 类结束
/**************************************************************************
* (C) Copyright 2004-2005 by Jingkui Zhong(钟京馗) and Huan Tang(唐桓). *
* All Rights Reserved. *
* *
* DISCLAIMER: The authors of this code have used their *
* best efforts in preparing the code. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these codes. The authors *
* shall not be liable in any event for incidental or consequential *
* damages in connection with, or arising out of, the furnishing, *
* performance, or use of these programs. *
**************************************************************************/
[/code]
程序运行图:
function TempSave(ElementID)
{
CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
CommentsPersistDiv.save("CommentXMLStore");
}
function Restore(ElementID)
{
CommentsPersistDiv.load("CommentXMLStore");
document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
}
源码下载:http://www.hnzz3z.com:8103/zz3zcwb/cwb/dir5/Fig3.3.zip |
|