⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pie.java

📁 提供 精通Java Web动态图表编程 一书的源代码, 大家赶快下载呀
💻 JAVA
字号:
// Fig. 3.01_02: Pie.java
// Java Applet Web图表实例2:绘制书籍销售量统计图之饼图

import java.awt.*; // 引入 java.awt包中所有的类
import javax.swing.*; // 引入 javax.swing包中所有的类
import java.text.*; // 引入java.text包中所有的类

public class Pie extends JApplet
{
  Image offImage;
  Graphics offGraphics;

  int appletWidth = 600, appletHeight = 500;

  int bookSales[] = new int[5];

  int totalSales = 0;

  String bookTitle[] =
  {
    "Python", "JAVA", "C#", "Perl", "PHP"
  };

  // 初始化颜色数组
  Color color[] =
  {
    new Color(99, 99, 0), Color.GREEN, Color.YELLOW, Color.RED, Color.BLUE
  };

  // 初始化绘图缓冲区
  public void init()
  {
    offImage = createImage(appletWidth, appletHeight);
    offGraphics = offImage.getGraphics();

    for (int i = 0; i < bookSales.length; i++)
    {
      bookSales[i] = 1+(int)(Math.random() * 100);
      totalSales += bookSales[i];
    }

  }

  public void paint(Graphics g)
  {
    // 调用父类的 paint 方法
    super.paint(g);
    update(g);

  } // paint 方法结束

  public void update(Graphics g)
  {
    // 绘制背景
    offGraphics.setColor(new Color(144, 255, 255));
    offGraphics.fillRect(0, 0, appletWidth, appletHeight);
	
    // 绘制标题区域
    offGraphics.setColor(Color.BLACK);
    offGraphics.setFont(new Font("方正隶变简体", Font.BOLD, 30));
    offGraphics.drawString("Java Web图表设计Applet版--饼图", 15, 30);

    // 绘制图例
    offGraphics.drawString("计算机编程类图书销售统计表", 70, 465);
    offGraphics.setFont(new Font("SansSerif", Font.PLAIN, 12));
    offGraphics.drawString("编程类图书", 400, 125);
    offGraphics.drawString("销售数量", 475, 125);
    offGraphics.drawString("所占比例", 535, 125);

    offGraphics.setColor(Color.blue);
    offGraphics.drawRect(395, 95, 190, 300);

    // 绘制实心圆弧	
    int arcStartAngle = 30, arcAngle = 0;
    float proportion;
    DecimalFormat twoDigits = new DecimalFormat("0.00");

    for (int i = 0; i < bookTitle.length; i++)
    {
      arcAngle = (int)(bookSales[i] * 360 / (float)totalSales + 0.5);
      proportion = ((float)bookSales[i]) / totalSales * 100;

      offGraphics.setColor(color[i]);

	  if (i < bookTitle.length - 1)
      {
        offGraphics.fillArc(15, 95, 350, 300, arcStartAngle, arcAngle);
      }
      else
      {
        offGraphics.fillArc(30, 93, 350, 300, arcStartAngle, arcAngle);
      }

      arcStartAngle += arcAngle;

      offGraphics.fillRect(400, 155+i * 50, 12, 12);
      offGraphics.setColor(Color.black);
      offGraphics.drawString(bookTitle[i], 420, 167+i * 50);
      offGraphics.drawString("" + bookSales[i], 490, 167+i * 50);
      offGraphics.drawString(twoDigits.format(proportion) + "%", 540, 167+i * 50);
    }

    // 输出缓冲区图像
    g.drawImage(offImage, 0, 0, null);

  }

} //  Pie 类结束

/**************************************************************************
 * (C) Copyright 2004-2005 by Jingkui Zhong(钟京馗) and Huan Tang(唐桓).  *
 * All Rights Reserved.                                                   *
 *                                                                        *
 * DISCLAIMER: The authors of this code have used their                   *
 * best efforts in preparing the code. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these codes. The authors *
 * shall not be liable in any event for incidental or consequential       *
 * damages in connection with, or arising out of, the furnishing,         *
 * performance, or use of these programs.                                 *
  **************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -