`
wuzijingaip
  • 浏览: 337353 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

java 画饼图和图案

阅读更多
一。
饼图
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程序给大家一起来看看
   钟伟海(newwei2001@yahoo.com.cn)
  */
  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));
		}
	}
}

分享到:
评论

相关推荐

    js 画饼图 javascript

    打开这个文件,你可以看到实际的代码实现和效果,进一步学习和理解JS画饼图的技巧。 总的来说,JS画饼图涉及到数据可视化、图形绘制以及可能的用户交互。通过选择合适的库或利用原生的HTML5特性,我们可以创建出...

    asp实现画饼图功能

    asp实现画饼图功能,很有用哦,asp实现画饼图功能,很有用哦

    android achat画饼图和柱图

    "android achat画饼图和柱图"这个项目就是针对这一需求,提供了在Android平台上绘制饼图和柱状图的功能。它基于对`chatengine`库中的柱图和饼图组件的复用和优化,为开发者提供了一套简洁且高效的图表解决方案。 ...

    Android 画饼图

    本项目"Android 画饼图"旨在提供一个立体和平面效果的饼图组件,适用于各种需要展示部分与整体之间关系的应用场景。下面将详细介绍如何在Android中实现饼图,以及相关的关键知识点。 首先,我们要理解饼图的基本...

    Java接月饼小游戏.zip

    Java接月饼小游戏Java接月饼小游戏Java接月饼小游戏 Java接月饼小游戏Java接月饼小游戏Java接月饼小游戏 Java接月饼小游戏Java接月饼小游戏Java接月饼小游戏 Java接月饼小游戏Java接月饼小游戏Java接月饼小游戏 Java...

    Java代码实例-画饼状图java实例.rar

    在这个"Java代码实例-画饼状图java实例.rar"压缩包中,包含了一个名为"画饼状图java实例.txt"的文件,它提供了具体的Java代码示例,帮助我们理解如何实现这一功能。 首先,我们需要了解Java中的图形库。Java提供了...

    html5画饼图

    画饼图 使用html5加javascript技术生成饼图 简单容易理解

    piechart.zip_Labview饼状图_labview piechart_labview 画饼图_piechart la

    标题中的"piechart.zip_Labview饼状图_labview piechart_labview 画饼图_piechart lab"暗示了这个压缩包包含了一个关于在LabVIEW中创建和使用饼状图的实例或教程。描述提到"LabVIEW中实现饼图显示,提供比较好的人机...

    【ASP.NET】按比例画饼图

    【ASP.NET】按比例画饼图 一个很简单的实例

    数据分析,画饼图的jupyter notebook

    在数据分析中,可视化是一种强大的工具,可以帮助我们更好地理解数据的分布和特征。Jupyter Notebook 是一个广泛使用的交互式编程环境,特别适合进行数据分析和可视化。在这个场景中,我们将讨论如何使用Jupyter ...

    C#354-画饼图组件源代码

    在C#编程中,画饼图是一种常用的可视化方式,它能直观地展示数据的比例和分布。本资源"**C#354-画饼图组件源代码**"提供了一个用于创建饼图的自定义组件,这对于我们进行数据分析、报告展示或者用户界面设计都非常有...

    C# 画饼形图,条形图

    C# 画饼形图,条形图

    android画饼图

    "android画饼图"这个主题主要涉及到如何在Android应用中自定义一个饼状图控件。在这个项目中,开发者分享了其在GitHub上的源代码,供其他开发者学习和使用。 1. **自定义控件**:在Android中,如果系统自带的图表库...

    画饼状图java实例

    本文将深入探讨如何使用Java绘制饼状图,这是一个实用且基础的技能,能够帮助开发者更好地呈现数据。 ### 知识点一:饼状图的基本概念 饼状图是一种用于展示数据比例的图形表示方法,它将一个圆形分割成若干个扇形...

    MFC如何画饼图

    对于饼图的美观,可以考虑使用渐变填充或者图案填充。 7. **添加标签和百分比** - 为了增加可读性,可以在每个扇形外添加标签显示其对应的值或百分比。可以使用TextOut()函数在合适的位置绘制文本。 8. **交互...

    用CSS画饼图

    用CSS绘制饼图,针对移动端上的展示,建议在谷歌浏览器上打开效果更佳,上传了两个html文件,pie.html是css静态画饼图,pieAuto.html是用我自己写的插件可以根据传入的参数动态绘制饼图

    javascript画饼图

    JavaScript 画饼图是一种在网页上可视化数据的常见方法,特别是在数据分析和信息展示中。饼图可以直观地表示各部分占整体的比例,非常适合用来展示各项数据的占比情况。在这个话题中,我们将深入探讨如何使用纯 ...

    利用VB画饼状比例图

    在VB(Visual Basic)编程环境中,我们可以利用图形用户界面(GUI)和内置的绘图功能来创建各种图表,包括饼状比例图。饼状图是一种常见的数据可视化工具,用于显示不同部分在一个整体中的相对大小。在VB中实现这个...

Global site tag (gtag.js) - Google Analytics