登录社区:用户名: 密码: 忘记密码 网页功能:加入收藏 设为首页 网站搜索  

文档

下载

图书

论坛

安全

源码

硬件

游戏
首页 信息 空间 VB VC Delphi Java Flash 补丁 控件 安全 黑客 电子书 笔记本 手机 MP3 杀毒 QQ群 产品库 分类信息 编程网站
  立华软件园 - 安全技术中心 - 技术文档 - JAVA 技术文章 | 相关下载 | 电子图书 | 攻防录像 | 安全网站 | 在线论坛 | QQ群组 | 搜索   
 安全技术技术文档
  · 安全配制
  · 工具介绍
  · 黑客教学
  · 防火墙
  · 漏洞分析
  · 破解专题
  · 黑客编程
  · 入侵检测
 安全技术工具下载
  · 扫描工具
  · 攻击程序
  · 后门木马
  · 拒绝服务
  · 口令破解
  · 代理程序
  · 防火墙
  · 加密解密
  · 入侵检测
  · 攻防演示
 安全技术论坛
  · 安全配制
  · 工具介绍
  · 防火墙
  · 黑客入侵
  · 漏洞检测
  · 破解方法
 其他安全技术资源
  · 攻防演示动画
  · 电子图书
  · QQ群组讨论区
  · 其他网站资源
最新招聘信息

Lightweight UI Framework(有?a生?A形Button的源?a)
发表日期:2004-07-30作者:[转贴] 出处:  


Java AWT: Lightweight UI Framework

The Problem

One of the issues with the 1.0 AWT is that creating new components requires creating subclasses of java.awt.Canvas or java.awt.Panel, which means that each new component owns its own opaque native window. This one-to-one mapping between components and native windows results in three problems:
  1. Native windows can be heavyweight, so it's undesirable to have too many of them.
  2. Native windows are opaque, so they can't be used to implement transparent regions.
  3. Native windows are handled differently across platforms, so the AWT has to struggle to maintain a consistent view across these varied platforms.
Hooks have been implemented that enable the creation of "lightweight" UI components; these hooks are called the Lightweight UI Framework.

Lightweight UI Framework

The Lightweight UI Framework is very simple -- it boils down to the ability to now directly extend the java.awt.Component and java.awt.Container classes in order to create components which do not have native opaque windows associated with them. These lightweight components and containers fit right into the existing AWT models, such as painting, layout, and events, and as such, require no special handling or additional APIs. Existing subclasses of Canvas and Panel can be easily migrated to lightweight versions by simply changing their superclass appropriately.

The advantages of creating lightweight components are the following:

  • The Lightweight component can now have transparent areas by simply not rendering to those areas in its paint() method (although, until we get full shape support from Java2D, the bounding box will remain rectangular).
  • The Lightweight component is "lighter" in that it requires no native data-structures or peer classes.
  • There is no native code required to process lightweight components, hence handling of lightweights is 100% implemented in common java code, which leads to complete consistency across platforms.

