Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1522310
  • 博文数量: 290
  • 博客积分: 3468
  • 博客等级: 中校
  • 技术积分: 3461
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-28 22:21
文章分类

全部博文(290)

文章存档

2016年(13)

2015年(3)

2014年(42)

2013年(67)

2012年(90)

2011年(75)

分类: Java

2012-04-28 10:59:20

这是这个Applet的html文件:








下面的画垂直柱状图的源码:

点击(此处)折叠或打开

  1. // Fig. 3.02_01: LogoVBar.java
  2. // Java Applet Web图表实例3:带Java徽标的Web图表

  3. import java.awt.*; // 引入 java.awt包中所有的类
  4. import javax.swing.*; // 引入 javax.swing包中所有的类
  5. public class LogoVBar extends JApplet
  6. {
  7.   Image logoJPG;
  8.   Image offImage;
  9.   Graphics offGraphics;
  10.   int appletWidth = 600, appletHeight = 500;
  11.   int bookSales[] = new int[5];
  12.   String bookTitle[] =
  13.   {
  14.     "Python", "JAVA", "C#", "Perl", "PHP"
  15.   };
  16.   // 初始化颜色数组
  17.   Color color[] =
  18.   {
  19.     new Color(99, 99, 0), Color.GREEN, Color.YELLOW, Color.RED, Color.BLUE
  20.   };
  21.   // 初始化绘图缓冲区
  22.   public void init()
  23.   {
  24.     offImage = createImage(appletWidth, appletHeight);
  25.     offGraphics = offImage.getGraphics();
  26.     for (int i = 0; i < bookSales.length; i++)
  27.     {
  28.       bookSales[i] = 1+(int)(Math.random() * 100);
  29.     }
  30.     logoJPG = getImage(getDocumentBase(), "javaLogo.jpg");
  31.   }
  32.   public void paint(Graphics g)
  33.   {
  34.     // 调用父类的 paint 方法
  35.     super.paint(g);
  36.     update(g);
  37.   } // paint 方法结束
  38.   public void update(Graphics g)
  39.   {
  40.     // 绘制标题区域
  41.     offGraphics.drawImage(logoJPG, 100, 0, this);
  42.     offGraphics.setColor(Color.BLACK);
  43.     offGraphics.setFont(new Font("方正粗宋简体", Font.BOLD, 30));
  44.     offGraphics.drawString("带Java徽标的Web图表", 135, 40);
  45.     // 绘制代表销售量的10条直线
  46.     offGraphics.setFont(new Font("SansSerif", Font.PLAIN, 12));
  47.     int salesValue = 0;
  48.     for (int i = 418; i > 0; i -= 38)
  49.     {
  50.       offGraphics.setColor(Color.BLACK);
  51.       offGraphics.drawString("" + salesValue, 40, (i + 27));
  52.       offGraphics.setColor(Color.LIGHT_GRAY);
  53.       offGraphics.drawLine(80, (i + 27), 520, (i + 27));
  54.       salesValue += 10;
  55.     }
  56.     // 绘制实心矩形    
  57.     int drawHigh = 0;
  58.     for (int i = 0; i < bookTitle.length; i++)
  59.     {
  60.       offGraphics.setColor(color[i]);
  61.       drawHigh = (int)(Math.ceil(bookSales[i] * 3.8));
  62.       offGraphics.fill3DRect(110+i * 80, 445-drawHigh, 50, drawHigh, true);
  63.       offGraphics.setColor(Color.BLACK);
  64.       offGraphics.drawString(bookTitle[i], 110+i * 80, 465);
  65.     }
  66.     // 绘制坐标系
  67.     offGraphics.setColor(Color.BLACK);
  68.     offGraphics.drawLine(80, 40, 80, 445);
  69.     offGraphics.drawLine(80, 445, 550, 445);
  70.     // 绘制坐标系说明
  71.     offGraphics.setFont(new Font("黑体", Font.BOLD, 16));
  72.     offGraphics.drawString("销售量", 20, 50);
  73.     offGraphics.drawString("编程书籍", 500, 465);
  74.     // 输出缓冲区图像
  75.     g.drawImage(offImage, 0, 0, null);
  76.   }
  77. }




原文链接:
阅读(1488) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~