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

📄 drawbufferedcylinder.java

📁 提供 精通Java Web动态图表编程 一书的源代码, 大家赶快下载呀
💻 JAVA
字号:
// Fig. 2.11_03: DrawCylinderApplet.java
// 采用双缓冲技术绘制圆柱体

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

public class DrawBufferedCylinder extends JApplet
{
  int rectX = 10, rectY = 120;
  int rectWidth = 100, rectHeight = 250;
  int ovalHeight = 40;
  int ovalX1, ovalY1, ovalX2, ovalY2, ovalWidth;
  Color outlineColor = Color.LIGHT_GRAY;
  Color fillColor = Color.RED;

  Image offImage;
  Graphics offGraphics;

  int appletWidth = 370, appletHeight = 420;

  // 初始化绘图缓冲区
  public void init()
  {
    offImage = createImage(appletWidth, appletHeight);
    offGraphics = offImage.getGraphics();
  }

  // 计算椭圆的绘制参数
  public void calculateOvalCoordinate()
  {
    ovalWidth = this.rectWidth;
    ovalX1 = this.rectX;
    ovalY1 = this.rectY - ovalHeight / 2;
    ovalX2 = this.rectX;
    ovalY2 = ovalY1 + rectHeight;
  }

  // 设置矩形左上角的坐标
  public void resetRectCoordinate(int rectX, int rectY)
  {
    this.rectX = rectX;
    this.rectY = rectY;
  }

  // 绘制圆柱体
  public void drawCylinder(Graphics g)
  {
    // 计算椭圆的坐标
    calculateOvalCoordinate();

    // 绘制下方椭圆
    g.setColor(fillColor);
    g.fillOval(ovalX2, ovalY2, ovalWidth, ovalHeight);
    g.setColor(outlineColor);
    g.drawOval(ovalX2, ovalY2, ovalWidth, ovalHeight);

    // 绘制中间的矩形
    g.setColor(fillColor);
    g.fillRect(rectX, rectY, rectWidth, rectHeight);

    // 绘制上方的椭圆
    g.setColor(g.getColor().darker());
    g.fillOval(ovalX1, ovalY1, ovalWidth, ovalHeight);
    g.setColor(outlineColor);
    g.drawOval(ovalX1, ovalY1, ovalWidth, ovalHeight);
  }

  public void paint(Graphics g)
  {
    update(g);
  } // paint 方法结束

  public void update(Graphics g)
  {
    offGraphics.setColor(Color.BLACK);
    offGraphics.setFont(new Font("汉真广标", Font.PLAIN, 35));
    offGraphics.drawString("圆柱体画法", 10, 40);

    //调用drawCylinder方法,绘制圆柱体
    drawCylinder(offGraphics);

    rectHeight = 350;
    resetRectCoordinate(250, 50);

    fillColor = Color.BLUE;

    // 调用drawCylinder方法,绘制圆柱体
    drawCylinder(offGraphics);

    g.drawImage(offImage, 0, 0, null);
  }


} // DrawBufferedCylinder 类结束

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