一。
饼图
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.math.*;
import java.util.*;
import java.awt.geom.Arc2D;
import java.awt.geom.*;
import java.awt.Polygon;
import java.util.Date;
public class CakeChart extends JFrame {
// 初始数据
private double data[] = { 100., 150, 113., 127., 299.234, 120., 93., 123.,
127., 199., 157., 189. };
private String percent[] = new String[data.length]; // 每个数据所占百分比
private int radian[] = new int[data.length]; // 每个数据所对应的弧度数
private double max, min;
// 初始化每块饼的颜色
private int dia = 240;// 设置饼图透明度
private Color c1 = new Color(0, 255, 0, dia);
private Color c2 = new Color(255, 255, 0, dia);
private Color c3 = new Color(255, 0, 0, dia);
private Color c4 = new Color(255, 128, 64, dia);
private Color c5 = new Color(255, 128, 255, dia);
private Color c6 = new Color(255, 0, 128, dia);
private Color c7 = new Color(233, 124, 24, dia);
private Color c8 = new Color(204, 119, 115, dia);
private Color c9 = new Color(89, 159, 230, dia);
private Color c10 = new Color(148, 140, 179, dia);
private Color c11 = new Color(128, 0, 64, dia);
private Color c12 = new Color(174, 197, 208, dia);
private Color colors[] = { c1, c2, c3, c12, c11, c6, c4, c8, c9, c10, c5,
c7 };
// 统计图的宽度和高度
private int width = 800;
private int height = 450;
// 定义标题起始坐标变量
private int titleStart_x;
private int titleStart_y;
// 定义圆心坐标
private int oval_x = 60;
private int oval_y = 90;
// 定义椭圆的长轴和短轴
private int long_axes = 280;
private int short_axes = 200;
// 定义图例区域起点坐标
private int cutlineRect_x = 70;
private int cutlineRect_y = 450;
// 定义图倒区域矩形的宽度和高度
private int cutlineRect_width = 100;
private int cutlineRect_heigth = 235;
// 定义日期变量
private Date date = new Date();
// 定义统计图其他显示信息的超始坐标
private int otherInfo_x1;
private int otherInfo_y1;
private int otherInfo_x2;
private int otherInfo_y2;
private int otherInfo_x3;
private int otherInfo_y3;
// 定义标题\X\Y轴信息
private String title = "服务器流量统计 (饼形统计图) ";
private String otherInfo1 = "很经典的的一个统计饼图!!";
private String otherInfo2 = "www.eduwest.com";
private String otherInfo3 = "绘图日期:";
// 双缓冲设置
private Image offScreenImage = null;
private Graphics offScreenBuffer = null;
public CakeChart() {
super("数量统计饼图");
setSize(800, 500);
setLocation(100, 177);
setResizable(true);
setVisible(true);
// setBackground(Color.pink);
init();
}
public void init() {
offScreenImage = this.createImage(width, height);
offScreenBuffer = offScreenImage.getGraphics();
NumberBudget();
CoorBudget();
}
public void NumberBudget() {
// 求数据中的最大值和最小值
max = data[0];
min = data[0];
for (int mm = 0; mm < data.length; mm++) {
if (data[mm] > max)
max = data[mm];
if (data[mm] < min)
min = data[mm];
}
// 对数据进行求和运算
float allData_sum = 0;
for (int s = 0; s < data.length; s++) {
allData_sum += data[s];
}
// 计算每个数据占总数的百分比
for (int p = 0; p < data.length; p++) {
percent[p] = String
.valueOf(Math.round(data[p] / allData_sum * 100))
+ "%";
}
// 计算每个数据所对应的弧度数
for (int r = 0; r < data.length; r++) {
radian[r] = Math.round((float) data[r] / allData_sum * 360);
}
}
public void CoorBudget() {
// 预算标题信息的起始坐标
titleStart_x = 22;
titleStart_y = (width / 2) - (title.length() * 15 / 2);
// 初始化统计图其他显示信息的起始坐标(位置固定)
otherInfo_x1 = 390;
otherInfo_y1 = 450;
otherInfo_x2 = 400;
otherInfo_y2 = 480;
otherInfo_x3 = 390;
otherInfo_y3 = 5;
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
PaintBackground(offScreenBuffer);
PaintChart(offScreenBuffer);
g.drawImage(offScreenImage, 0, 0, this);
}
public void PaintBackground(Graphics g) {
// 渐变背景初始颜色
Color BackStartColor = Color.white;
Color BackLastColor = new Color(162, 189, 230);
Color titleColor = Color.black;
Color otherInfoColor = new Color(41, 78, 118);
// 标题背景颜色
Color titleBackColor = new Color(147, 179, 225);
// 统计图中心区域颜色
Color cutlineColor = new Color(0, 128, 255, 50);
// 图例数据颜色
Color cutDataColor = Color.white;
Font titleFont = new Font("黑体", Font.BOLD, 18);
Font otherFont = new Font("宋体", Font.PLAIN, 12);
String year = "";
String month = "";
String day = "";
Graphics2D g2 = (Graphics2D) g;
RenderingHints hints = new RenderingHints(null);
hints.put(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHints(hints);
GradientPaint gradient = new GradientPaint(0, 0, BackStartColor, 0,
400, BackLastColor, false);
g2.setPaint(gradient);
Rectangle2D rect = new Rectangle2D.Double(0, 0, width, height);
g2.fill(rect);
// 绘制标题背景
g2.setColor(titleBackColor);
g2.fill3DRect(0, 0, width, 30, true);
// 绘制图例区域背景
g2.setColor(cutlineColor);
g2.fillRect(cutlineRect_y, cutlineRect_x, cutlineRect_width,
cutlineRect_heigth);
g2.setColor(Color.white);
g2.drawRect(cutlineRect_y, cutlineRect_x, cutlineRect_width,
cutlineRect_heigth);
// 绘制统计图标题
g2.setFont(titleFont);
g2.setColor(titleColor);
g2.drawString(title, titleStart_y, titleStart_x);
// 显示绘制日期
year = Integer.toString(1900 + date.getYear());
month = Integer.toString(date.getMonth() + 1);
day = Integer.toString(date.getDate());
g2.drawString(year + "年" + month + "月" + day + "日", otherInfo_y3 + 60,
otherInfo_x3);
// 显示数据百分比
int colorRectWH = 15;
int space = 5; // 图例中小色块之间的间隔距离
int addData = cutlineRect_x;
for (int i = 0; i < data.length; i++) {
g2.setColor(colors[i]);
g2.fill3DRect(cutlineRect_y, addData, colorRectWH, colorRectWH,
true);
if (data[i] == max || data[i] == min)
g2.setColor(Color.red);
else
g2.setColor(cutDataColor);
g2.drawString(String.valueOf(data[i]) + " (" + percent[i] + ")",
cutlineRect_y + 20, addData + colorRectWH - space);
addData += colorRectWH + space;
}
}
public void PaintChart(Graphics g) {
int start = 0;
int rVal = 192;
int gVal = 192;
int bVal = 192;
int frameCount = 60;
Graphics2D g2 = (Graphics2D) g;
RenderingHints hints = new RenderingHints(null);
hints.put(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHints(hints);
for (int t = 20; t >= 0; t--) {
rVal = t * 255 / frameCount;
gVal = t * 255 / frameCount;
bVal = t * 255 / frameCount;
g2.setColor(new Color(rVal, gVal, bVal, 50));
g2.drawOval(oval_x, oval_y + t, long_axes, short_axes);
}
for (int a = 0; a < data.length; a++) {
Arc2D arc = new Arc2D.Float(Arc2D.PIE);
g2.setColor(colors[a]);
arc.setFrame(oval_x, oval_y, long_axes, short_axes);
arc.setAngleStart(start);
arc.setAngleExtent(radian[a]);
g2.fill(arc);
if (data[a] == max || data[a] == min)
g2.setColor(Color.white);
else
g2.setColor(new Color(223, 223, 223, 150));
g2.draw(arc);
start += radian[a];
}
}
public static void main(String[] arsg) {
try {
CakeChart cake = new CakeChart();
} catch (Exception exe) {
}
}
}
二。
图案
//Julia集放大
/*
最近在看分行算法这本书,才发现原来数学公式也能构造这么艺术的东西.都怪当年数学没好好学,看起来特费劲.
特发个applet程序给大家一起来看看
钟伟海([email protected])
*/
import java.awt. *;
import java.applet. *;
import java.awt.event. *;
public class Juliazoom extends Applet implements MouseListener , MouseMotionListener
{
private final int MAX = 256 ;
private final double SX = -1.0 ;
// 实部的最小值
private final double SY = -1.5 ;
// 虚部的最小值
private final double EX = 2.0 ;
// 实部的最大值
private final double EY = 1.5 ;
// 虚部的最大值
private static int picX , picY , xs , ys , xe , ye ;
private static double x1 , y1 , x2 , y2 , p , q ,xb ,yb ;
private static boolean action , rechteck , fertig , stopit ;
private static float xy ;
private Image bild ;
private Graphics g1 ;
private Cursor c1 , c2 ;
public void init () // 初始化所有实例
{
p = -0.46 ;
q = 0.57 ;
fertig = false ;
addMouseListener (this );
addMouseMotionListener (this );
c1 = new Cursor (Cursor.WAIT_CURSOR );
c2 = new Cursor (Cursor.CROSSHAIR_CURSOR );
picX = getSize ().width ;
picY = getSize ().height ;
xy = (float )picX / (float )picY ;
bild = createImage (picX , picY );
g1 = bild.getGraphics ();
fertig = true ;
} public void destroy () // 删除所有实例
{
if (fertig )
{
removeMouseListener (this );
removeMouseMotionListener (this );
bild = null ;
g1 = null ;
c1 = null ;
c2 = null ;
System.gc ();
}
} public void start ()
{
action = false ;
rechteck = false ;
stopit = false ;
startwerte ();
xb = (x2 - x1 ) / (double )picX ;
yb = (y2 - y1 ) / (double )picY ;
julia ();
} public void stop ()
{
} public void paint (Graphics g )
{
update (g );
} public void update (Graphics g )
{
g.drawImage (bild , 0 , 0 , this );
if (rechteck )
{
g.setColor (Color.white );
if (xs < xe )
{
if (ys < ye ) g.drawRect (xs , ys , (xe - xs ), (ye - ys ));
else g.drawRect (xs , ye , (xe - xs ), (ys - ye ));
} else
{
if (ys < ye ) g.drawRect (xe , ys , (xs - xe ), (ye - ys ));
else g.drawRect (xe , ye , (xs - xe ), (ys - ye ));
}
}
} private void julia () // 计算所有的点
{
int x , y ;
float h , b , alt = 0.0f ;
action = false ;
setCursor (c1 );
showStatus ("正在计算julia集,请等待");
p = -0.46 ;
q = 0.57 ;
for (x = 0 ; x < picX ; x +=2 )
{
for (y = 0 ; y < picY ; y ++)
{
h = punktfarbe (x1 + xb * (double )x , y1 + yb * (double )y ,p ,q );
// 颜色值
if (h != alt )
{
b = 1.0f - h * h ;
g1.setColor (Color.getHSBColor ( h ,1 ,b ));
alt = h ;
} g1.drawLine (x , y , x + 1 , y );
} showStatus ( "At " + x + " of " + picX );
if (stopit ) x = picX ;
} if (stopit )
{
showStatus ("Aborted");
stopit = false ;
} else
{
showStatus ("julia集已准备好,请用鼠标选择放大区域.");
} setCursor (c2 );
action = true ;
} // 由迭代产生的0.0到1.0的颜色值
private float punktfarbe (double x0 , double y0 ,double p ,double q )
{
double r = 0.0 , xk = 0 , yk = 0 ;
int j = 0 ;
while ((j < MAX ) && (r < 4.0 ))
{
xk = x0 * x0 - y0 * y0 + p ;
yk = 2.0 * x0 * y0 + q ;
j ++;
r = yk * yk + xk * xk ;
x0 = xk ;
y0 = yk ;
} return (float )j / (float )MAX ;
} private void startwerte () // 重新开始值
{
x1 = SX ;
y1 = SY ;
x2 = EX ;
y2 = EY ;
if ((float )((x2 - x1 ) / (y2 - y1 )) != xy ) x1 = x2 - (y2 - y1 ) * (double )xy ;
} public void mousePressed (MouseEvent e )
{
e.consume ();
if (action )
{
xs = e.getX ();
ys = e.getY ();
}
} public void mouseReleased (MouseEvent e )
{
int z , w ;
e.consume ();
if (action )
{
xe = e.getX ();
ye = e.getY ();
if (xs > xe )
{
z = xs ;
xs = xe ;
xe = z ;
} if (ys > ye )
{
z = ys ;
ys = ye ;
ye = z ;
} w = (xe - xs );
z = (ye - ys );
if ((w < 2 ) && (z < 2 )) startwerte ();
else
{
if (((float )w > (float )z * xy )) ye = (int )((float )ys + (float )w / xy );
else xe = (int )((float )xs + (float )z * xy );
x2 = x1 + xb * (double )xe ;
y2 = y1 + yb * (double )ye ;
x1 += xb * (double )xs ;
y1 += yb * (double )ys ;
} xb = (x2 - x1 ) / (double )picX ;
yb = (y2 - y1 ) / (double )picY ;
julia ();
rechteck = false ;
repaint ();
} else
{
stopit = true ;
}
} public void mouseEntered (MouseEvent e )
{
} public void mouseExited (MouseEvent e )
{
} public void mouseClicked (MouseEvent e )
{
} public void mouseDragged (MouseEvent e )
{
e.consume ();
if (action )
{
xe = e.getX ();
ye = e.getY ();
rechteck = true ;
repaint ();
}
} public void mouseMoved (MouseEvent e )
{
}
}
三。
五星红旗
/**
使用Java2D绘制五星红旗
五星红旗的绘制标准见百度百科: http://baike.baidu.com/view/5163.htm
注意: 该处对于第一颗小星的位置说明有误,应将“左五右十”改成“左十右五”
@author Eastsun
@version 2008/10/17 1.0
*/
import javax.swing.*;
import java.awt.geom.*;
import java.awt.*;
public class NationalFlag extends JPanel {
public static void main(String[] args) {
JFrame frame = new JFrame("五星红旗 By Eastsun");
frame.getContentPane().add(new NationalFlag(600));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
/**
* 创建一个五角星形状. 该五角星的中心坐标为(sx,sy),中心到顶点的距离为radius,其中某个顶点与中心的连线的偏移角度为theta(弧度)
*
* @return pentacle 一个☆
*/
public static Shape createPentacle(double sx, double sy, double radius,
double theta) {
final double arc = Math.PI / 5;
final double rad = Math.sin(Math.PI / 10) / Math.sin(3 * Math.PI / 10);
GeneralPath path = new GeneralPath();
path.moveTo(1, 0);
for (int idx = 0; idx < 5; idx++) {
path.lineTo((float)(rad * Math.cos((1 + 2 * idx) * arc)),(float) (rad
* Math.sin((1 + 2 * idx) * arc)));
path.lineTo((float)Math.cos(2 * (idx + 1) * arc), (float)Math.sin(2 * (idx + 1)
* arc));
}
path.closePath();
AffineTransform atf = AffineTransform.getScaleInstance(radius, radius);
atf.translate(sx / radius, sy / radius);
atf.rotate(theta);
return atf.createTransformedShape(path);
}
private int width, height;
private double maxR = 0.15, minR = 0.05;
private double maxX = 0.25, maxY = 0.25;
private double[] minX = { 0.50, 0.60, 0.60, 0.50 };
private double[] minY = { 0.10, 0.20, 0.35, 0.45 };
/**
* 创建一个宽度为width的国旗
*/
public NationalFlag(int width) {
this.width = width / 3 * 3;
this.height = width / 3 * 2;
setPreferredSize(new Dimension(this.width, this.height));
}
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
// 画旗面
g2d.setPaint(Color.RED);
g2d.fillRect(0, 0, width, height);
// 画大☆
double ox = height * maxX, oy = height * maxY;
g2d.setPaint(Color.YELLOW);
g2d.fill(createPentacle(ox, oy, height * maxR, -Math.PI / 2));
// 画小★
for (int idx = 0; idx < 4; idx++) {
double sx = minX[idx] * height, sy = minY[idx] * height;
double theta = Math.atan2(oy - sy, ox - sx);
g2d.fill(createPentacle(sx, sy, height * minR, theta));
}
}
}