Java开发实例大全提高篇——Applet的应用

第21章  Applet的应用
21.1  Applet在html中的使用
实例549  在html中显示Applet
HtmlShowApplet.java
    public void paint(Graphics g){
        g.drawString("html文件已经运行", 50, 50);// 绘制文本
        g.drawString("在html中显示了Applet程序", 50, 80);// 绘制文本
    }
HtmlShowApplet.html
<applet code = "com.zzk.HtmlShowApplet.class" width ="260" height = "300">
</applet>

实例550  设置Applet的显示位置
ShowPositionApplet.java
    String author;// 声明成员变量
    public void init() {
        author = "ZhenKun Zhang";// 初始化成员变量
    }
    public void paint(Graphics g){
        g.setColor(Color.blue);// 设置颜色
        g.drawString(author, 50, 30);// 绘制文本
    }
ShowPositionApplet.html
<html>
原有内容1
<applet code = "com.zzk.ShowPositionApplet.class" width ="180" height = "120" Align = "right">
</applet>
原有内容2
</html>

实例551  Applet获取页面传递的参数
ShowParamApplet.java
    String id;// 声明成员变量
    String name;// 声明成员变量
    String sex;// 声明成员变量
    public void init() {
        id = getParameter("id");// 获得参数id的值
        name = getParameter("name");// 获得参数name的值
        sex = getParameter("sex");// 获得参数sex的值
    }
    public void paint(Graphics g) {
        g.setColor(Color.blue);// 设置颜色
        g.drawString("学号:", 30, 10);// 绘制文本
        g.drawString(id, 70, 10);// 绘制获得的id值
        g.drawString("姓名:", 30, 30);// 绘制文本
        g.drawString(name, 70, 30);// 绘制获得的name值
        g.drawString("性别:", 30, 50);// 绘制文本
        g.drawString(sex, 70, 50);// 绘制获得的sex值
    }
ShowParamApplet.html
<html>
<applet  code = "com.zzk.ShowParamApplet.class" width ="260" height = "180">
<param name = "id" value = "9801**01" />
<param name = "name" value = "ZhenKun Zhang" />
<param name = "sex" value = "男" />
</applet>
</html>

实例552  使用<applet>标记中的archive属性
UseArchivePropertyApplet.java
    public void init() {
        // 使用与html文件关联的NewPanelApp.jar文件中的NewPanel类创建对象
        npanel.NewPanel npanel = new npanel.NewPanel();
        setLayout(null);// 设置容器为绝对布局
        npanel.setBounds(10, 10, 254, 167);// 设置NewPanel对象的显示位置和大小
        add(npanel);// 将NewPanel对象添加到容器中
    }
NewPanel.java
    private JTextField textField;// 声明文本框
    public NewPanel() {
        super();// 调用超类的构造方法
        setLayout(null);// 设置面板为绝对布局
        setBounds(100, 100, 254, 167);// 设置面板的位置和大小
        final JButton button = new JButton();// 创建按钮
        button.setText("按钮一");// 指定按钮标题
        button.setBounds(38, 86, 73, 28);// 指定按钮在面板中的显示位置和大小
        add(button);// 将按钮添加到面板上
        final JButton button_1 = new JButton();// 创建按钮
        button_1.setText("按钮二");// 指定按钮标题
        button_1.setBounds(140, 86, 73, 28);// 指定按钮在面板中的显示位置和大小
        add(button_1);// 将按钮添加到面板上
        textField = new JTextField();// 创建文本框
        textField.setText("这是一个文本框控件");// 指定文本框的标题
        textField.setBounds(38, 35, 175, 22);// 设置文本框的显示位置和大小
        add(textField);// 将文本框添加到面板上
    }
UseArchivePropertyApplet.html
<applet code = "com.zzk.UseArchivePropertyApplet.class" width ="300" height = "220" archive = "NewPanelApp.jar">
</applet>

21.2  Applet的方法
实例553  使用paint()方法绘制页面内容
PaintMethodApplet.java
    public void paint(Graphics g) {
        g.setColor(Color.BLUE);// 设置颜色
        g.drawString("这是使用paint()方法绘制的文本。", 30, 40);// 绘制文本内容
        g.setColor(Color.RED);// 设置颜色
        g.drawString("下面是使用paint()方法绘制的图形。", 30, 80);// 绘制文本内容
        g.drawRect(30, 100, 50, 40);// 绘制图形
    }
PaintMethodApplet.html
<html>
<applet code = "com.zzk.PaintMethodApplet.class" width ="260" height = "180">
</applet>
</html>

实例554  使用update()方法更新页面内容
UpdateMethodApplet.java
    boolean flag = false;// 定义标记变量
    public void start() {
        repaint();// 重新调用paint方法
    }
    public void paint(Graphics g) {
        g.setColor(Color.RED);// 设置颜色
        g.drawString("这是使用paint()方法绘制的文本。", 30, 120);// 绘制文本
        g.setColor(Color.BLUE);// 设置颜色
        g.drawString("下面是使用paint()方法绘制的图形。", 30, 140);// 绘制文本
        g.drawRect(30, 150, 50, 40);// 绘制矩形
        update(g);// 调用updatae方法
    }
    public void update(Graphics g) {
        if (flag) {
            g.clearRect(0, 0, 300, 220);// 标记变量为true时,清除内容
            flag = false;// 设置标记变量为false
        } else {
            flag = true;// 设置标记变量为true
        }
        g.setColor(Color.BLUE);// 设置颜色
        g.drawString("这是使用updatePaint()方法绘制的文本。", 30, 20);// 绘制文本
        g.setColor(Color.RED);// 设置颜色
        g.drawString("下面是使用updatePaint()方法绘制的图形。", 30, 40);// 绘制文本
        g.drawRect(30, 50, 50, 40);// 绘制矩形
    }
UpdateMethodApplet.html
<html>
<applet code = "com.zzk.UpdateMethodApplet.class" width ="300" height = "220">
</applet>
</html>

实例555  使用repaint()方法重新绘制页面
RepaintMethodApplet.java
    static int iFlag = 0;// 定义标记变量
    public void start() {
        iFlag++;// 调整标记变量的值
        repaint();// 重新调用paint()方法
    }
    public void paint(Graphics g) {
        if (iFlag == 1) {
            g.drawString("这是使用paint()方法绘制的文本。", 30, 60);// 绘制文本
        } else if (iFlag == 2) {
            g.setColor(Color.RED);// 设置颜色
            g.drawString("下面是使用paint()方法绘制的图形。", 30, 60);// 绘制文本
            g.drawRect(30, 80, 50, 40);// 绘制矩形
        } else if (iFlag == 3) {
            g.setColor(Color.BLUE);// 设置颜色
            g.drawString("下面是使用paint()方法绘制的图形。", 30, 60);// 绘制文本
            g.drawRect(30, 80, 50, 40);// 绘制矩形
        } else {
            g.setColor(Color.GREEN);// 设置颜色
            g.drawString("下面是使用paint()方法绘制的图形。", 30, 60);// 绘制文本
            g.drawRect(30, 80, 50, 40);// 绘制矩形
        }
    }

RepaintMethodApplet.html
<html>
<applet code = "com.zzk.RepaintMethodApplet.class" width ="300" height = "220">
</applet>
</html>

实例556  Applet显示地址栏上的路径
GainAddressBarPathApplet.java
    URL url;// 声明URL对象
    public void start() {
        url = this.getDocumentBase();// 获得地址栏上路径的URL对象
    }
    public void paint(Graphics g) {
        g.setColor(Color.blue);// 设置颜色
        g.drawString(url.getFile(), 30, 20);// 绘制地址栏上的路径
    }

GainAddressBarPathApplet.html
<html>
<applet code = "com.zzk.GainAddressBarPathApplet.class" width ="1200" height = "220">
</applet>
</html>

实例557  Applet显示class存放的路径
ClassSavePathApplet.java
    URL url;// 声明URL对象
    public void start() {
        url = this.getCodeBase();// 获得class存放路径的URL对象
    }
    public void paint(Graphics g) {
        g.setColor(Color.blue);// 设置颜色
        g.drawString(url.getFile(), 30, 20);// 绘制class的存放路径
    }

ClassSavePathApplet.html
<html>
<applet code = "com.zzk.ClassSavePathApplet.class" width ="1200" height = "220">
</applet>
</html>

