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

📄 drawtriangleapplet.java

📁 提供 精通Java Web动态图表编程 一书的源代码, 大家赶快下载呀
💻 JAVA
字号:
// Fig. 2.10_04: DrawTriangleApplet.java
// 绘制等腰三角形

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

public class DrawTriangleApplet extends JApplet
{
    int[] TriangleXValues = new int[3];
    int[] TriangleYValues = new int[3];
    int alpha = 60; //等腰三角形的底角
    int baseLine = 80; //等腰三角形的底边长

    // 三角形的方向
    final int UP = 1, RIGHT = 2, DOWN = 3, LEFT = 4;

    // 三角形的绘制风格
    final int OUTLINE = 1, FILL = 2, MIXED = 3;

    Color fillColor = Color.BLACK; // 缺省的填充颜色
    Color outlineColor = Color.BLACK; //缺省的轮廓颜色

    public void drawTrigangle(int x, int y, int triangleDirection, int
        triangleStryle, Graphics g)
    {
        // 计算偏移量
        int offset = (int)(baseLine / 2 * Math.tan(alpha * Math.PI / 180));

        // 计算三角形3个顶点的坐标
        switch (triangleDirection)
        {
            case 1:
                TriangleXValues[0] = x - baseLine / 2;
                TriangleYValues[0] = y;
                TriangleXValues[1] = x + baseLine / 2;
                TriangleYValues[1] = y;
                TriangleXValues[2] = x;
                TriangleYValues[2] = y - offset;
                break;

            case 2:
                TriangleXValues[0] = x;
                TriangleYValues[0] = y - baseLine / 2;
                TriangleXValues[1] = x;
                TriangleYValues[1] = y + baseLine / 2;
                TriangleXValues[2] = x + offset;
                TriangleYValues[2] = y;
                break;

            case 3:
                TriangleXValues[0] = x - baseLine / 2;
                TriangleYValues[0] = y;
                TriangleXValues[1] = x + baseLine / 2;
                TriangleYValues[1] = y;
                TriangleXValues[2] = x;
                TriangleYValues[2] = y + offset;
                break;

            case 4:
                TriangleXValues[0] = x;
                TriangleYValues[0] = y - baseLine / 2;
                TriangleXValues[1] = x;
                TriangleYValues[1] = y + baseLine / 2;
                TriangleXValues[2] = x - offset;
                TriangleYValues[2] = y;
                break;
        }

        // 根据三角形的坐标及风格进行绘制
        switch (triangleStryle)
        {
            case 1:
                g.setColor(outlineColor);
                g.drawPolygon(TriangleXValues, TriangleYValues, 3);
                break;

            case 2:
                g.setColor(fillColor);
                g.fillPolygon(TriangleXValues, TriangleYValues, 3);
                break;

            case 3:
                g.setColor(fillColor);
                g.fillPolygon(TriangleXValues, TriangleYValues, 3);
                g.setColor(outlineColor);
                g.drawPolygon(TriangleXValues, TriangleYValues, 3);
                break;
        }

    }

    public void setBaseLine(int baseLine)
    {
        this.baseLine = baseLine;
    }

    public void setFillColor(Color fillColor)
    {
        this.fillColor = fillColor;
    }

    public void setOutlineColor(Color outlineColor)
    {
        this.outlineColor = outlineColor;
    }

    // 在applet上绘制三角形
    public void paint(Graphics g)
    {
        // 调用父类的 paint 方法
        super.paint(g);

        // 设置字体
        g.setFont(new Font("汉真广标", Font.PLAIN, 35));
        g.drawString("三角形的画法", 10, 35);

        setFillColor(Color.RED);

        // 绘制三角形1		
        drawTrigangle(50, 120, UP, OUTLINE, g);

        // 绘制三角形2	
        drawTrigangle(100, 120, RIGHT, FILL, g);

        setBaseLine(40);
        setOutlineColor(Color.BLUE);
        setFillColor(Color.GREEN);

        // 绘制三角形3	
        drawTrigangle(200, 120, DOWN, MIXED, g);

        setBaseLine(60);
        setFillColor(Color.ORANGE);

        // 绘制三角形4	
        drawTrigangle(300, 120, LEFT, FILL, g);

    } // paint 方法结束

} //  DrawTriangleApplet 类结束

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