关于内部类的一些介绍

1.内部类的概念

2.内部类的书写方式

3.内部类编译文件特点

1.内部类:通俗来讲就是类里边包含类  一个外部类中可以包含多个内部类

第一种形式的

//外部类
public class Test {
    //内部类
    class Testclass{  
    }

}

第二种形式的:

//RandPicAction-->外部类       Color-->外部类
public class RandPicAction extends ActionSupport {
    private void outputImage1(int iwidth, int iheight, OutputStream os,
            String verifyCode) throws Exception {
        int vclength = verifyCode.length();
        /* 实例化一个图片缓存对象 */
        BufferedImage image = new BufferedImage(iwidth, iheight,
                BufferedImage.TYPE_INT_RGB);
        // 获取图形上下文
        Graphics2D g = image.createGraphics();
        /* 随机数生成器 */
        Random random = new Random();
        /* 消除线性锯齿 */
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        /* 定义5种不同的颜色 */
        Color[] colors = new Color[5];
        Color[] colorSpaces = new Color[] { Color.WHITE, Color.CYAN,
                Color.GRAY, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,
                Color.PINK, Color.YELLOW };
        /* 对颜色分裂并排序 */
        float[] fractions = new float[colors.length];
        for (int i = 0; i < colors.length; i++) {
            colors[i] = colorSpaces[random.nextInt(colorSpaces.length)];
            fractions[i] = random.nextFloat();
        }
        Arrays.sort(fractions);
        /* 画一个矩形框并设置边框颜色 */
        g.setColor(Color.WHITE);// 设置背景色
        g.drawRect(0, 0, iwidth - 10, iheight - 10);
        /* 给矩形框内填充颜色 */
        g.setColor(Color.WHITE);// 设置背景色
        g.fillRect(0, 2, iwidth, iheight - 4);
        /* 扭曲矩形框的图片背景 */
        shear(g, iwidth, iheight, this.getRandColor(220, 250));

        /* 添加噪点 */
        float yawpRate = 0.06f;/* 噪声率 */
        int area = (int) (yawpRate * iwidth * iheight);
        for (int i = 0; i < area; i++) {
            int x = random.nextInt(iwidth);
            int y = random.nextInt(iheight);
            int rgb = getRandomIntColor();
            image.setRGB(x, y, rgb);
        }
        /* 设置随机字体和颜色 */
        g.setColor(this.getRandColor(30, 200));
        g.setColor(Color.BLUE);
        int fontSize = iheight - 15;
        g.setFont(this.getRandFont(fontSize));
        /* 循环字符并一一画出 */
        char[] chars = verifyCode.toCharArray();
        for (int i = 0; i < vclength; i++) {
            /* 仿射变换字符 */
            AffineTransform affine = new AffineTransform();
            affine.setToRotation(
                    Math.PI / 4 * random.nextDouble()
                            * (random.nextBoolean() ? 1 : -1),
                    (iwidth / vclength) * i + fontSize / 2, iheight / 2);
            g.setTransform(affine);
            /* 画出该字符 */
            g.drawChars(chars, i, 1, ((iwidth - 4) / vclength) * i + 8, iheight
                    / 2 + fontSize / 2 - 5);
        }
        /* 产生图像 */
        g.dispose();
        /* 输出图形 */
        ImageIO.write(image, "jpeg", os);
    }

3.内部类编译文件

上图表示这个类中包含3个内部类

暂时只用到了这些 先这样记录 以后在进行补充

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值