21.3  Applet中的文字处理
实例558  控制Applet字体大小
SetFontSizeApplet.java
    public void paint(Graphics g) {
        Font font = new Font("宋体", Font.PLAIN, 16);// 创建字体对象,字体大小为16
        g.setFont(font);// 设置字体
        g.drawString("字体大小为16", 30, 20);// 绘制文本
        font = new Font("宋体", Font.PLAIN, 22);// 创建字体对象,字体大小为22
        g.setFont(font);// 设置字体
        g.drawString("字体大小为22", 30, 60);// 绘制文本
        font = new Font("宋体", Font.PLAIN, 36);// 创建字体对象,字体大小为36
        g.setFont(font);// 设置字体
        g.drawString("字体大小为36", 30, 120);// 绘制文本
    }
SetFontSizeApplet.html
<html>
<applet code = "com.zzk.SetFontSizeApplet.class" width ="300" height = "220">
</applet>
</html>

实例559  控制Applet文字位置
SetTextPositionApplet.java
    public void paint(Graphics g) {
        Font font = new Font("宋体", Font.PLAIN, 32);// 创建字体对象
        g.setFont(font);// 设置字体
        g.drawString("标题", 65, 40);// 绘制文本,其位置坐标为(65,40)
        font = new Font("宋体", Font.PLAIN, 16);// 创建字体对象
        g.setFont(font);// 设置字体
        g.drawString("正文一的内容", 50, 80);// 绘制文本,其位置坐标为(50,80)
        g.drawString("正文二的内容", 50, 120);// 绘制文本,其位置坐标为(50,120)
        g.drawString("正文三的内容", 50, 160);// 绘制文本,其位置坐标为(50,160)
    }
SetTextPositionApplet.html
<html>
<applet code = "com.zzk.SetTextPositionApplet.class" width ="300" height = "220">
</applet>
</html>

实例560  控制Applet字体样式
SetFontStyleApplet.java
    public void paint(Graphics g) {
        Font font = new Font("宋体", Font.PLAIN, 16);// 创建字体对象,字体样式为普通字体
        g.setFont(font);// 设置字体
        g.drawString("普通字体", 30, 20);// 绘制文本
        font = new Font("宋体", Font.ITALIC, 22);// 创建字体对象,字体样式为倾斜字体
        g.setFont(font);// 设置字体
        g.drawString("倾斜字体", 30, 60);// 绘制文本
        font = new Font("宋体", Font.BOLD, 28);// 创建字体对象,字体样式为加粗字体
        g.setFont(font);// 设置字体
        g.drawString("加粗字体", 30, 120);// 绘制文本
    }
SetFontStyleApplet.html
<html>
<applet code = "com.zzk.SetFontStyleApplet.class" width ="300" height = "220">
</applet>
</html>

实例561  Applet中绘制立体效果的文字
SolidTextApplet.java
    public void paint(Graphics g) { // 重写paint()方法
        String value = "立体文字效果";
        int x = 10; // 文本位置的横坐标
        int y = 120; // 文本位置的纵坐标
        Font font = new Font("宋体", Font.BOLD, 72); // 创建字体对象
        g.setFont(font); // 设置字体
        g.setColor(Color.GRAY);// 设置颜色为灰色
        int i = 0;// 循环变量
        while (i < 8) {
            g.drawString(value, x, y); // 绘制文本
            x += 1;// 调整绘制点的横坐标值
            y += 1;// 调整绘制点的纵坐标值
            i++;// 调整循环变量的值
        }
        g.setColor(Color.BLACK);// 设置颜色黑色
        g.drawString(value, x, y); // 绘制文本
    }
SolidTextApplet.html
<html>
<applet code = "com.zzk.SolidTextApplet.class" width ="500" height = "220">
</applet>
</html>

实例562  Applet中绘制阴影效果的文字
ShadowTextApplet.java
    public void paint(Graphics g) { // 重写paint()方法
        String value = "阴影文字";
        int x = 10; // 文本位置的横坐标
        int y = 120; // 文本位置的纵坐标
        Font font = new Font("华文行楷", Font.BOLD, 80); // 创建字体对象
        g.setFont(font); // 设置字体
        g.setColor(Color.GRAY);// 设置颜色为灰色
        g.drawString(value, x, y); // 绘制文本
        x += 3;// 调整绘制点的横坐标值
        y += 3;// 调整绘制点的纵坐标值
        g.setColor(Color.BLACK);// 设置颜色黑色
        g.drawString(value, x, y); // 绘制文本
    }
ShadowTextApplet.html
<html>
<applet code = "com.zzk.ShadowTextApplet.class" width ="500" height = "220">
</applet>
</html>

实例563  Applet中绘制倾斜效果的文字
ShearTextApplet.java
    public void paint(Graphics g) { // 重写paint()方法
        Graphics2D g2 = (Graphics2D) g;// 转换为Graphics2D类型
        String value = "倾斜文字";// 绘制的文本
        int x = 10; // 文本位置的横坐标
        int y = 190; // 文本位置的纵坐标
        Font font = new Font("华文行楷", Font.BOLD, 72); // 创建字体对象
        g2.setFont(font); // 设置字体
        g2.shear(0.1, -0.4);// 倾斜画布
        g2.drawString(value, x, y); // 绘制文本
    }
ShearTextApplet.html
<html>
<applet code = "com.zzk.ShearTextApplet.class" width ="380" height = "220">
</applet>
</html>

实例564  Applet中绘制渐变效果的文字
GradientTextApplet.java
    public void paint(Graphics g) { // 重写paint()方法
        Graphics2D g2 = (Graphics2D) g;// 转换为Graphics2D类型
        String value = "渐变色文字";// 绘制的文本
        int x = 15; // 文本位置的横坐标
        int y = 120; // 文本位置的纵坐标
        Font font = new Font("楷体", Font.BOLD, 60); // 创建字体对象
        g2.setFont(font); // 设置字体
        GradientPaint paint = new GradientPaint(20, 20, Color.BLUE, 100, 120,
                Color.RED, true);// 创建循环渐变的GradientPaint对象
        g2.setPaint(paint);// 设置渐变
        g2.drawString(value, x, y); // 绘制文本
    }
GradientTextApplet.html
<html>
<applet code = "com.zzk.GradientTextApplet.class" width ="380" height = "220">
</applet>
</html>

