TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
这是这个Applet的HTML文件:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<BODY>
<APPLET CODE = "LogoVBar.class" WIDTH = "600" HEIGHT = "500">
</APPLET>
</BODY>
</HTML>
下面的画垂直柱状图的源码:
- // Fig. 3.02_01: LogoVBar.java
- // Java Applet Web图表实例3:带Java徽标的Web图表
- import java.awt.*; // 引入 java.awt包中所有的类
- import javax.swing.*; // 引入 javax.swing包中所有的类
- public class LogoVBar extends JApplet
- {
- Image logoJPG;
- 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);
- }
- logoJPG = getImage(getDocumentBase(), "javaLogo.jpg");
- }
- public void paint(Graphics g)
- {
- // 调用父类的 paint 方法
- super.paint(g);
- update(g);
- } // paint 方法结束
- public void update(Graphics g)
- {
- // 绘制标题区域
- offGraphics.drawImage(logoJPG, 100, 0, this);
- offGraphics.setColor(Color.BLACK);
- offGraphics.setFont(new Font("方正粗宋简体", Font.BOLD, 30));
- offGraphics.drawString("带Java徽标的Web图表", 135, 40);
- // 绘制代表销售量的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);
- }
- } // LogoVBar 类结束
复制代码 /*************************************************************
(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. *
****************************************************************/
源码下载:http://file.javaxxz.com/2014/10/29/235545328.zip |
|