文档详情

JSP应用开发与实践 教学课件 作者 刘乃琦 王冲 第14章 综合案例——九宫格日记网.ppt

发布:2017-08-16约5.4万字共106页下载文档
文本预览下载声明
weather = request.getRealPath(images/ + weather + .png); String[] content = (String[]) session.getAttribute(diary); File bgImgFile; //背景图片 if (默认.equals(template)) { bgImgFile = new File(request.getRealPath(images/bg_00.jpg)); Image src = ImageIO.read(bgImgFile); // 构造Image对象 g.drawImage(src, 0, 0, width, height, null); // 绘制背景图片 outWord(g, content, weather, 0, 0); } else if (女孩.equals(template)) { bgImgFile = new File(request.getRealPath(images/bg_01.jpg)); Image src = ImageIO.read(bgImgFile); // 构造Image对象 g.drawImage(src, 0, 0, width, height, null); // 绘制背景图片 outWord(g, content, weather, 25, 110); } else { bgImgFile = new File(request.getRealPath(images/bg_02.jpg)); Image src = ImageIO.read(bgImgFile); // 构造Image对象 g.drawImage(src, 0, 0, width, height, null); // 绘制背景图片 outWord(g, content, weather, 30, 5); } ImageIO.write(image, PNG, response.getOutputStream()); session.setAttribute(diaryImg, image); // 将生成的日记图片保存到Session中 } } 在service()方法的下面编写outWord()方法,用于将九宫格日记的内容写到图片上,具体的代码如下: public void outWord(Graphics g, String[] content, String weather, int offsetX, int offsetY) { Font mFont = new Font(微软雅黑, Font.PLAIN, 26); // 通过Font构造字体 g.setFont(mFont); // 设置字体 g.setColor(new Color(0, 0, 0)); // 设置颜色为黑色 int contentLen = 0; int x = 0; // 文字的横坐标 int y = 0; // 文字的纵坐标 for (int i = 0; i content.length; i++) { contentLen = content[i].length(); // 获取内容的长度 x = 45 + (i % 3) * 170 + offsetX; y = 130 + (i / 3) * 140 + offsetY; 判断当前内容是否为天气,如果是天气,则先获取当前日记,并输出,然后再绘制天气图片。 if (content[i].equals(weathervalue)) { File bgImgFile = new File(weather); mFont = new Font(微软雅黑, Font.PLAIN, 14); // 通过Font构造字体 g.setFont(mFont); // 设置字体 Date date = new Date(); String newTime = new SimpleDateFormat(yyyy年M月d日 E).format(date); g.drawString(newTime, x - 12, y - 60); Image src; try { src = ImageIO.read(bgIm
显示全部
相似文档