30.17. <w:drawImage>

DrawImage是一个强大的画图的构件,能够在后台用java代码画图,然后生成图片,输出到前端。

30.17.1. 构件信息

表 30.46. 构件信息

Component Typeorg.operamasks.faces.component.widget.UIDrawImage
Component Familyjavax.faces.Graphic
Component Classorg.operamasks.faces.component.widget.UIDrawImage
Tag Classorg.operamasks.faces.webapp.widget.UIDrawImageTag
Renderer Typeorg.operamasks.faces.component.widget.UIDrawImage
Renderer Class(AJAX)org.operamasks.faces.render.widget.ajax.AjaxDrawImageRenderer

继承体系

+java.lang.Object

++javax.faces.component.UIComponent

+++javax.faces.component.UIComponentBase

++++javax.faces.component.UIGraphic

+++++javax.faces.component.html.HtmlGraphicImage

++++++org.operamasks.faces.component.widget.base.UIDrawImageBase

+++++++org.operamasks.faces.component.widget.UIDrawImage

30.17.2. 属性

表 30.47. 属性

alpha 定义类 org.operamasks.faces.component.widget.UIDrawImage
类型 javax.el.ValueExpression(java.lang.Boolean)
是否必须 延时求值
图片是否透明(IE下不起作用)
alt 定义类 javax.faces.component.html.HtmlGraphicImage
类型 javax.el.ValueExpression(java.lang.String)
是否必须 延时求值
当图片不能显示时显示的文字
binding 定义类 javax.faces.component.UIComponentBase
类型 javax.el.ValueExpression(javax.faces.component.UIComponent)
是否必须 延时求值
一个值表达式,用于把该组件链接到一个ManagedBean的某个属性。
dir 定义类 javax.faces.component.html.HtmlGraphicImage
类型 javax.el.ValueExpression(java.lang.String)
是否必须 延时求值
文本的阅读方向,可选值有LTR(从左至右)和RTL(从右至左)
draw 定义类 org.operamasks.faces.component.widget.base.UIDrawImageBase
类型 java.lang.String
是否必须 延时求值
绑定后台的画图方法
dynamic 定义类 org.operamasks.faces.component.widget.base.UIDrawImageBase
类型 javax.el.ValueExpression(boolean)
是否必须 延时求值
图片是否是动态
height 定义类 javax.faces.component.html.HtmlGraphicImage
类型 javax.el.ValueExpression(java.lang.String)
是否必须 延时求值
图片的高度
id 定义类 javax.faces.component.UIComponentBase
类型 java.lang.String
是否必须 延时求值
这个组件的组件标识符。这个值在最近的命名容器类型的父组件范围内,必须是唯一的。
inline 定义类 org.operamasks.faces.component.widget.UIDrawImage
类型 javax.el.ValueExpression(java.lang.Boolean)
是否必须 延时求值
图片是否直接编码到页面中(ie6不支持直接将图片编码到页面中)
ismap 定义类 javax.faces.component.html.HtmlGraphicImage
类型 javax.el.ValueExpression(boolean)
是否必须 延时求值
图片是否是地图
lang 定义类 javax.faces.component.html.HtmlGraphicImage
类型 javax.el.ValueExpression(java.lang.String)
是否必须 延时求值
图片的语言
longdesc 定义类 javax.faces.component.html.HtmlGraphicImage
类型 javax.el.ValueExpression(java.lang.String)
是否必须 延时求值
图片的描述
onclick 定义类 javax.faces.component.html.HtmlGraphicImage
类型 java.lang.String
是否必须 延时求值
onclick事件,使用设备(一般来说是鼠标)单击构件时触发
ondblclick 定义类 javax.faces.component.html.HtmlGraphicImage
类型 java.lang.String
是否必须 延时求值
ondblclick事件,使用设备(一般来说是鼠标)双击构件时触发
onkeydown 定义类 javax.faces.component.html.HtmlGraphicImage
类型 java.lang.String
是否必须 延时求值
onkeydown事件,按下设备(一般是键盘)按钮时触发
onkeypress 定义类 javax.faces.component.html.HtmlGraphicImage
类型 java.lang.String
是否必须 延时求值
onkeypress事件,按下设备(键盘)按钮并释放时触发
onkeyup 定义类 javax.faces.component.html.HtmlGraphicImage
类型 java.lang.String
是否必须 延时求值
onkeyup事件,释放设备(键盘)按钮时触发
onmousedown 定义类 javax.faces.component.html.HtmlGraphicImage
类型 java.lang.String
是否必须 延时求值
onmousedown事件,在构件上按下鼠标时触发
onmousemove 定义类 javax.faces.component.html.HtmlGraphicImage
类型 java.lang.String
是否必须 延时求值
onmousemove事件,鼠标在构件上移动时触发
onmouseout 定义类 javax.faces.component.html.HtmlGraphicImage
类型 java.lang.String
是否必须 延时求值
onmouseout事件,鼠标移出构件时触发
onmouseover 定义类 javax.faces.component.html.HtmlGraphicImage
类型 java.lang.String
是否必须 延时求值
onmouseover,鼠标移到构件上时触发
onmouseup 定义类 javax.faces.component.html.HtmlGraphicImage
类型 java.lang.String
是否必须 延时求值
onmouseup事件,释放鼠标时触发
rendered 定义类 javax.faces.component.UIComponentBase
类型 boolean(boolean)
是否必须 延时求值
一个标志,指出该组件是否要在任何随后的form提交过程中被渲染或处理。 这个属性的缺省值是true。
style 定义类 javax.faces.component.html.HtmlGraphicImage
类型 javax.el.ValueExpression(java.lang.String)
是否必须 延时求值
构件渲染时应用到构件的CSS风格,对应于元素的style属性
styleClass 定义类 javax.faces.component.html.HtmlGraphicImage
类型 javax.el.ValueExpression(java.lang.String)
是否必须 延时求值
空格分隔的CSS风格类名列表,渲染时作为元素的"class"属性
title 定义类 javax.faces.component.html.HtmlGraphicImage
类型 javax.el.ValueExpression(java.lang.String)
是否必须 延时求值
为构件提供的提示信息
type 定义类 org.operamasks.faces.component.widget.UIDrawImage
类型 javax.el.ValueExpression(java.lang.String)
是否必须 延时求值
图片MIME类型,如"image/jpeg"等,缺省为"image/jpeg",(IE下不起作用)
usemap 定义类 javax.faces.component.html.HtmlGraphicImage
类型 javax.el.ValueExpression(java.lang.String)
是否必须 延时求值
图片使用的热点描述的name值
value 定义类 javax.faces.component.UIGraphic
类型 java.lang.String
是否必须 延时求值
图片的URL地址
width 定义类 javax.faces.component.html.HtmlGraphicImage
类型 javax.el.ValueExpression(java.lang.String)
是否必须 延时求值
图片的宽度

