分享
 
 
 

Lightweight UI Framework(有產生圓形Button的源碼)

王朝java/jsp·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

Java AWT: Lightweight UI Framework

The ProblemOne 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:

Native windows can be heavyweight, so it's undesirable to have too many of them.

Native windows are opaque, so they can't be used to implement transparent regions.

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 FrameworkThe 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 ComponentsLightweight 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 PanelsThe 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 CodeFollowing 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.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有