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

📄 vbar.java

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

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

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

  int appletWidth = 600, appletHeight = 500;

  int bookSales[] = new int[5];

  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);
    }

  }

  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("方正粗宋简体", Font.BOLD, 30));
    offGraphics.drawString("Java Web图表设计Applet版--柱状图", 15, 30);

	// 绘制代表销售量的10条直线
	offGraphics.setFont(new Font("SansSerif", Font.PLAIN, 12));
    int salesValue = 0;
    for (int i = 418; i > 0; i -= 38)
    {
      offGraphics.setColor(Color.BLACK);
      offGraphics.drawString("" + salesValue, 40, (i + 27));

      offGraphics.setColor(Color.LIGHT_GRAY);
      offGraphics.drawLine(80, (i + 27), 520, (i + 27));
      
      salesValue += 10;
    }
	
    // 绘制实心矩形	
	int drawHigh = 0;

    for (int i = 0; i < bookTitle.length; i++)
    {
      offGraphics.setColor(color[i]);
      drawHigh = (int)(Math.ceil(bookSales[i] * 3.8));
      
      offGraphics.fill3DRect(110+i * 80, 445-drawHigh, 50, drawHigh, true);

      offGraphics.setColor(Color.BLACK);
      offGraphics.drawString(bookTitle[i], 110+i * 80, 465);
     }

    // 绘制坐标系
	offGraphics.setColor(Color.BLACK);
    offGraphics.drawLine(80, 40, 80, 445);
    offGraphics.drawLine(80, 445, 550, 445);
	
	// 绘制坐标系说明
	offGraphics.setFont(new Font("黑体", Font.BOLD, 16));
    offGraphics.drawString("销售量", 20, 50);
    offGraphics.drawString("编程书籍", 500, 465);

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

  }

} //  VBar 类结束

/**************************************************************************
 * (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 + -