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

📄 pie.java

📁 提供 精通Java Web动态图表编程 一书的源代码, 大家赶快下载呀
💻 JAVA
字号:
// Fig. 6.02_02_02: Pie.java
// Java JSP Web图表实例1:
// 通过读取HTML/JSP文档中的数据来绘制书籍销售量统计饼图

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 = new String[5];

    String[] tempBookSales = new String[5];

    String chartTitle, subTitle;

    String titleFontName;

    int titleFontSize = 20;

    // 初始化颜色数组
    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();
		
        titleFontName = getParameter("titleFontName");

        String tempFontSize = getParameter("titleFontSize");
        if (tempFontSize != null)
        {
            titleFontSize = Integer.parseInt(tempFontSize);
        }

        chartTitle = getParameter("chartTitle");

        for (int i = 0; i < 5; i++)
        {
            bookTitle[i] = getParameter("bookTitle" + (i + 1));
            tempBookSales[i] = getParameter("bookSales" + (i + 1));
            if (tempBookSales[i] != null)
            {
                bookSales[i] = Integer.parseInt(tempBookSales[i]);
            }
            else
            {
                bookSales[i] = 0;
            }
            totalSales += bookSales[i];
        }

        subTitle = getParameter("subTitle");
    }

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

    public void update(Graphics g)
    {
 
        // 绘制标题区域
        offGraphics.setColor(Color.BLACK);
        offGraphics.setFont(new Font(titleFontName, Font.BOLD, titleFontSize));
        offGraphics.drawString(chartTitle, 15, 30);
        offGraphics.drawString(subTitle, 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 + -