| 添加到收藏夹 | 返回目录页 | 上一篇:applet 代码2 |
- Draw a pie chart
import java.util.*;import java.awt.*;
import java.applet.Applet;
public class Graph extends Applet {
int depth, radius;
public void init() {
float value;
String at = getParameter("width");
radius = (at != null) ? Integer.valueOf(at).intValue() : 100;
at = getParameter("depth");
depth = (at != null) ? Integer.valueOf(at).intValue() : 20;
at = getParameter("values");
PieChartCanvas c = new PieChartCanvas(radius, depth);
setLayout(new BorderLayout());
// Create Hashtable to map color name (String) to Color type
Hashtable colors = new Hashtable();
colors.put("green", Color.green);
colors.put("red", Color.red);
colors.put("blue", Color.blue);
colors.put("yellow", Color.yellow);
colors.put("magenta", Color.magenta);
colors.put("cyan", Color.cyan);
colors.put("orange", Color.orange);
colors.put("pink", Color.pink);
colors.put("white", Color.white);
colors.put("black", Color.black);
// "value-color,value-color,..."
StringTokenizer t = new StringTokenizer(at, ",");
String s;
int i;
while (t.hasMoreTokens()) {
s = t.nextToken();
i = s.indexOf(@#-@#);
value = Float.valueOf(s.substring(0, i)).floatValue();
c.addSlice(value, (Color)colors.get(s.substring(i + 1)));
}
resize(c.getMinimumSize().width, c.getMinimumSize().height);
add("Center", c);
}
}
class PieChartCanvas extends Canvas {
/*
** author Ciaran Treanor ciaran@broadcom.ie
*/
final double aspectFudge = 2.5;
int radius, depth, called = 1, numSlices = 0;
float total = 0, value[] = new float[10];
Color color[] = new Color[10];
Graphics offGraphics;
Image gfxBuff;
public PieChartCanvas(int radius, int depth) {
this.value = value;
this.color = color;
this.radius = radius;
this.depth = depth;
}
public void paint(Graphics g) {
int startAngle;
float angle;
Dimension d = getSize();
if(gfxBuff == null) {
gfxBuff = createImage(d.width, d.height);
offGraphics = gfxBuff.getGraphics();
offGraphics.setColor(getBackground());
offGraphics.fillRect(0, 0, d.width, d.height);
}
// do the 3d effect
for(int x = depth; x >= 1; x--) {
startAngle = -45;
for(int i = 0; i < numSlices; i++) {
offGraphics.setColor(color[i].darker());
angle = Math.round(360 * (value[i] / total));
offGraphics.fillArc(0, x, radius, (int)(radius / aspectFudge),
startAngle, (int)angle);
startAngle += angle;
}
}
// draw the pie slice
startAngle = -45;
for(int i = 0; i < numSlices; i++) {
offGraphics.setColor(color[i]);
angle = Math.round(360 * (value[i] / total));
offGraphics.fillArc(0, 0, radius, (int)(radius / aspectFudge),
startAngle, (int)angle);
startAngle += angle;
}
g.drawImage(gfxBuff, 0, 0, null);
}
public void addSlice(float value, Color color) {
this.value[numSlices] = value;
this.color[numSlices++] = color;
total += value;
}
public Dimension getPreferredSize() {
return getMinimumSize();
}
public Dimension getMinimumSize() {
return new Dimension(radius, (int)((radius / aspectFudge) + depth));
}
}
[JavaPie.hmtl]
<HTML>
<APPLET CODE=Graph.class WIDTH=150 HEIGHT=150>
<PARAM NAME="depth" VALUE="30">
<PARAM NAME="width" VALUE="120">
<PARAM NAME="values" VALUE="1-red,2-green,3-blue">
</APPLET>
</HMTL>
下一篇:提升JSP应用程序的七大绝招
↓相关文章:
- · 提升JSP应用程序的七大绝招
- · J2EE学习经验和流程
- · 怎样在J2EE组件中引用和查找Web服务
- · J2EE搭建WAP站点时图片发布统一格式处理解决方案
- · J2EE Enterprise Beans(中文翻译)
- · J2ME技术在手机开发领域的优势和不足
- · 安装 WebSphere应用服务器
- · 实战J2EE—开发购物网站(三)
- · 实战J2EE—开发购物网站(一)
- · J2EE项目中开发团队的组建
- · 开发J2EE应用的要领
- · J2EE系统优化的几点体会(一、对象)
- · Java服务器端编程安全必读(1)
- · J2EE构造web service
- · J2EE的13种核心技术(二)
- · 使用EJB3.O简化EJB开发(三)
- · 使用EJB3.O简化EJB开发(一)
- · J2EE编程起步(二)
- · 开发第一个J2EE、配置J2EE环境及Cloudscape数据库
- · J2EE Server下的第一个EJB程序(图文进阶篇)
- · 使用sitemesh建立复合视图 - 3.其他讨论
- · 使用sitemesh建立复合视图 - 1.hello
- · 基于J2EE的开发的一些经验
- · J2EE架构的6个最佳实践
- · 学习j2ee初有领悟的小总结
- · j2ee常见错误(放到这里备查)
- · 我与TOMCAT不得不说的二三事
- · J2EE SDK 1.4+Resin 2.1.16 的安装过程
- · J2EE的13种核心技术(一)
- · 开发J2EE应用的要领
- · J2EE 第2次课笔记--Servlet
- · 谈谈J2SE中的序列化(二)
- · J2EE deployment files(application-client.xml)
- · J2EE deployment files(ejb-jar2.0.xml)
- · J2EE deployment files (application.xml)
- · PHP 挑战 J2EE 和 .NET
- · J2EE初学者需要理解的五个问题
- · 开发J2EE应用应遵循的几点原则
- · J2EE初学者需要理解的五个问题
- · J2EE编程起步(二)
- · J2EE初学者需要理解的问题
- · 初学J2EE会碰到的一些问题
- · J2EE clustering 2---有些老了
- · J2EE 探索者:用五个容易的步骤实现 JSP 自定义标记
- · J2EE 中使用EntityBean和JDO各有什么优点缺点