实例565  Applet中绘制会变色的文字
ChangeTextColorApplet.java
    Color color = new Color(0, 0, 255);// 创建颜色对象
    public void init() {
        Thread thread = new Thread(this);// 创建线程对象
        thread.start();// 启动线程对象
    }
    public void paint(Graphics g) { // 重写paint()方法
        Graphics2D g2 = (Graphics2D) g;// 转换为Graphics2D类型
        String value = "会变色的文字";// 绘制的文本
        int x = 10; // 文本位置的横坐标
        int y = 110; // 文本位置的纵坐标
        Font font = new Font("楷体", Font.BOLD, 40); // 创建字体对象
        g2.setFont(font); // 设置字体
        g2.setColor(color);// 设置颜色
        g2.drawString(value, x, y); // 绘制文本
    }
    public void run() {
        Random random = new Random();// 创建随机数对象
        while (true) {
            int R = random.nextInt(256);// 随机产生颜色的R值
            int G = random.nextInt(256);// 随机产生颜色的G值
            int B = random.nextInt(256);// 随机产生颜色的B值
            color = new Color(R, G, B);// 创建颜色对象
            repaint();// 调用paint()方法
            try {
                Thread.sleep(300);// 休眠300毫秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
ChangeTextColorApplet.html
<html>
<applet code = "com.zzk.ChangeTextColorApplet.class" width ="380" height = "220">
</applet>
</html>

实例566  Applet中绘制顺时针旋转的文字
ClockwiseTextApplet.java
    private JTextField textField;
    ClockwiseTextPanel clockwiseTextPanel = new ClockwiseTextPanel(); // 创建面板类的实例
    public void init() {
        setLayout(new BorderLayout());
        add(clockwiseTextPanel); // 将面板类的实例添加到窗体容器中
        textField = new JTextField();
        textField.addCaretListener(new CaretListener() {
            public void caretUpdate(CaretEvent arg0) {
                String text = textField.getText();// 获取文本框字符串
                clockwiseTextPanel.setText(text);// 为面板中的text变量赋值
            }
        });
        getContentPane().add(textField, BorderLayout.SOUTH);
    }
    class ClockwiseTextPanel extends JPanel { // 创建内部面板类
        private String text;
        public ClockwiseTextPanel() {
            setOpaque(false);// 设置面板为透明
            setLayout(null);// 设置为绝对布局
        }
        public String getText() {
            return text; // 获得成员变量的值
        }
        public void setText(String text) {
            this.text = text;// 为成员变量赋值
            repaint();// 调整paint()方法
        }
        public void paint(Graphics g) {// 重写paint()方法
            Graphics2D g2 = (Graphics2D) g;// 获得Graphics2D的实例
            int width = getWidth();// 获得面板的宽度
            int height = getHeight();// 获得面板的高度
            if (text != null) {
                char[] array = text.toCharArray();// 将文本转换为字符数组
                int len = array.length * 5;// 定义圆的半径,同时可以调整文字的距离
                Font font = new Font("宋体", Font.BOLD, 22);// 创建字体
                g2.setFont(font);// 设置字体
                double angle = 0;// 定义初始角度
                for (int i = 0; i < array.length; i++) {// 遍历字符串中的字符
                    if (i == 0) {
                        g2.setColor(Color.BLUE);// 第一个字符用蓝色
                    } else {
                        g2.setColor(Color.BLACK);// 其他字符用黑色
                    }
                    int x = (int) (len * Math.sin(Math.toRadians(angle + 270)));// 计算每个文字的横坐标位置
                    int y = (int) (len * Math.cos(Math.toRadians(angle + 270)));// 计算每个文字的纵坐标位置
                    g2.drawString(array[i] + "", width / 2 + x, height / 2 - y);// 绘制字符
                    angle = angle + 360d / array.length;// 改变角度
                }
            }
        }
    }
ClockwiseTextApplet.html
<html>
<applet code = "com.zzk.ClockwiseTextApplet.class" width ="380" height = "400">
</applet>
</html>

实例567  Applet中动态绘制文本
demo.txt
图片溶合特效
图片淡入淡出特效
图片百叶窗特效
图片马赛克特效
DynamicDrawTextApplet.java
    int x = 20;// 起始点的x坐标
    int y = 30;// 起始点的y坐标
    String textStrings = "小应用程序中缩放图像\n小应用程序中翻转图像\n小应用程序中旋转图像\n小应用程序中倾斜图像";// 需要动态绘制的字符串
    String value = "";// 存储字符串中的单个字符
    public void init() {
        Thread thread = new Thread(this);// 创建线程对象
        thread.start();// 启动线程对象
    }
    public void paint(Graphics g) {
        Font font = new Font("华文楷体", Font.BOLD, 20);// 创建字体对象
        g.setFont(font);// 指定字体
        g.setColor(Color.RED);// 指定颜色
        g.drawString(value, x, y);// 绘制文本
    }
    public void update(Graphics g) {// 重写update方法,防止无法显示绘制的所有内容
        paint(g);// 调用paint()方法
    }
    public void run() {
        try {
            for (int i = 0; i < textStrings.length(); i++) {
                Thread.sleep(400); // 当前线程休眠400毫秒
                value = textStrings.substring(i, i + 1);// 截取字符串中的一个字符
                if (value.equals("\n")) {// 是换行符
                    x = 20;// 下一行起始点的x坐标
                    y += 30;// 下一行文本的y坐标
                } else {// 不是回车或换行符
                    x += 20;// 当前行下一个字的x坐标
                }
                repaint();// 调用paint()方法
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
DynamicDrawTextApplet.html
<html>
<applet code = "com.zzk.DynamicDrawTextApplet.class" width ="380" height = "220">
</applet>
</html>

21.4  Applet中的图形处理
实例568  Applet绘制直线
DrawLineApplet.java
    public void paint(Graphics g) {
        String value = "画直线";
        int x = 215;// 直线的横坐标(右)
        int y = 45;// 直线的纵坐标(右)
        int x1 = 15;// 直线的横坐标(左)
        int y1 = 45;// 直线的纵坐标(左)
        int x2 = 300;// 直线的横坐标(右)
        int y2 = 100;// 直线的纵坐标(右)
        int x3 = 60;// 直线的横坐标(左)
        int y3 = 100;// 直线的纵坐标(左)
        g.setColor(Color.blue);// 设置颜色红色
        g.drawLine(x, y, x1, y1);// 绘制直线
        g.drawLine(x2, y2, x3, y3);// 绘制直线
        g.drawString(value, 120, 75);// 绘制文本
    }
DrawLineApplet.html
<html>
<applet  code = "com.zzk.DrawLineApplet.class" width ="800" height = "500">
</applet>
</html>

实例569  Applet绘制矩形
DrawRectApplet.java
    public void paint(Graphics g) {
        String value = "画矩形";
        int x = 42;// 矩形的横坐标
        int y = 27;// 矩形的纵坐标
        int width = 122;// 矩形的宽度
        int height = 64;// 矩形的高度
        g.setColor(Color.BLUE);// 设置颜色蓝色
        g.drawRect(x, y, width, height);// 绘制矩形
        int x0 = 54;// 矩形的横坐标
        int y0 = 37;// 矩形的纵坐标
        int width0 = 130;// 矩形的宽度
        int height0 = 69;// 矩形的高度
        g.setColor(Color.BLUE);// 设置颜色蓝色
        g.drawRect(x0, y0, width0, height0);// 绘制矩形
        int x1 = 67;// 矩形的横坐标
        int y1 = 48;// 矩形的纵坐标
        int width1 = 137;// 矩形的宽度
        int height1 = 73;// 矩形的高度
        g.setColor(Color.BLUE);// 设置颜色蓝色
        g.drawRect(x1, y1, width1, height1);// 绘制矩形
        g.drawString(value, 185, 143);// 绘制文本
    }
DrawRectApplet.html
<html>
<applet  code = "com.zzk.DrawRectApplet.class" width="500" height="220">
</applet>
</html>

实例570  Applet绘制圆角矩形
DrawRoundRectApplet.java
    public void paint(Graphics g) {
        String value = "画圆角矩形";
        int x = 20;// 圆角矩形位置的横坐标
        int y = 20;// 圆角矩形位置的纵坐标
        int width = 70;// 圆角矩形宽度
        int height = 129;// 圆角矩形高度
        int xr = 5;                         // 圆角矩形圆角的水平弧度
        int yr = 7;                         // 圆角矩形圆角的垂直弧度

        g.setColor(Color.blue);// 设置颜色
        g.drawRoundRect(x, y, width, height, xr, yr); // 绘制图形
        
        int x0 = 35; // 圆角矩形位置的横坐标
        int y0 = 35; // 圆角矩形位置的纵坐标
        int width0 = 82; // 圆角矩形宽度
        int height0 = 139; // 圆角矩形高度
        int xr0 = 10; // 圆角矩形圆角的水平弧度
        int yr0 = 12; // 圆角矩形圆角的垂直弧度
        g.setColor(Color.blue);// 设置颜色
        g.drawRoundRect(x0, y0, width0, height0, xr0, yr0);// 绘制图形
        
        int x1 = 59;// 圆角矩形位置的横坐标
        int y1 = 59;// 圆角矩形位置的纵坐标
        int width1 = 92;// 圆角矩形宽度
        int height1 = 151;// 圆角矩形高度
        int xr1 = 20;// 圆角矩形圆角的水平弧度
        int yr1 = 22;// 圆角矩形圆角的垂直弧度
        g.setColor(Color.blue);// 设置颜色
        g.drawRoundRect(x1, y1, width1, height1, xr1, yr1);// 绘制图形
        g.drawString(value, 165, 30);// 绘制文本
    }
DrawRoundRectApplet.html
<html>
<applet  code = "com.zzk.DrawRoundRectApplet.class" width ="500" height = "320">
</applet>
</html>

实例571  Applet绘制椭圆
DrawOvalApplet.java
    public void paint(Graphics g) {
        String value = "画椭圆";
        int x = 25;// 椭圆位置横坐标
        int y = 40;// 椭圆位置
        int xr = 150;// 椭圆的横坐标半径
        int yr = 150;// 椭圆的纵坐标半径
        g.drawOval(x, y, xr, yr);// 绘制椭圆
        int x0 = 67;// 椭圆位置横坐标
        int y0 = 40;// 椭圆位置
        int xr0 = 65;// 椭圆的横坐标半径
        int yr0 = 150;// 椭圆的纵坐标半径
        g.setColor(Color.blue);// 设置颜色
        g.drawOval(x0, y0, xr0, yr0);// 绘制椭圆
        g.drawString(value, 180, 190);// 绘制文本
    }
DrawOvalApplet.html
<html>
<applet  code = "com.zzk.DrawOvalApplet.class" width ="500" height = "320">
</applet>
</html>

实例572  Applet绘制圆弧
DrawArcApplet.java
    public void paint(Graphics g) {
        String value = "画弧";
        int x = 35;// 弧位置的横坐标
        int y = 65;// 弧位置的纵坐标
        int l = 150;// 弧的长度
        int width = 80;// 弧的宽度
        int startAngle = 10;// 弧的起始角度
        int endAngle = -120;// 终止画弧前扫过的角度
        g.setColor(Color.red);// 设置颜色
        g.drawArc(x + 20, y, l, width, startAngle, endAngle);// 绘制弧
        
        int x0 = 5;// 弧位置的横坐标
        int y0 = 40;// 弧位置的纵坐标
        int l0 = 180;// 弧的长度
        int width0 = 120;// 弧的宽度
        int startAngle0 = 70;// 弧的起始角度
        int endAngle0 = 180;// 终止画弧前扫过的角度
        g.setColor(Color.red);// 设置颜色
        g.drawArc(x0 + 20, y0, l0, width0, startAngle0, endAngle0);// 绘制弧
        
        int x1 = 19;// 弧位置的横坐标
        int y1 = 90;// 弧位置的纵坐标
        int l1 = 200;// 弧的长度
        int width1 = 100;// 弧的宽度
        int startAngle1 = 5;// 弧的起始角度
        int endAngle1 = 300;// 终止画弧前扫过的角度
        g.setColor(Color.red);// 设置颜色
        g.drawArc(x1 + 20, y1, l1, width1, startAngle1, endAngle1);// 绘制弧
        g.drawString(value, 195, 160);
        
    }
DrawArcApplet.html
<html>
<applet  code = "com.zzk.DrawArcApplet.class" width ="500" height = "220">
</applet>
</html>

实例573  Applet绘制折线
DrawPolylineApplet.java
    public void paint(Graphics g) {
        String value = "折线";
        int[] x = { 30, 60, 60, 20 };// 声明表示折线横坐标的数组
        int[] y = { 30, 70, 150, 80 };// 声明表示折线纵坐标的数组
        int num1 = x.length;// 提取x,y坐标对数组的长度
        g.setColor(Color.blue);// 设置颜色
        g.drawPolyline(x, y, num1);// 绘制折线
        int[] x0 = { 80, 110, 65, 80, 200 };// 声明表示折线横坐标的数组
        int[] y0 = { 30, 70, 100, 120, 150 };// 声明表示折线纵坐标的数组
        int num2 = x0.length;// 提取x0,y0坐标对数组的长度
        g.drawPolyline(x0, y0, num2);// 绘制折线
        g.drawString(value, 160, 130);// 绘制文本
    }
DrawPolylineApplet.html
<html>
<applet  code = "com.zzk.DrawPolylineApplet.class" width ="500" height = "320">
</applet>
</html>

实例574  Applet绘制多角形
DrawPolygonApplet.java
    public void paint(Graphics g) {
        String value = "绘制多角形";
        int x[] = { 60, 103, 170, 150, 120 };// 声明多角形的横坐标数组
        int y[] = { 80, 180, 140, 80, 120 };// 声明多角形的纵坐标数组
        int num = x.length;// 取得多角形x,y坐标对数组的长度
        g.setColor(Color.blue);// 设置颜色
        g.drawPolygon(x, y, num);// // 绘制多角形
        g.drawString(value, 120, 70);// 绘制文本
    }
DrawPolygonApplet.html
<html>
<applet  code = "com.zzk.DrawPolygonApplet.class" width ="500" height = "220">
</applet>
</html>

实例575  Applet绘制图片
DrawImageApplet.java
    public void paint(Graphics g) {// 初始化方法
        String value = "绘制图片";
        Image image = null;// 声明图像对象
        image = getImage(getCodeBase(), "com/zzk/PD2.jpg");// 获得图片信息
        int x = 10;// 图像位置的横坐标
        int y = 20;// 图像位置的纵坐标
        int width = image.getWidth(this);// 获得图像的宽度
        int height = image.getHeight(this);// 获取图像的高度
        g.drawImage(image, x + 150, y + 30, width / 5, height / 5, this);// 绘制图像
        g.drawImage(image, x + 25, y + 10, (int) (width * 0.2),
                (int) (height * 0.3), this);// 绘制图像
        g.drawString(value, 140, 170);// 绘制文本
    }

DrawImageApplet.html
<html>
<applet  code = "com.zzk.DrawImageApplet.class" width ="1200" height = "1000">
</applet>
</html>

实例576  Applet中的图形加运算
AddOperationApplet.java
    public void paint(Graphics g) {
        String value = "图形相加运算";
        Graphics2D g2d = (Graphics2D) g;// p转换为可用的Graphics2D对象
        Rectangle2D.Float rect1 = new Rectangle2D.Float(20, 70, 185, 60);// 创建矩形对象
        Rectangle2D.Float rect2 = new Rectangle2D.Float(120, 20, 65, 160);// 创建矩形对象
        Area area1 = new Area(rect1);// 创建区域矩形
        Area area2 = new Area(rect2);// 创建区域矩形
        area1.add(area2);// 两个区域进行相加
        g2d.draw(area1);// 绘制相加后的区域矩形
        Rectangle2D.Float rect3 = new Rectangle2D.Float(230, 70, 185, 60);// 创建矩形对象
        Rectangle2D.Float rect4 = new Rectangle2D.Float(290, 20, 65, 160);// 创建矩形对象
        Area area3 = new Area(rect3);// 创建区域矩形
        Area area4 = new Area(rect4);// 创建区域矩形
        area3.add(area4);// 两个区域进行相加
        g2d.draw(area3);// 绘制相加后的区域矩形
        g2d.drawString(value, 25, 56);// 绘制文本
    }
AddOperationApplet.html
<html>
<applet  code = "com.zzk.AddOperationApplet.class" width ="1200" height = "1000">
</applet>
</html>

实例577  Applet中的图形减运算
SubtractOperationApplet.java
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;// 强制转换为Graphics2D对象
        Rectangle2D.Float rect1 = new Rectangle2D.Float(80, 20, 160, 160);// 创建矩形对象
        Ellipse2D.Float ellipse1 = new Ellipse2D.Float(130, 80, 140, 140);// 创建椭圆对象
        Area area1 = new Area(rect1);// 创建区域矩形
        Area area2 = new Area(ellipse1);// 创建区域椭圆
        area1.subtract(area2);// 两个区域图形相减
        g2d.fill(area1);// 绘制相减后的区域图形
        Rectangle2D.Float rect2 = new Rectangle2D.Float(240, -35, 120, 120);// 创建矩形对象
        Ellipse2D.Float ellipse2 = new Ellipse2D.Float(290, 20, 160, 160);// 创建椭圆对象
        Area area3 = new Area(rect2);// 创建矩形区域
        Area area4 = new Area(ellipse2);// 创建椭圆对象的区域图形
        area4.subtract(area3);// 两个区域图形相减
        g2d.fill(area4);// 绘制相减后的区域图形
    }
SubtractOperationApplet.html
<html>
<applet  code = "com.zzk.SubtractOperationApplet.class" width ="1200" height = "1000">
</applet>
</html>

实例578  Applet中的图形交运算
IntersectOperationApplet.java
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;// 强制转换为Graphics2D对象
        Rectangle2D.Float rect1 = new Rectangle2D.Float(0, 40, 140, 140);// 创建矩形对象
        Ellipse2D.Float ellipse1 = new Ellipse2D.Float(80, 40, 140, 140);// 创建椭圆对象
        Area area1 = new Area(rect1);// 创建矩形区域
        Area area2 = new Area(ellipse1);// 创建椭圆区域
        area1.intersect(area2);// 两个区域相交
        g2d.fill(area1);// 绘制相交后的区域图形
        Rectangle2D.Float rect2 = new Rectangle2D.Float(240, 0, 160, 160);// 创建矩形对象
        Ellipse2D.Float ellipse2 = new Ellipse2D.Float(190, 60, 140, 140);// 创建椭圆对象
        Area area3 = new Area(rect2);// 创建矩形区域
        Area area4 = new Area(ellipse2);// 创建椭圆区域
        area3.intersect(area4);// 两个区域相交
        g2d.fill(area3);// 绘制相交后的区域图形
    }
IntersectOperationApplet.html
<html>
<applet  code = "com.zzk.IntersectOperationApplet.class" width ="1200" height = "1000">
</applet>
</html>

实例579  Applet中的图形异或运算
ExclusiveOrOperationApplet.java
    public void paint(Graphics g) { // 重写paint()方法
        Graphics2D g2d = (Graphics2D) g; // 强制转换为Graphics2D对象
        Ellipse2D.Float ellipse1 = new Ellipse2D.Float(30, 80, 180, 80);// 创建椭圆对象
        Ellipse2D.Float ellipse2 = new Ellipse2D.Float(80, 30, 80, 180);// 创建椭圆对象
        Area area1 = new Area(ellipse1);// 创建椭圆区域
        Area area2 = new Area(ellipse2);// 创建椭圆区域
        area1.exclusiveOr(area2);// 两个区域图形进行异或运算
        g2d.fill(area1);// 绘制异或运算后的区域图形
        Ellipse2D.Float ellipse3 = new Ellipse2D.Float(270, 80, 180, 80);// 创建椭圆对象
        Ellipse2D.Float ellipse4 = new Ellipse2D.Float(270, 30, 80, 180);// 创建椭圆对象
        Area area3 = new Area(ellipse3);// 创建椭圆区域
        Area area4 = new Area(ellipse4);// 创建椭圆区域
        area3.exclusiveOr(area4);// 绘制异或运算后的区域图形
        g2d.fill(area3);// 绘制异或运算后的区域图形
    }
ExclusiveOrOperationApplet.html
<html>
<applet  code = "com.zzk.ExclusiveOrOperationApplet.class" width ="1200" height = "1000">
</applet>
</html>

实例580  Applet中绘制纹理填充图形
DrawGrainApplet.java
    private BufferedImage img;// 声明图像对象
    public void init() { // 初始化方法
        img = new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB);// 创建缓冲对象
        Graphics2D g = img.createGraphics();// 创建Graphics2D对象
        g.setPaint(Color.yellow);// 指定颜色
        g.draw(new Rectangle(0, 0, 25, 25));// 绘制矩形
        g.setPaint(Color.red);// 指定颜色
        g.fill(new Rectangle(25, 0, 25, 25));// 填充矩形
        g.setPaint(Color.green);// 指定颜色
        g.fill(new Rectangle(0, 0, 25, 25));// 填充矩形
    }
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;// 强制转换为Graphics2D对象
        g2d.setPaint(new TexturePaint(img, new Rectangle(0, 0, 10, 10)));// 创建纹理并把它加到Graphics中
        g2d.fill(new Ellipse2D.Double(10, 10, 260, 145));// 填充图形
    }

DrawGrainApplet.html
<html>
<applet  code = "com.zzk.DrawGrainApplet.class" width ="500" height = "220">
</applet>
</html>

21.5  Applet中的图像处理
实例581  Applet中缩放图像
ZoomImageApplet.java
    public void paint(Graphics g) {
        String value = "缩放图像";
        Image img = null;// 声明图像对象
        img = getImage(getCodeBase(), "com/zzk/PD2.jpg");// 获得图片信息
        AffineTransform tr = AffineTransform.getScaleInstance(0.25, 0.25);// 创建变形以获得缩放对象
        tr.translate(120, 100);// 设置对象平移
        AffineTransform tr2 = AffineTransform.getScaleInstance(0.15, 0.15);// 创建变形以获得缩放对象
        tr2.translate(900, 950);// 设置对象平移
        Graphics2D g2d = (Graphics2D) g;// 将g转换成一个可用的Graphics2D对象
        g2d.drawImage(img, tr, this);// 绘制图像
        g2d.drawImage(img, tr2, this);// 绘制图像
        g2d.drawString(value, 60, 150);// 绘制文本
    }
ZoomImageApplet.html
<html>
<applet  code = "com.zzk.ZoomImageApplet.class" width ="500" height = "220">
</applet>
</html>

实例582  Applet中翻转图像
TurnImageApplet.java
    public void paint(Graphics g) {
        String value = "翻转图像";
        Image img = null;// 声明图像对象
        img = getImage(getCodeBase(), "com/zzk/PD4.jpg");// 获得图片信息
        int w = img.getWidth(this);// 设置图像的宽度
        int h = img.getHeight(this);// 设置图像的高度
        Graphics2D g2d = (Graphics2D) g;// 将g转换为可以利用的Graphics2D
        g2d.drawString(value, 100, 130);// 绘制文本
        AffineTransform tr = new AffineTransform(-1, 0, 0, 1, w, 0);// 创建变换对象并横向翻转
        AffineTransform tr2 = new AffineTransform(1, 0, 0, -1, 0, h);// 创建变换对象并垂直翻转
        tr.translate(-20, 40);// 图像位置的平移
        tr2.translate(120, -40);// 图像位置的平移
        g2d.drawImage(img, tr, this);// 绘制图像
        g2d.drawImage(img, tr2, this);// 绘制图像
    }
TurnImageApplet.html
<html>
<applet  code = "com.zzk.TurnImageApplet.class" width ="500" height = "220">
</applet>
</html>

实例583  Applet中旋转图像
SlantImageApplet.java
    public void paint(Graphics g) {
        String value = "倾斜图像";
        Image img = null;// 声明图像对象
        img = getImage(getCodeBase(), "com/zzk/PD5.jpg");// 获得图片信息
        Graphics2D g2d = (Graphics2D) g;// 强制转换为Graphics2D对象
        g2d.drawString(value, 209, 170);// 绘制文本
        AffineTransform tr = new AffineTransform();// 创建AffineTransform对象
        tr.translate(210, 32);// 图像位置的平移
        tr.shear(3, 3);// 倾斜图像
        g2d.drawImage(img, tr, this);// 绘制图像
        AffineTransform tr1 = AffineTransform.getScaleInstance(3.5, 3.5);// 获得AffineTransform对象
        tr1.translate(15, 13);// 图像位置的平移
        g2d.drawImage(img, tr1, this);// 绘制图像
    }
SlantImageApplet.html
<html>
<applet  code = "com.zzk.SlantImageApplet.class" width ="500" height = "220">
</applet>
</html>

实例584  Applet中倾斜图像
SetImageLightenessApplet.java
    private BufferedImage image;// 声明缓冲图像对象
    public void paint(Graphics g) {
        Image img = null;// 声明图像对象
        String value = "调整图片亮度";
        img = getImage(getCodeBase(), "com/zzk/PPD.jpg");// 获得图像对象
        int a = img.getWidth(this); // 获得图片宽度赋给变量a
        int b = img.getHeight(this);// 获得图片高度赋给变量b
        if (a >= 0 || b >= 0) {
            image = new BufferedImage(img.getWidth(this), img.getHeight(this),
                    BufferedImage.TYPE_INT_RGB);// 创建缓冲图像对象
            image.getGraphics().drawImage(img, 0, 0, null);// 在缓冲图像对象上绘制图像
            float fa = 2.0f;// 声明表示像素分量
            float fb = -30.0f;// 声明表示像素分量
            RescaleOp op = new RescaleOp(fa, fb, null);// 创建RescaleOp对象
            image = op.filter(image, null); // 过滤缓冲图像对象,实现调整图像亮度的功能
            g.drawImage(img, 30, 40, this);// 绘制原图像对象
            g.drawImage(image, 220, 40, this);// 绘制调整亮度后的缓冲图像对象
            g.drawString(value, 265, 188);// 绘制文本
        }
    }
SetImageLightenessApplet.html
<html>
<applet  code = "com.zzk.SetImageLightenessApplet.class" width ="1200" height = "1000">
</applet>
</html>

实例585  Applet中调整图片的亮度
ImageRotateApplet.java
    public void paint(Graphics g) {
        String value = "旋转图像";
        Image img = null;// 声明图像对象
        img = getImage(getCodeBase(), "com/zzk/PD2.jpg");// 获得图片信息
        Graphics2D g2d = (Graphics2D) g; // 强制转换为Graphics2D对象
        g2d.drawString(value, 180, 150);// 绘制文本
        int x = 50;// 图像位置的横坐标
        int y = -10;// 图像位置的纵坐标
        int w = img.getWidth(this);// 获得图片的宽度
        int h = img.getHeight(this);// 获得图片的高度
        g2d.drawImage(img, x, y + 50, w / 5, h / 5, this);// 绘制图形
        g2d.drawImage(img, x + 150, y + 50, w / 5, h / 5, this);// 绘制图形
        AffineTransform tr = new AffineTransform();// 创建变形对象
        tr.rotate(90, 15, 15, 65);// 设置旋转角度
        g2d.setTransform(tr);// 执行旋转
        g2d.drawImage(img, x + 150, y + 20, w / 5, h / 5, this);// 绘制图形
        tr.rotate(35, 15, 30, 65);// 设置旋转角度
        g2d.setTransform(tr);// 执行旋转
        g2d.drawImage(img, x + 120, y - 60, w / 5, h / 5, this);// 绘制图形
    }
ImageRotateApplet.html
<html>
<applet  code = "com.zzk.ImageRotateApplet.class" width ="500" height = "220">
</applet>
</html>

实例586  Applet中绘制中文验证码
DrawChinesePasswordApplet.java
    int WIDTH = 120;// 宽度
    int HEIGHT = 35;// 高度
    private String num = "";// 验证码
    Random random = new Random();// 实例化Random
    public void paint(Graphics g) {
        String hanZi = "本实例通过从字符串中随机获得四个字符实现了中文验证码的功能";// 定义验证码使用的汉字
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
                BufferedImage.TYPE_INT_RGB);// 实例化BufferedImage
        Graphics gs = image.getGraphics(); // 获取Graphics类的对象
        if (!num.isEmpty()) {
            num = "";// 清空验证码
        }
        Font font = new Font("黑体", Font.BOLD, 20); // 通过Font构造字体
        gs.setFont(font);// 设置字体
        gs.fillRect(0, 0, WIDTH, HEIGHT);// 填充一个矩形
        // 输出随机的验证文字
        for (int i = 0; i < 4; i++) {
            int index = random.nextInt(hanZi.length());// 随机获得汉字的索引值
            String ctmp = hanZi.substring(index, index + 1);// 获得指定索引处的一个汉字
            num += ctmp;// 更新验证码
            Color color = new Color(20 + random.nextInt(120), 20 + random
                    .nextInt(120), 20 + random.nextInt(120));// 生成随机颜色
            gs.setColor(color); // 设置颜色
            Graphics2D gs2d = (Graphics2D) gs;// 将文字旋转指定角度
            AffineTransform trans = new AffineTransform();// 实例化AffineTransform
            trans.rotate(random.nextInt(45) * 3.14 / 180, 22 * i + 8, 7);
            float scaleSize = random.nextFloat() + 0.8f;// 缩放文字
            if (scaleSize > 1f)
                scaleSize = 1f;// 如果scaleSize大于1,则等于1
            trans.scale(scaleSize, scaleSize); // 进行缩放
            gs2d.setTransform(trans);// 设置AffineTransform对象
            gs2d.drawString(ctmp, WIDTH / 6 * i + 28, HEIGHT / 2);// 画出验证码
        }
        g.drawImage(image, 85, 80, null);// 在小应用程序中绘制验证码
    }
DrawChinesePasswordApplet.html
<html>
<applet  code = "com.zzk.DrawChinesePasswordApplet.class" width ="1200" height = "1000">
</applet>
</html>

实例587  Applet中绘制图片验证码
DrawPasswordWithImageApplet.java
   int WIDTH = 120;// 宽度
    int HEIGHT = 35;// 高度
    private String num = "";// 验证码
    Random random = new Random();// 实例化Random
    public void paint(Graphics g) {
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
                BufferedImage.TYPE_INT_RGB);// 实例化BufferedImage
        Graphics gs = image.getGraphics(); // 获取Graphics类的对象
        if (!num.isEmpty()) {
            num = "";// 清空验证码
        }
        Font font = new Font("黑体", Font.BOLD, 20); // 通过Font构造字体
        gs.setFont(font);// 设置字体
        gs.fillRect(0, 0, WIDTH, HEIGHT);// 填充一个矩形
        Image img = null;// 声明图像对象
        img = getImage(getCodeBase(), "com/zzk/PPD.jpg"); // 创建图像对象
        gs.drawImage(img, 0, 0, WIDTH, HEIGHT, this);// 在缓冲图像对象上绘制图像
        // 输出随机的验证字符
        for (int i = 0; i < 4; i++) {
            char ctmp = (char) (random.nextInt(26) + 65); // 生成A~Z的字母
            num += ctmp;// 更新验证码
            Color color = new Color(20 + random.nextInt(120), 20 + random
                    .nextInt(120), 20 + random.nextInt(120));// 生成随机颜色
            gs.setColor(color); // 设置颜色
            Graphics2D gs2d = (Graphics2D) gs;// 将文字旋转指定角度
            AffineTransform trans = new AffineTransform();// 实例化AffineTransform
            trans.rotate(random.nextInt(45) * 3.14 / 180, 22 * i + 8, 7);
            float scaleSize = random.nextFloat() + 0.8f;// 缩放文字
            if (scaleSize > 1f)
                scaleSize = 1f;// 如果scaleSize大于1,则等于1
            trans.scale(scaleSize, scaleSize); // 进行缩放
            gs2d.setTransform(trans);// 设置AffineTransform对象
            gs2d.drawString(String.valueOf(ctmp), WIDTH / 6 * i + 28, HEIGHT / 2);// 画出验证码
        }
        g.drawImage(image, 85, 80, null);// 在小应用程序中绘制含有图片验证码的缓冲图像
    }
DrawPasswordWithImageApplet.html
<html>
<applet  code = "com.zzk.DrawPasswordWithImageApplet.class" width ="1200" height = "1000">
</applet>
</html>

实例588  Applet中绘制带干扰线的验证码
DrawPasswordWithDisturbApplet.java
    int WIDTH = 120;// 设置宽度
    int HEIGHT = 35;// 设置高度
    private String num = "";// 验证码
    Random random = new Random();// 实例化Random
    public void paint(Graphics g) {
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
                BufferedImage.TYPE_INT_RGB);// 实例化BufferedImage
        Graphics gs = image.getGraphics(); // 获取Graphics类的对象
        if (!num.isEmpty()) {
            num = "";// 清空验证码
        }
        Font font = new Font("黑体", Font.BOLD, 20); // 通过Font构造字体
        gs.setFont(font);// 设置字体
        gs.fillRect(0, 0, WIDTH, HEIGHT);// 填充一个矩形
        Image img = null;
        img = getImage(getCodeBase(), "com/zzk/PPD.jpg"); // 创建图像对象
        gs.drawImage(img, 0, 0, WIDTH, HEIGHT, this);// 在缓冲图像对象上绘制图像
        int startX1 = random.nextInt(20);// 随机获取第一条干扰线起点的x坐标
        int startY1 = random.nextInt(20);// 随机获取第一条干扰线起点的y坐标
        int startX2 = random.nextInt(30) + 35;// 随机获取第一条干扰线终点的x坐标,也是第二条干扰线起点的x坐标
        int startY2 = random.nextInt(10) + 20;// 随机获取第一条干扰线终点的y坐标,也是第二条干扰线起点的y坐标
        int startX3 = random.nextInt(30) + 90;// 随机获取第二条干扰线终点的x坐标
        int startY3 = random.nextInt(10) + 5;// 随机获取第二条干扰线终点的y坐标
        gs.setColor(Color.RED);
        gs.drawLine(startX1, startY1, startX2, startY2);// 绘制第一条干扰线
        gs.setColor(Color.BLUE);
        gs.drawLine(startX2, startY2, startX3, startY3);// 绘制第二条干扰线
        for (int i = 0; i < 4; i++) {// 输出随机的验证文字
            char ctmp = (char) (random.nextInt(26) + 65); // 生成A~Z的字母
            num += ctmp;// 更新验证码
            Color color = new Color(20 + random.nextInt(120), 20 + random
                    .nextInt(120), 20 + random.nextInt(120));// 生成随机颜色
            gs.setColor(color); // 设置颜色
            Graphics2D gs2d = (Graphics2D) gs;// 将文字旋转指定角度
            AffineTransform trans = new AffineTransform();// 实例化AffineTransform
            trans.rotate(random.nextInt(45) * 3.14 / 180, 22 * i + 8, 7);
            float scaleSize = random.nextFloat() + 0.8f;// 缩放文字
            if (scaleSize > 1f)
                scaleSize = 1f;// 如果scaleSize大于1,则使其等于1
            trans.scale(scaleSize, scaleSize); // 进行缩放
            gs2d.setTransform(trans);// 设置AffineTransform对象
            gs2d.drawString(String.valueOf(ctmp), WIDTH / 6 * i + 28, HEIGHT / 2);// 画出验证码
        }
        g.drawImage(image, 85, 80, null);// 在小应用程序中绘制含有图片和干扰线验证码的缓冲图像
    }
