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个内部类
暂时只用到了这些 先这样记录 以后在进行补充