其中一个小程序; package org.zhq426; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JDialog; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.*; public class DrawState extends JDialog { int result[] = { }; private String[] title = { "优秀", "良好", "中等", "及格", "不及格" }; private Color[] color = { Color.blue, new Color(255, 92, 0), new Color(255, 0, 30), new Color(0, 255, 30), new Color(255, 0, 240), Color.cyan }; public static void main(String args[]) { try { DrawState dialog = new DrawState(); dialog.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); dialog.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } public DrawState() { super(); setBounds(100, 100, 500, 375); // } public DrawState(int result[]){ super(); this.result=result; setBounds(100, 100, 500, 375); } private int statStyle = 1; public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; Dimension dim = this.getSize(); g2.setColor(Color.white); g2.fillRect(0, 0, dim.width, dim.height); int total = result[result.length - 1]; int begTangle = 0; int endTangle = 0; g2.setColor(Color.black); int pointx = dim.width / 2;// 圆心x int pointy = dim.height / 2;// 圆心y int r = 150;// 半径 long[] pointsx = new long[result.length - 1]; long[] pointsy = new long[result.length - 1]; pointsx[0] = pointx + r; pointsy[0] = pointy; for (int i = 1; i < result.length - 1; i++) { if (i >= 6) { g2.setColor(color[i % 6]); } else { g2.setColor(color[i]); } if (result[i] > 0) { int percent = Math.round((result[i] * 100f) / total); // round()返回最接近参数的int值 endTangle = Math.round(percent * 360f / 100f); title[i] = title[i] + "(" + percent + "%)"; g2.fillArc(pointx - r, pointy - r, 2 * r, 2 * r, begTangle, endTangle); // 新中心点 pointsx[i] = Math.round(Math.cos(Math.toRadians (begTangle + endTangle * 0.5)) * r + pointx); pointsy[i] = Math.round(pointy - Math.sin(Math.toRadians (begTangle + endTangle * 0.5)) * r); // 延长点 long pointsxEx = Math.round(Math.cos(Math.toRadians (begTangle + endTangle * 0.5)) * (r + 50) + pointx); long pointsyEx = Math.round(pointy - Math.sin(Math.toRadians (begTangle + endTangle * 0.5)) * (r + 50)); g2.setColor(Color.black); g2.draw(new Line2D.Double(pointsx[i], pointsy[i], pointsxEx, pointsyEx)); // 画文本框 g2.setColor(new Color(251, 249, 206)); if (((int) pointsxEx) < pointx) { g2.fillRect((int) pointsxEx - 110, (int) pointsyEx - 10, 110, 20); g2.setColor(Color.black); g2.drawString(title[i], (int) pointsxEx - 110, (int) pointsyEx + 5); } else { g2.fillRect((int) pointsxEx, (int) pointsyEx - 10, 110, 20); g2.setColor(Color.black); g2.drawString(title[i], (int) pointsxEx, (int) pointsyEx + 5); } begTangle = begTangle + endTangle; } } } }
- 1
- 2
- 粉丝: 0
- 资源: 1
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助