We are using this framework in an upcoming version of the toolkit (beyond 1.1) to implement pure-java versions of the base UI controls (Button, List, etc.) which implement a common look-and-feel across the platforms (and don't use the native peers).

Mixing Lightweight & Heavyweight Components

Lightweight components can be freely mixed with existing heavyweight components. This means that lightweight components can be made direct children of heavyweight containers, heavyweight components can be made direct children of lightweight containers, and heavyweight and lightweights can be mixed within containers (with the one caveat that the heavyweight sibling will always be "on top" if it overlaps with a lightweight, regardless of the specified z-order).

Putting Lightweight components in Existing Panels

The painting and event dispatching mechanism for lightweight components is handled by the Container class. This means that the painting of lightweight components is triggered from within the paint() method of its container. Therefore, if a lightweight component is placed inside of a Container instance where the paint method has been overridden but which does not call super.paint(), the paint() method of the lightweight component will never be called. This could be a common occurrence if you're using existing classes which extend Panel in order to implement the painting of a border or bevel, but which don't call "super.paint()" (because it was not an issue with 1.0.2). So if your lightweight components are not showing up, this is the first thing to check!

Double Buffering

Because lightweight components are entirely rendered in Java, the use of double-buffering in their containers can really smooth out their rendering to avoid flashing. By default, the Container class does not implement double-buffering, but this is extremely easy to do! Following is an example of a double-buffered Panel which implements smooth rendering for any lightweight components placed inside it:
public class DoubleBufferPanel extends Panel {      Image offscreen;  /**   * null out the offscreen buffer as part of invalidation   */  public void invalidate() {      super.invalidate();      offscreen = null;  }  /**   * override update to *not* erase the background before painting   */  public void update(Graphics g) {      paint(g);  }  /**   * paint children into an offscreen buffer, then blast entire image   * at once.   */  public void paint(Graphics g) {      if(offscreen == null) {         offscreen = createImage(getSize().width, getSize().height);      }      Graphics og = offscreen.getGraphics();      og.setClip(0,0,getSize().width, getSize().height);      super.paint(og);      g.drawImage(offscreen, 0, 0, null);      og.dispose();  }}

Sample Code

Following is sample code showing the creation of a lightweight round button class, which shows off the transparency aspect of lightweight components.
import java.lang.*;import java.util.*;import java.awt.*;import java.awt.event.*;/** * RoundButton - a class that produces a lightweight button. */public class RoundButton extends Component {  String label;                      // The Button's text  protected boolean pressed = false; // true if the button is detented.    /**   * Constructs a RoundButton with the specified label.   * @param label the label of the button   */  public RoundButton(String label) {      this.label = label;      enableEvents(AWTEvent.MOUSE_EVENT_MASK);  }    /**   * paints the RoundButton   */  public void paint(Graphics g) {      int s = Math.min(getSize().width - 1, getSize().height - 1);            // paint the interior of the button      if(pressed) {          g.setColor(getBackground().darker().darker());      } else {          g.setColor(getBackground());      }      g.fillArc(0, 0, s, s, 0, 360);            // draw the perimeter of the button      g.setColor(getBackground().darker().darker().darker());      g.drawArc(0, 0, s, s, 0, 360);            // draw the label centered in the button      Font f = getFont();      if(f != null) {          FontMetrics fm = getFontMetrics(getFont());          g.setColor(getForeground());          g.drawString(label,                       s/2 - fm.stringWidth(label)/2,                       s/2 + fm.getMaxDescent());      }  }    /**   * The preferred size of the button.    */  public Dimension getPreferredSize() {      Font f = getFont();      if(f != null) {          FontMetrics fm = getFontMetrics(getFont());          int max = Math.max(fm.stringWidth(label) + 40, fm.getHeight() + 40);          return new Dimension(max, max);      } else {          return new Dimension(100, 100);      }  }    /**   * The minimum size of the button.    */  public Dimension getMinimumSize() {      return new Dimension(100, 100);  }      /**    * Paints the button and distribute an action event to all listeners.    */   public void processMouseEvent(MouseEvent e) {       Graphics g;       switch(e.getID()) {          case MouseEvent.MOUSE_PRESSED:            // render myself inverted....            pressed = true;            // Repaint might flicker a bit. To avoid this, you can use            // double buffering (see the Gauge example).            repaint();             break;          case MouseEvent.MOUSE_RELEASED:            // render myself normal again            if(pressed == true) {                pressed = false;                // Repaint might flicker a bit. To avoid this, you can use                // double buffering (see the Gauge example).                repaint();            }            break;          case MouseEvent.MOUSE_ENTERED:            break;          case MouseEvent.MOUSE_EXITED:            if(pressed == true) {                // Cancel! Don't send action event.                pressed = false;                // Repaint might flicker a bit. To avoid this, you can use                // double buffering (see the DoubleBufferPanel example above).                repaint();                // Note: for a more complete button implementation,                // you wouldn't want to cancel at this point, but                // rather detect when the mouse re-entered, and                // re-highlight the button. There are a few state                // issues that that you need to handle, which we leave                // this an an excercise for the reader (I always                // wanted to say that!)            }            break;       }       super.processMouseEvent(e);   }   }


Send feedback to:java-awt@java.sun.com
Copyright © 1997, Sun Microsystems, Inc. All rights reserved.

我来说两句】 【发送给朋友】 【加入收藏】 【返加顶部】 【打印本页】 【关闭窗口
中搜索 Lightweight UI Framework(有?a生?A形Button的源?a)

 ■ [欢迎对本文发表评论]
用  户:  匿名发出:
您要为您所发的言论的后果负责,故请各位遵纪守法并注意语言文明。

最新招聘信息

关于我们 / 合作推广 / 给我留言 / 版权举报 / 意见建议 / 广告投放 / 友情链接  
Copyright ©2001-2006 Lihuasoft.net webmaster(at)lihuasoft.net
网站编程QQ群   京ICP备05001064号 页面生成时间:0.0016