📄 pie3d.java
字号:
package pie;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.text.*;
public class Pie3D {
private int[] data;
private String month;
private String[] months = new String[]{"一","二","三","四","五","六"};
private BufferedImage createImage() {
int width = 500, height = 375;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
//打开反锯齿功能
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//填充整个背景
g2d.setPaint(Color.WHITE);
g2d.fillRect(0, 0, width, height);
//绘制渐进圆角矩形阴影
GradientPaint grayGP = new GradientPaint(0, 0, Color.GRAY, width, height, new Color(218, 214, 212), false);
g2d.setPaint(grayGP);
g2d.fillRoundRect(5, 5, width-5, height-5, 50, 50);
//绘制渐进圆角矩形背景
GradientPaint yellowGP = new GradientPaint(width, 0, new Color(252, 197, 113), width/2, height/2, new Color(255, 255, 169), true);
g2d.setPaint(yellowGP);
g2d.fillRoundRect(0, 0, width-5, height-5, 50, 50);
//绘制圆角矩形轮廓
BasicStroke bs = new BasicStroke(1.0f);
g2d.setStroke(bs);
g2d.setPaint(new Color(55,71, 105));
g2d.drawRoundRect(0, 0, width-5, height-5, 50, 50);
g2d.drawRoundRect(390, 47, 95, 65, 15, 15);
//绘制图表标题
String chartTitle = month + "月份销售统计图";
g2d.setColor(Color.BLACK);
g2d.setFont(new Font("宋体", Font.PLAIN, 30));
int stringLength = g2d.getFontMetrics().stringWidth(chartTitle);
g2d.drawString(chartTitle, (width-stringLength)/2, 30 );
//定义圆弧
Arc2D.Double arc2d = new Arc2D.Double();
double startAngle = 30.0, arcAngle = 0.0;
double rectWidth = 295.0, rectHeight = 245.0;
Point2D.Double p2d = new Point2D.Double((width - rectWidth)/2, 70.0);
int arcType = Arc2D.PIE;
//声明绘制数据
String bookTitle[] = {"JAVA", ".NET", "C", "PB", "PASCAL"};
Color color[] = new Color[5];
color[0] = new Color(99, 99, 0);
color[1] = new Color(255,169,66);
color[2] = new Color(33, 255, 66);
color[3] = new Color(33, 0, 255);
color[4] = new Color(255, 0, 66);
double bookSales[] = new double[5];
double totalSales = 0.0;
double proportion = 0.0;
for (int i = 0; i< bookSales.length; i++) {
bookSales[i] = data[i+1];
totalSales += bookSales[i];
}
//定义厚度
int thickness = 25;
//绘制起始角度不大于90度的圆弧
startAngle = 30;
for (int i = 0 ; i < bookTitle.length; i++) {
arcAngle = bookSales[i]*360 / totalSales;
if (startAngle >= 270) break;
for (int j= thickness; j> 0; j--) {
proportion = bookSales[i]/totalSales * 100 ;
g2d.setColor( color[i].darker() );
arc2d = new Arc2D.Double(p2d.x, p2d.y + j, rectWidth, rectHeight, startAngle ,arcAngle, arcType);
g2d.fill( arc2d) ;
}
// 更新圆弧的起始角度
startAngle += arcAngle;
}
// 绘制起始角度不小于270度的圆弧
startAngle = 390;
for(int i = bookTitle.length -1 ; i >0 ; i--) {
arcAngle = bookSales[i]*360 / totalSales;
if (startAngle <= 270 ) break;
for (int j= thickness; j> 0; j--) {
proportion = bookSales[i]/totalSales * 100 ;
g2d.setColor( color[i].darker() );
arc2d = new Arc2D.Double(p2d.x, p2d.y + j, rectWidth, rectHeight, startAngle, -arcAngle, arcType);
g2d.fill( arc2d) ;
}
//更新圆弧的起始角度
startAngle -= arcAngle;
}
//绘制最上面一层圆弧
startAngle = 30;
for(int i = 0 ; i < bookTitle.length; i++) {
arcAngle = bookSales[i]*360 / totalSales;
proportion = bookSales[i]/totalSales * 100 ;
g2d.setColor(color[i]);
arc2d = new Arc2D.Double(p2d.x, p2d.y , rectWidth, rectHeight, startAngle, arcAngle, arcType);
g2d.fill(arc2d) ;
//描绘圆弧轮廓
g2d.setStroke(new BasicStroke(1.2f));
g2d.setPaint(Color.GRAY);
g2d.draw(arc2d);
// 更新圆弧的起始角度
startAngle += arcAngle;
// 格式化浮点数的输出格式
DecimalFormat twoDigits = new DecimalFormat("0.00");
g2d.setFont(new Font("Courier New", Font.PLAIN, 12));
// 重新关闭反锯齿功能,用于绘制饼图的说明文字
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
// 绘制说明文字
drawTips(bookTitle[i] + " " + twoDigits.format(proportion) + "%", Color.BLACK, arc2d, g2d);
g2d.setColor(color[i]);
g2d.fillRect(400, 50+i * 12, 10, 10);
g2d.setColor(Color.BLACK);
g2d.drawString(bookTitle[i], 415, 60+i * 12);
g2d.drawString(String.valueOf(data[i+1]), 460, 60+i * 12);
// 重新打开反锯齿功能
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}
return image;
}
private void drawTips(String tips, Color color, Arc2D.Double arc2d, Graphics2D g2d) {
Arc2D.Double position = arc2d;
position.setAngleExtent(arc2d.getAngleExtent()/2);
position.x = arc2d.x - 15;
position.y = arc2d.y - 15;
position.width = arc2d.getWidth() + 50;
position.height = arc2d.getHeight() + 50;
Point2D.Double endPoint = (Point2D.Double)position.getEndPoint();
g2d.setPaint(color);
int stringLength = g2d.getFontMetrics().stringWidth(tips);
if (endPoint.x <= arc2d.getCenterX())
g2d.drawString(tips, (float)endPoint.x-stringLength, (float)endPoint.y);
else
g2d.drawString(tips, (float)endPoint.x, (float)endPoint.y );
}
public BufferedImage getImage() {
return createImage();
}
public void setData(int[] da) {
data = da;
if (data[0] == 1) month = months[0];
else if (data[0] == 2) month = months[1];
else if (data[0] == 3) month = months[2];
else if (data[0] == 4) month = months[3];
else if (data[0] == 5) month = months[4];
else if (data[0] == 6) month = months[5];
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -