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 not 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 Implement That Enable The Creation of "Lightweight" UI Components; The 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
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 .
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 do not 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" 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.GRAPHICs ();
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. *;
/ **
. '
* /
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 runkbutton
* /
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 (). 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 GETMINIMUMUMSIZE () {
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 'TEND 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 completion Button Implement,
// you would''t Want to Cancel At this Point, But
// Rather Detect When a mouse re-entered, And
// RE-highlight the button. there is a few state
// Issues That That You Need to Handle, Which We Leave
// this an an excercise for the reader (i always
// Wanded to sign That!)
}
Break;
}
Super.ProcessMouseEvent (e);
}
}
Send feedback to: java-awt@java.sun.com