DrawPasswordWithDisturbApplet.html
<html>
<applet  code = "com.zzk.DrawPasswordWithDisturbApplet.class" width ="1200" height = "1000">
</applet>
</html>

实例589  Applet中模糊图像
BlurImageApplet.java
    private BufferedImage image;// 声明缓冲图像对象
    public void paint(Graphics g) {
        Image img = null;// 声明创建图像对象
        String value = "模糊图像";
        img = getImage(getCodeBase(), "com/zzk/PPD.jpg");// 获得图片信息
        int a = img.getWidth(this); // 获得图片宽度赋给变量a
        int b = img.getHeight(this);// 获得图片高度赋给变量b
        if (a >= 0 || b >= 0) {
            image = new BufferedImage(img.getWidth(this), img.getHeight(this),
                    BufferedImage.TYPE_INT_RGB);// 创建缓冲图像对象
            image.getGraphics().drawImage(img, 0, 0, null);// 在缓冲图像对象上绘制图像
            float[] data = { 0.0532f, 0.132f, 0.0532f, 0.132f, 0.19f, 0.138f,
                    0.0532f, 0.132f, 0.0532f };// 声明表示像素分量的数组
            Kernel kernel = new Kernel(3, 3, data); // 创建 Kernel对象
            ConvolveOp op = new ConvolveOp(kernel);// 创建ConvolveOp对象
            image = op.filter(image, null); // 过滤缓冲图像对象
            g.drawImage(img, 25, 35, this);// 绘制缓冲图像对象
            g.drawImage(image, 215, 35, this);// 绘制缓冲图像对象
            g.drawString(value, 268, 186);// 绘制文本
        }
    }