30.17.3. 构件API

30.17.3.1. UIDrawImage

表 30.48. 常用方法

refresh 方法签名 public void refresh()
参数说明 无参数
返回值 无返回值
方法描述 刷新图片

30.17.4. 示例

以下是一个验证码的例子,w:drawImage的"draw"属性绑定后台的"draw"方法,该方法负责画图,最终图片就显示在客户端。

<w:drawImage width="60" height="20" draw="#{LoginBean.draw}">

LoginBean的代码:

@ManagedBean(scope = ManagedBeanScope.SESSION) 
public class LoginBean{
 
   ...... 

  public void draw(Graphics g, int width, int height) { 
    g.setColor(randColor(200, 250)); 
    g.fillRect(0, 0, width, height); 
    g.setFont(new Font("Times New Roman", Font.PLAIN, 18)); 
    g.setColor(randColor(160, 200)); 
    // 随机产生155条干扰线,使图像中的验证码不易被识别 
    for (int i = 0; i &amp;amp;lt; 155; i++) { 
      int x = random.nextInt(width); 
      int y = random.nextInt(height); 
      int xl = random.nextInt(12); 
      int yl = random.nextInt(12); 
      g.drawLine(x, y, x + xl, y + yl); 
    } 
    // 将验证码显示在图像中 
    String code = randCode(); 
    for (int i = 0; i &amp;amp;lt; 4; i++) { 
      g.setColor(randColor(20,130)); 
      g.drawString(code.substring(i,i+1), 13 * i + 6, 16); 
    } 
  } 

  ...... 

}