自己写的平面柱状图,有个小bug

这几天突然想自己写个柱状图,查了下API,就开始动手写了个,用JApplet做的视图,方便调试,但是发现一个bug,不知道怎么回事,知道的请回下,谢谢.

下面是例程:

package com.lazy.histogram; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import javax.swing.JApplet; /** * 柱状图 * * @author lazy_p * @date 2010-3-20 */ public class PlaneHistogram extends JApplet { private static final long serialVersionUID = 2322130592726740649L; private final int imageHeight = 400;// 柱状图的默认高度 private final int histogramWidth = 15;// 柱形图的宽度 private final int histogramPitch = 10;// 柱形图的间距 private float scaling = 1f;// 缩放的比例 private int maxStrWidth = 0; // 字符串需要的最大宽度 @Override public void paint(Graphics g) { paintPlaneHistogram(g, "柱状图", new int[] { 100, 60, 80, 50, 90 }, new String[] { "苹果", " 梨子", "香蕉", "菠萝蜜", "火腿肠" }, new Color[] { Color.RED, Color.GREEN, Color.BLUE, Color.cyan, Color.BLACK }); } /** * <pre> * 参数b[i]和str[i]必须对应 * </pre> * * @param g * @param title * @param b * @param str * @param color * 可以为空 */ private void paintPlaneHistogram(Graphics g, String title, int[] b, String[] str, Color[] color) { FontMetrics metrics = null; // 调用calculateImageSize()计算出图片的高度和宽度 setSize(calculateImageSize(g.getFontMetrics(new Font(null, Font.PLAIN, 12)), str, b)); g.setFont(new Font(null, Font.BOLD, 18)); g.setColor(Color.RED); g.drawString(title, (this.getWidth() - g.getFontMetrics().stringWidth( title)) >> 1, 30);// 画标题 g.setFont(new Font(null, Font.PLAIN, 12)); metrics = g.getFontMetrics(); g.setColor(Color.BLACK); g.drawLine(10, 0, 10, this.getHeight() - 15); // 画Y坐标 g.drawLine(10, this.getHeight() - 15, this.getWidth(), this.getHeight() - 15);// 画X坐标 for (int i = 0; i < b.length; ++i) { if (color != null) g.setColor(color[i]);// 设置前景色 else g.setColor(Color.RED); int x = 20 + i * (histogramPitch + histogramWidth + (maxStrWidth >> 1));// 计算出X坐标 int y = this.getHeight() - 16 - (int) (b[i] * scaling); // 计算出Y坐标 // 画占的比例 g.drawString(b[i] + "", x - ((metrics.stringWidth(b[i] + "") - histogramWidth) >> 1), y); // 画平面的柱状图 g.drawRect(x, y, histogramWidth, (int) (b[i] * scaling)); g.fillRect(x, y, histogramWidth, (int) (b[i] * scaling)); // 画每一项表示的东西 g.drawString(str[i], x - ((metrics.stringWidth(str[i]) - histogramWidth) >> 1), this.getHeight() - 2); } } /** * 计算实际需要显示的高度和宽度 * * @param metrics * @param str * @param b * @return Dimension */ private Dimension calculateImageSize(FontMetrics metrics, String[] str, int[] b) { int len = str.length; maxStrWidth = -1; // 字符串需要的最大宽度 for (int i = 0; i < len; ++i) { int strwidth = metrics.stringWidth(str[i]); // 计算出该字符串需要占据的宽度 if (strwidth > maxStrWidth) maxStrWidth = strwidth; } int maxHeight = -1; for (int i = 0; i < len; ++i) { if (b[i] > maxHeight) maxHeight = b[i]; } int width = ((maxStrWidth >> 1) + histogramPitch + histogramWidth) * len;// 算出需要的宽度 int height = imageHeight; if (maxHeight > height) { scaling = (float) height / maxHeight; // 当给的最大的比例值大于了默认的高度就算个比例 height += 30; } else height = maxHeight + 80; return new Dimension(width + 15, height); } public static void main(String[] args) { } }

效果如图:

下一步:把平面的转化为立体效果的,赫赫!到时整理下代码打包.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值