最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

Java AWT: Lightweight UI Framework

工作和技术 crifan 1681浏览 0评论

Duke logo doing a cartwheel Java AWT: Lightweight UI Framework

from jdk150 English Version


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:

<code>
public class DoubleBufferPanel extends Panel {&nbsp;&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp; Image offscreen;

&nbsp;&nbsp; /**
&nbsp;&nbsp;&nbsp; * null out the offscreen buffer as part of invalidation
&nbsp;&nbsp;&nbsp; */
&nbsp;&nbsp; public void invalidate() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; super.invalidate();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; offscreen = null;
&nbsp;&nbsp; }

&nbsp;&nbsp; /**
&nbsp;&nbsp;&nbsp; * override update to *not* erase the background before painting
&nbsp;&nbsp;&nbsp; */
&nbsp;&nbsp; public void update(Graphics g) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; paint(g);
&nbsp;&nbsp; }

&nbsp;&nbsp; /**
&nbsp;&nbsp;&nbsp; * paint children into an offscreen buffer, then blast entire image
&nbsp;&nbsp;&nbsp; * at once.
&nbsp;&nbsp;&nbsp; */
&nbsp;&nbsp; public void paint(Graphics g) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(offscreen == null) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; offscreen = createImage(getSize().width, getSize().height);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Graphics og = offscreen.getGraphics();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; og.setClip(0,0,getSize().width, getSize().height);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; super.paint(og);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; g.drawImage(offscreen, 0, 0, null);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; og.dispose();
&nbsp;&nbsp; }
}
</code>

Sample Code

Following is sample code showing the creation of a lightweight round button class, which shows off the transparency aspect of lightweight components.

<code>
import java.lang.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

/**
 * RoundButton - a class that produces a lightweight button.
 */
public class RoundButton <strong>extends Component</strong> {

&nbsp;&nbsp; String label;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // The Button's text
&nbsp;&nbsp; protected boolean pressed = false; // true if the button is detented.
&nbsp;&nbsp; 
&nbsp;&nbsp; /**
&nbsp;&nbsp;&nbsp; * Constructs a RoundButton with the specified label.
&nbsp;&nbsp;&nbsp; * @param label the label of the button
&nbsp;&nbsp;&nbsp; */
&nbsp;&nbsp; public RoundButton(String label) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.label = label;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; enableEvents(AWTEvent.MOUSE_EVENT_MASK);
&nbsp;&nbsp; }
&nbsp;&nbsp; 
&nbsp;&nbsp; /**
&nbsp;&nbsp;&nbsp; * paints the RoundButton
&nbsp;&nbsp;&nbsp; */
&nbsp;&nbsp; public void paint(Graphics g) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int s = Math.min(getSize().width - 1, getSize().height - 1);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // paint the interior of the button
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(pressed) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; g.setColor(getBackground().darker().darker());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; g.setColor(getBackground());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; g.fillArc(0, 0, s, s, 0, 360);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // draw the perimeter of the button
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; g.setColor(getBackground().darker().darker().darker());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; g.drawArc(0, 0, s, s, 0, 360);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // draw the label centered in the button
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Font f = getFont();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(f != null) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FontMetrics fm = getFontMetrics(getFont());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; g.setColor(getForeground());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; g.drawString(label,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s/2 - fm.stringWidth(label)/2,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s/2 + fm.getMaxDescent());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp; }
&nbsp;&nbsp; 
&nbsp;&nbsp; /**
&nbsp;&nbsp;&nbsp; * The preferred size of the button. 
&nbsp;&nbsp;&nbsp; */
&nbsp;&nbsp; public Dimension getPreferredSize() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Font f = getFont();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(f != null) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FontMetrics fm = getFontMetrics(getFont());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int max = Math.max(fm.stringWidth(label) + 40, fm.getHeight() + 40);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return new Dimension(max, max);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return new Dimension(100, 100);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp; }
&nbsp;&nbsp; 
&nbsp;&nbsp; /**
&nbsp;&nbsp;&nbsp; * The minimum size of the button. 
&nbsp;&nbsp;&nbsp; */
&nbsp;&nbsp; public Dimension getMinimumSize() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return new Dimension(100, 100);
&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; /**
&nbsp;&nbsp;&nbsp;&nbsp; * Paints the button and distribute an action event to all listeners.
&nbsp;&nbsp;&nbsp;&nbsp; */
&nbsp;&nbsp;&nbsp; public void processMouseEvent(MouseEvent e) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Graphics g;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; switch(e.getID()) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case MouseEvent.MOUSE_PRESSED:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // render myself inverted....
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pressed = true;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Repaint might flicker a bit. To avoid this, you can use
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // double buffering (see the Gauge example).
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; repaint(); 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case MouseEvent.MOUSE_RELEASED:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // render myself normal again
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(pressed == true) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pressed = false;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Repaint might flicker a bit. To avoid this, you can use
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // double buffering (see the Gauge example).
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; repaint();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case MouseEvent.MOUSE_ENTERED:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case MouseEvent.MOUSE_EXITED:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(pressed == true) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Cancel! Don't send action event.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pressed = false;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Repaint might flicker a bit. To avoid this, you can use
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // double buffering (see the DoubleBufferPanel example above).
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; repaint();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Note: for a more complete button implementation,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // you wouldn't want to cancel at this point, but
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // rather detect when the mouse re-entered, and
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // re-highlight the button. There are a few state
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // issues that that you need to handle, which we leave
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // this an an excercise for the reader (I always
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // wanted to say that!)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; super.processMouseEvent(e);
&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; 
}
</code>
Last Updated: January 31, 1997

The Problem


转载请注明:在路上 » Java AWT: Lightweight UI Framework

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
79 queries in 0.192 seconds, using 22.18MB memory