BlurImageApplet.html
<html>
<applet  code = "com.zzk.BlurImageApplet.class" width ="500" height = "220">
</applet>
</html>

实例590  Applet中锐化图像
SharperImageApplet.java
    private BufferedImage image;// 声明缓冲图像对象
    public void paint(Graphics g) {
        Image img = null;// 声明创建图像对象
        String value = "锐化图像";
        img = getImage(getCodeBase(), "com/zzk/PPD.jpg");// 获得图片信息
        int a = img.getWidth(this); // 获得图片宽度赋给变量a
        int b = img.getHeight(this);// 获得图片高度赋给变量b
        if (a >= 0 || b >= 0) {
            image = new BufferedImage(img.getWidth(this), img.getHeight(this),
                    BufferedImage.TYPE_INT_RGB);// 创建缓冲图像对象
            image.getGraphics().drawImage(img, 0, 0, null);// 在缓冲图像对象上绘制图像
            float[] data = { 0.0f, -1.0f, 0.0f, -1.0f, 6.0f, -1.0f, 0.0f,
                    -1.0f, 0.0f };// 声明表示像素分量的数组
            Kernel kernel = new Kernel(3, 3, data); // 创建 Kernel对象
            ConvolveOp op = new ConvolveOp(kernel);// 创建ConvolveOp对象
            image = op.filter(image, null); // 过滤缓冲图像对象
            g.drawImage(img, 25, 35, this);// 绘制缓冲图像对象
            g.drawImage(image, 215, 35, this);// 绘制缓冲图像对象
            g.drawString(value, 275, 182);// 绘制文本
        }
    }
