Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 425|回复: 0

[Swing学习]读取HTML文档中的数据来绘制销售量统计饼图

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-29 23:55:41 | 显示全部楼层 |阅读模式
    1. 这是这个Applet的html文件:
    2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    3. <HTML>
    4. <BODY>
    5.         <APPLET CODE = "PieNew.class" WIDTH = "600" HEIGHT = "500">
    6.                 <PARAM NAME = "titleFontName" VALUE = "黑体">
    7.                 <PARAM NAME = "titleFontSize" VALUE = "30">
    复制代码

         <PARAM NAME = "chartTitle" VALUE = "Java Web图表设计Applet & TML版">
         <PARAM NAME = "bookTitle1" VALUE = "python">
    <PARAM NAME = "bookSales1" VALUE = "45">
         <PARAM NAME = "bookTitle2" VALUE = "java">
    <PARAM NAME = "bookSales2" VALUE = "115">

    <PARAM NAME = "bookTitle3" VALUE = "C#">
    <PARAM NAME = "bookSales3" VALUE = "70">
         <PARAM NAME = "bookTitle4" VALUE = "Perl">
    <PARAM NAME = "bookSales4" VALUE = "60">
         <PARAM NAME = "bookTitle5" VALUE = "PHP">
    <PARAM NAME = "bookSales5" VALUE = "35">
         <PARAM NAME = "subTitle" VALUE = "计算机编程类图书销售统计图">
         </APPLET>
    </BODY>
    </HTML>
      
        下面是源文件:
    // Fig. 3.03: PieNew.java
    // Java Applet Web图表实例5:
    // 通过读取HTML文档中的数据来绘制书籍销售量统计饼图
    import java.awt.*; // 引入 java.awt包中所有的类
    import javax.swing.*; // 引入 javax.swing包中所有的类
    import java.text.*; // 引入 java.text包中所有的类
    public class PieNew 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 = getParameter("bookTitle" + (i + 1));
                tempBookSales = getParameter("bookSales" + (i + 1));
                if (tempBookSales != null)
                {
                    bookSales = Integer.parseInt(tempBookSales);
                }
                else
                {
                    bookSales = 0;
                }
                totalSales += bookSales;
            }
            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 * 360 / (float)totalSales + 0.5);
                proportion = ((float)bookSales) / totalSales * 100;
                offGraphics.setColor(color);
                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, 420, 167+i * 50);
                offGraphics.drawString("" + bookSales, 490, 167+i * 50);
                offGraphics.drawString(twoDigits.format(proportion) + "%", 540, 167
                    +i * 50);
            }
            // 输出缓冲区图像
            g.drawImage(offImage, 0, 0, null);
        }
    } //  PieNew 类结束
    /**************************************************************************
    * (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.                                 *
    **************************************************************************/

                                                  [/code]
       
      
       
       

         
       

         
       
      
      
      程序运行图:
       
      
         

      
      
       
       

         
       

         
       
      
       

      



                            function TempSave(ElementID)
                            {
                                    CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
                                    CommentsPersistDiv.save("CommentXMLStore");
                            }
                            function Restore(ElementID)
                            {
                                    CommentsPersistDiv.load("CommentXMLStore");
                                    document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
                            }
                   
                      











    源码下载:http://www.hnzz3z.com:8103/zz3zcwb/cwb/dir5/Fig3.3.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-26 01:18 , Processed in 0.320178 second(s), 36 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表