前几天自己写了个平面的柱状图的实现,呵呵!现在花了一点时间,将其其升级为了立体柱状图.还是用的JApplet做的试图,现在没加双缓冲,所以看到的是一个一个的出现的,有时间的话我再好好把这两个类重构下,加个双缓冲,呵呵!效果如图:
示例代码如下:
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 * @version 1.0 * */ public class Histogram extends JApplet { private static final long serialVersionUID = 3562814718711641084L; private final int histogramWidth = 15; // 柱宽 private int histogramPitch = 10;// 间距 private float scaling = 0.0f; private int maxStrWidth = 0; // 字符串需要的最大宽度 public Histogram() { } @Override public void paint(Graphics g) { // setSize(200, 250); int[] b = new int[] { 100, 1000, 1200, 900 }; Color[] color = new Color[] { Color.red, Color.green, Color.lightGray, Color.BLUE }; paintHistogram(g, b, new String[] { "香蕉", "萝卜", "菠萝菠萝蜜", "西瓜" }, color); } /** * 画基面 * * @param g */ private void paintBase(Graphics g) { g.drawPolygon(new int[] { 10, 40, 40, 10 }, new int[] { 30, 10, this.getHeight() - 30, this.getHeight() - 10 }, 4); g.setColor(new Color(100, 100, 100, 20)); g.fillPolygon(new int[] { 10, 40, 40, 10 }, new int[] { 30, 10, this.getHeight() - 30, this.getHeight() - 10 }, 4); g.setColor(Color.BLACK); g.drawPolygon(new int[] { 10, 40, this.getWidth() - 10, this.getWidth() - 30 }, new int[] { this.getHeight() - 10, this.getHeight() - 30, this.getHeight() - 30, this.getHeight() - 10 }, 4); g.setColor(new Color(100, 100, 100, 20)); g.fillPolygon(new int[] { 10, 40, this.getWidth() - 10, this.getWidth() - 30 }, new int[] { this.getHeight() - 10, this.getHeight() - 30, this.getHeight() - 30, this.getHeight() - 10 }, 4); } private void paintHistogram(Graphics g, int[] b, String[] str, Color[] color) { int hWidth = this.getWidth(); int hHeight = this.getHeight(); FontMetrics metrics = this .getFontMetrics(new Font(null, Font.PLAIN, 12)); setSize(calculate(hHeight, str, b, metrics)); paintBase(g); // 画基平面 int len = b.length; for (int i = 0; i < len; ++i) { // //画比例 g.setColor(trans(color[i], 255)); Font font = new Font(null, Font.PLAIN, 12); g .drawString(b[i] + "", (20 + (histogramPitch + histogramWidth + (maxStrWidth >> 1)) * i) + ((histogramWidth - metrics.stringWidth(String .valueOf(b[i]))) >> 1), (int) (this .getHeight() - 25 - b[i] * scaling)); g.setColor(trans(color[i], 150)); int x = histogramWidth + (histogramWidth + histogramPitch + (maxStrWidth >> 1)) * i; // //画上面 if (i == 0) x += 10; // 画第一个立方方柱时 g.drawPolygon(new int[] { x, 5 + x, 20 + x, 15 + x }, new int[] { (int) (this.getHeight() - 15 - b[i] * scaling), (int) (this.getHeight() - 25 - b[i] * scaling), (int) (this.getHeight() - 25 - b[i] * scaling), (int) (this.getHeight() - 15 - b[i] * scaling) }, 4); g.fillPolygon(new int[] { x, 5 + x, 20 + x, 15 + x }, new int[] { (int) (this.getHeight() - 15 - b[i] * scaling), (int) (this.getHeight() - 25 - b[i] * scaling), (int) (this.getHeight() - 25 - b[i] * scaling), (int) (this.getHeight() - 15 - b[i] * scaling) }, 4); // 画左侧面// // g.setColor(trans(color[i], 20)); // g.drawPolygon(new int[] { 15 + 25 * i, 20 + 25 * i, 20 + 25 * i, // 15 + 25 * i }, new int[] { // this.getHeight() - 15 - b[i], // this.getHeight() - 25 - b[i], this.getHeight() - 25, // this.getHeight() - 15 }, 4); // g.setColor(trans(color[i], 20)); // g.fillPolygon(new int[] { 15 + 25 * i, 20 + 25 * i, 20 + 25 * i, // 15 + 25 * i }, new int[] { // this.getHeight() - 15 - b[i], // this.getHeight() - 25 - b[i], this.getHeight() - 25, // this.getHeight() - 15 }, 4); 画后面/ // g.setColor(trans(color[i], 20)); // g.drawRect(20 + 25 * i, this.getHeight() - 25 - b[i], 15, // b[i]); // g.fillRect(20 + 25 * i, this.getHeight() - 25 - b[i], 15, // b[i]); // 画前面部分/// g.drawRect(x, (int) (this.getHeight() - 15 - b[i] * scaling), histogramWidth, (int) (b[i] * scaling)); // System.out.println((int) (b[i] * scaling)); g.fillRect(x, (int) (this.getHeight() - 15 - b[i] * scaling), histogramWidth, (int) (b[i] * scaling)); // 画右侧面/ g.drawPolygon(new int[] { 15 + x, 20 + x, 20 + x, 15 + x }, new int[] { (int) (this.getHeight() - 15 - b[i] * scaling), (int) (this.getHeight() - 25 - b[i] * scaling), this.getHeight() - 25, this.getHeight() - 15 }, 4); g.fillPolygon(new int[] { 15 + x, 20 + x, 20 + x, 15 + x }, new int[] { (int) (this.getHeight() - 15 - b[i] * scaling), (int) (this.getHeight() - 25 - b[i] * scaling), this.getHeight() - 25, this.getHeight() - 15 }, 4); g.setColor(trans(color[i], 255)); g.drawString(str[i] + "", (5 + x) + ((histogramWidth - this.getFontMetrics(font).stringWidth( str[i])) >> 1), this.getHeight() - 2); } } /** * 给颜色加上透明度 * * @param color * @param alpha * 透明值(0-256) * @return Color * */ private Color trans(Color color, int alpha) { int rgb = color.getRGB(); int r = (rgb >> 16) & 0xff; int g = (rgb >> 8) & 0xff; int b = rgb & 0xff; return new Color(r, g, b, alpha); } private Dimension calculate(int h, String[] str, int[] b, FontMetrics metrics) { int len = b.length; int max = -1; for (int i = 0; i < len; ++i) { if (b[i] > max) max = b[i]; } int height = h; if (max > this.getHeight()) { scaling = (float) this.getHeight() / (max + 300); // 加了个任意数300, // 是为了让立方柱的高度始终小于整个applet的高度 } len = str.length; for (int i = 0; i < len; ++i) { if (metrics.stringWidth(str[i]) > maxStrWidth) maxStrWidth = metrics.stringWidth(str[i]); } int width = b.length * ((maxStrWidth >> 1) + histogramPitch + histogramWidth) + 20; // System.out.println(width); // return new Dimension(width, height+20);//如果这样写会有个很奇怪的现象出现,不知道为什么?郁闷..... return new Dimension(width, height); } }