SharperImageApplet.html
<html>
<applet  code = "com.zzk.SharperImageApplet.class" width ="500" height = "220">
</applet>
</html>

实例591  Applet中照亮图像边缘
lightenImageEdgeApplet.java
    private BufferedImage image;// 声明缓冲图像对象
    public void paint(Graphics g) {
        Image img = null;// 声明创建图像对象
        String value = "照亮图像边缘";
        img = getImage(getCodeBase(), "com/zzk/PPD.jpg");// 获得图片信息
        int a = img.getWidth(this); // 获得图片宽度赋给变量a
        int b = img.getHeight(this);// 获得图片高度赋给变量b
        if (a >= 0 || b >= 0) {
            image = new BufferedImage(a, b, BufferedImage.TYPE_INT_RGB);// 创建缓冲图像对象
            image.getGraphics().drawImage(img, 0, 0, this);// 在缓冲图像对象上绘制图像
            float[] f = { 0.0f, -1.5f, 0.0f, -1.5f, 6.0f, -1.5f, 0.0f, -1.5f,
                    0.0f };// 声明表示像素分量
            Kernel kernel = new Kernel(3, 3, f);// 创建Kernel对象
            ConvolveOp op = new ConvolveOp(kernel);// 创建RescaleOp对象
            image = op.filter(image, null); // 过滤缓冲图像对象
            g.drawImage(img, 25, 35, this);// 绘制缓冲图像对象
            g.drawImage(image, 217, 35, this);// 绘制缓冲图像对象
            g.drawString(value, 258, 186);// 绘制文本
        }
    }
lightenImageEdgeApplet.html
<html>
<applet  code = "com.zzk.lightenImageEdgeApplet.class" width ="500" height = "220">
</applet>
</html>

实例592  Applet中反向图像
NegativeImageApplet.java
    private BufferedImage image;// 声明缓冲图像对象
    public void paint(Graphics g) {
        String value = "反向图像";
        Image img = null;// 声明图像对象
        img = getImage(getCodeBase(), "com/zzk/PD3.jpg");// 获得图片信息
        int a = img.getWidth(this); // 获得图片宽度赋给变量a
        int b = img.getHeight(this);// 获得图片高度赋给变量b
        if (a >= 0 || b >= 0) {
            image = new BufferedImage(img.getWidth(this), img.getHeight(this),
                    BufferedImage.TYPE_INT_RGB);// 创建缓冲图像对象
            image.getGraphics().drawImage(img, 0, 0, null);// 在缓冲图像对象上绘制图像
            short[] negative = new short[256 * 1];// 创建表示颜色反向的分量数组
            for (int i = 0; i < 256; i++) {
                negative[i] = (short) (255 - i);// 为数组赋值
            }
            ShortLookupTable table = new ShortLookupTable(0, negative);// 创建查找表对象
            LookupOp op = new LookupOp(table, null);// 创建实现从源到目标查找操作的LookupOp对象
            image = op.filter(image, null);// 调用LookupOp对象的filter()方法,实现图像反向功能
            if (image != null) {
                g.drawImage(img, 35, 40, null);// 绘制缓冲图像对象
                g.drawImage(image, 220, 40, null);// 绘制缓冲图像对象
            }
            g.drawString(value, 265, 175);// 绘制文本
        }
    }
NegativeImageApplet.html
<html>
<applet  code = "com.zzk.NegativeImageApplet.class" width ="1200" height = "1000">
</applet>
</html>

实例593  Applet中图像动态拉伸
ImageElongateApplet.java
    private boolean flag = true;// 声明标记变量
    private static float xw;// 定义调整图像宽度的变量
    public void init(){
        xw = 0.5f;// 初始化图像宽度
    }
    public void start(){
        Thread th = new Thread(this);// 创建线程对象
        th.start();// 启动线程对象
    }
    public void paint(Graphics g) {
        Image img = null;// 声明图像对象
        img = getImage(getCodeBase(), "com/zzk/PD4.jpg");// 获得图片信息
        int w = img.getWidth(this);// 设置图像的宽度
        int h = img.getHeight(this);// 设置图像的高度
        Graphics2D g2d = (Graphics2D) g;// 将g转换为可以利用的Graphics2D
        g2d.drawImage(img, w - 50, h, this);// 绘制图像
        AffineTransform tr = new AffineTransform(xw, 0, 0, 1, 150, h);// 创建仿射变换对象进行设置变换(第一个参数)
        g2d.drawImage(img, tr, this);// 绘制图像
        
    }
    @Override
    public void run() {
        while (true) {
            if (flag) {// 标记变量为true时执行
                xw += 0.1f;// 使宽度变大
                if (xw > 2.0f) {// 宽度大于2.0时
                    flag = false;// 标记变量为false
                }
            } else {// 标记变量为false时执行
                xw -= 0.1f;// 使宽度变小
                if (xw < 0.5f) {// 宽度小于2.0时
                    flag = true;// 标记变量为true
                }
            }
            try {
                Thread.sleep(200);// 休眠200毫秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            repaint();// 重新调用paint()方法
        }
    }
ImageElongateApplet.html
<html>
<applet  code = "com.zzk.ImageElongateApplet.class" width ="500" height = "220">
</applet>
</html>

21.6  Applet中的文字动画
实例594  Applet中文字缩放动画
TextScaleApplet.java
    public void start() {
        Thread thread = new Thread(this);// 创建线程对象
        thread.start();// 启动线程对象
    }
    private Image img = null; // 声明图像对象
    boolean flag = false;// 定义标记变量,用户控制x的值
    int x = 12;// 定义表示字体大小的变量x
    Font font = new Font("华文楷体", Font.BOLD, 42);// 创建字体对象
    public void paint(Graphics g) {
        img = getImage(getCodeBase(), "com/zzk/PD3.jpg");// 获取图片资源的路径
        Graphics2D g2 = (Graphics2D) g;// 获得Graphics2D对象
        g2.drawImage(img, 0, 0, getWidth(), getHeight(), this);// 绘制图像
        g2.setFont(font);// 指定字体
        g2.setColor(Color.red);// 指定颜色
        g2.drawString("学无止境", 30, 120);// 绘制文本
    }
    public void run() {
        while (true) {
            if (flag) { // flag为true时
                x -= 2; // x进行减1计算
                if (x <= 12) {// x小于等于12时
                    x = 12; // x等于12
                    flag = false;// 为flag赋值为false
                }
            } else {// flag为false时
                x += 2;// x进行加1计算
                if (x >= 72) {// x大于等于72时
                    x = 72;// x等于72
                    flag = true;// 为flag赋值为true
                }
            }
            font = new Font("华文楷体", Font.BOLD, x);// 重新创建字体对象
            repaint();// 调用paint()方法
            try {
                Thread.sleep(50);// 休眠50毫秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
//    public void update(Graphics g) {
//        paint(g);
//    }
TextScaleApplet.html
<html>
<applet  code = "com.zzk.TextScaleApplet.class" width ="350" height = "200">
</applet>
</html>

实例595  Applet中文字跑马灯动画
HorseRaceLightTextApplet.java
    public void start() {
        Thread thread = new Thread(this);// 创建线程
        thread.start();// 启动线程对象
    }
    String value = "拥有编程词典,学习编程真轻松。";// 存储绘制的内容
    char[] drawChar = value.toCharArray();// 将绘制内容转换为字符数组
    int[] x = new int[drawChar.length];// 存储每个字符绘制点x坐标的数组
    int y = 100;// 存储绘制点y坐标
    
    public void paint(Graphics g) {
        Image img = null;
        img = getImage(getCodeBase(), "com/zzk/PD3.jpg"); // 创建图像对象
        g.clearRect(0, 0, getWidth(), getHeight());// 清除绘图上下文内容
        g.drawImage(img, 0, 0, getWidth(), getHeight(), this);// 绘制图像
        Font font = new Font("华文楷体", Font.BOLD, 20);// 创建字体对象
        g.setFont(font);// 指定字体
        g.setColor(Color.RED);// 指定颜色
        for (int j = drawChar.length - 1; j >= 0; j--) {
            g.drawString(drawChar[drawChar.length - 1 - j] + "", x[j], y);// 绘制文本
        }
    }
    public void run() {
        boolean flag = false;// 为false时表示第一次执行,x坐标进行等比递增,否则进行等差递减
        while (true) {// 读取内容
            try {
                for (int i = drawChar.length - 1; i >= 0; i--) {
                    if (!flag) {
                        x[i] = x[i] + 20 * i;// x坐标进行等比递增
                    } else {
                        x[i] = x[i] + 20;// x坐标进行等比递减
                    }
                    if (x[i] >= 360 - 20) {// 大于窗体宽度减20的值时
                        x[i] = 0; // x坐标为0
                    }
                }
                repaint();// 调用 paint()方法
                if (!flag) {
                    flag = true;// 赋值为true;
                }
                Thread.sleep(1000);// 当前线程休眠300毫秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public void update(Graphics g) { // 重写update()方法防止闪烁
        paint(g);
    }
HorseRaceLightTextApplet.html
<html>
<applet  code = "com.zzk.HorseRaceLightTextApplet.class" width ="350" height = "200">
</applet>
</html>

实例596  Applet中字幕显示动画
CaptionSpecificApplet.java
    int x = 50;// 存储绘制点的x坐标
    int y = 216;// 存储绘制点的y坐标
    String value = "明日图书网的网址";// 存储绘制的内容
    public void start() {
        Thread thread = new Thread(this);// 创建线程对象
        thread.start();// 启动线程对象
    }
    public void paint(Graphics g) {
        g.clearRect(0, 0, 316, 237);// 清除绘图上下文内容
        Image img = null;// 声明图片对象
        img = getImage(getCodeBase(), "com/zzk/PD3.jpg");
        g.drawImage(img, 0, 0, getWidth(), getHeight(), this);// 绘制图像
        Font font = new Font("华文楷体", Font.BOLD, 20);// 创建字体对象
        g.setFont(font);// 指定字体
        g.setColor(Color.RED);// 指定颜色
        g.drawString(value, x, y);// 绘制文本
    }
    public void run() {
        try {
            while (true) { // 读取内容
                Thread.sleep(100); // 当前线程休眠1秒
                if (y <= 216 - 50) {// 如果已经向上移动50像素
                    y = 216;// y坐标定位到最下方
                    if (value.equals("明日图书网的网址")) {
                        value = "http://www.mingribook.com";// 改变绘制的内容
                    } else {
                        value = "明日图书网的网址";// 改变绘制的内容
                    }
                } else {// 如果还没向上移动到50像素
                    y -= 2;// y坐标上移
                }
                repaint();// 调用paint()方法
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void update(Graphics g) { // 重写update()方法防止闪烁
        paint(g);
    }
CaptionSpecificApplet.html
<html>
<applet  code = "com.zzk.CaptionSpecificApplet.class" width ="350" height = "200">
</applet>
</html>

实例597  Applet中文字闪现动画
TextFlashApplet.java
    boolean flag = true;// 标记变量
    String value = "";// 存放绘制内容的变量
    public void start() {
        Thread thread = new Thread(this);// 创建线程对象
        thread.start();// 启动线程对象
    }
    public void paint(Graphics g) {
        Image img = null;// 声明图像对象
        img = getImage(getCodeBase(), "com/zzk/PD3.jpg");
        g.clearRect(0, 0, 310, 230);// 清除绘图上下文的内容
        g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
        Font font = new Font("华文楷体", Font.BOLD, 42);// 创建字体对象
        g.setFont(font);// 指定字体
        g.setColor(Color.red);// 指定颜色
        g.drawString(value, 30, 110);// 绘制文本
    }
    public void run() {
        try {
            while (true) { // 读取内容
                Thread.sleep(150);// 当前线程休眠150秒
                if (flag) {// flag为true
                    flag = false; // 赋值为false
                    value = "JAVA编程词典";// 为value赋值
                } else {
                    flag = true;// 赋值为true
                    value = "";// 赋值为空字符窜
                }
                repaint();// 调用paint()方法
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
TextFlashApplet.html
<html>
<applet  code = "com.zzk.TextFlashApplet.class" width ="350" height = "200">
</applet>
</html>

实例598  Applet中滚动广告字幕动画
RollTextApplet.java
    String value = "明日图书网的网址:http://www.mingribook.com";// 存放绘制的内容
    int x;// 设置横坐标
    int y;// 设置纵坐标
    public void init() { // 初始化方法
        x = 316;// 存储绘制点的x坐标
        y = 190;// 存储绘制点的y坐标
    }
    public void start() {
        Thread thread = new Thread(this);// 创建线程对象
        thread.start();// 启动线程对象
    }
    public void paint(Graphics g) {
        Image img = null;// 声明图像对象
        img = getImage(getCodeBase(), "com/zzk/PD3.jpg");// 获取图片资源路径
        g.drawImage(img, 0, 0, getWidth(), getHeight(), this);// 绘制图像
        g.clearRect(0, 0, 0, 230);// 清除绘图上下文的内容
        g.setColor(Color.BLACK);// 定义颜色
        Font font = new Font("华文楷体", Font.BOLD, 20);// 创建字体对象
        g.setFont(font);// 定义字体
        g.drawString(value, x, y);// 绘制文本
    }
    public void run() {
        try {
            while (true) { // 读取内容
                Thread.sleep(50); // 当前线程休眠1秒
                if (x <= -440) {// 该条件可以根据需要自行调整
                    x = 316;// x坐标定位到最右侧
                } else {
                    x -= 2;// x坐标左移
                }
                repaint();// 调用paint()方法
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void update(Graphics g) { // 重写update()方法防止闪烁
        paint(g);
    }
RollTextApplet.html
<html>
<applet  code = "com.zzk.RollTextApplet.class" width ="316" height = "200">
</applet>
</html>

转载于:https://my.oschina.net/MoreYoungGavin/blog/1083429

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值