Class WPaintedWidget

Direct Known Subclasses:
WAbstractChart, WAxisSliderWidget

public abstract class WPaintedWidget
extends WInteractWidget
A widget that is painted using vector graphics.

A painted widget is rendered from basic drawing primitives. Rendering is done not on the server but on the browser, using different rendering methods:

BrowserMethods Default method
Firefox 1.5+HtmlCanvas, InlineSVG, PngImage HtmlCanvas
Internet Explorer 6.0+InlineVML, PngImage InlineVML
Internet Explorer 9+HtmlCanvas, InlineSVG, PngImage HtmlCanvas
SafariHtmlCanvas, InlineSVG, PngImage HtmlCanvas
OperaInlineSVG, HtmlCanvas*, PngImage InlineSVG
other?HtmlCanvas, PngImage

* HtmlCanvas occasionally suffers from rendering artefacts in Opera.

The different rendering methods correspond to different WPaintDevice implementations, from which this widget choses a suitable one depending on the browser capabilities and configuration.

If no JavaScript is available, the JavaScript-based HtmlCanvas will not be used, and InlineSVG will be used instead. The method used may be changed by using setPreferredMethod().

In some browsers, InlineSVG requires that the document is rendered as XHTML. This must be enabled in the configuration file using the <send-xhtml-mime-type> option. By default, this option is off. Firefox 4 and Chrome do support svg in normal html mode.

The PngImage is the most portable rendering method, and may be the fastest if the painting is of high complexity and/or the image is fairly small.

To use a WPaintedWidget, you must derive from it and reimplement paintEvent(). To paint on a WPaintDevice, you will need to use a WPainter. Repainting is triggered by calling the update() method.

CSS

Styling through CSS is not applicable.

Note: A WPaintedWidget requires that it is given a size using resize() or by a layout manager.

Client side interaction and repainting

If the widget is drawn as an HTML canvas element, i.e. the method is HtmlCanvas, a WPaintedWidget can expose certain objects to be modified client side.

See Also:
WJavaScriptHandle, WJavaScriptExposableObject, WImage
  • Constructor Details

  • Method Details

    • remove

      public void remove()
      Destructor.
      Overrides:
      remove in class WInteractWidget
      See Also:
      WContainerWidget.removeWidget(WWidget widget)
    • setPreferredMethod

      public void setPreferredMethod​(WPaintedWidget.Method method)
      Sets the preferred rendering method.

      When method is supported by the browser, then it is chosen for rendering.

    • getPreferredMethod

      public WPaintedWidget.Method getPreferredMethod()
      Returns the preferred rendering method.

      See Also:
      setPreferredMethod(WPaintedWidget.Method method)
    • update

      public void update​(java.util.EnumSet<PaintFlag> flags)
      Lets the widget repaint itself.

      Repainting is not immediate, but happens after when the event loop is exited.

      Unless a PaintFlag.PaintUpdate paint flag is set, the widget is first cleared.

    • update

      public final void update​(PaintFlag flag, PaintFlag... flags)
      Lets the widget repaint itself.

      Calls update(EnumSet.of(flag, flags))

    • update

      public final void update()
      Lets the widget repaint itself.

      Calls update(EnumSet.noneOf(PaintFlag.class))

    • resize

      public void resize​(WLength width, WLength height)
      Description copied from class: WWidget
      Resizes the widget.

      Specifies a fixed size for this widget, setting CSS width and height properties. By default a widget has automatic width and height, which sets a size for the widget following CSS rules.

      When the widget is not managed by a layout manager, the automatic (natural) size of a widget depends on whether they widget is a block or inline widget:

      • a block widget takes by default the width of the parent, and the height that it needs based on its contents
      • an inline widget takes the width and height that it needs based on its contents (possibly wrapping over multiple lines). The width and height of an inline widget cannot be changed (by the letter of CSS, although most browsers will react to it in varying ways).

      When inserted in a layout manager, the size set will be used as a widget's preferred size, but the widget may be given a different size by the layout manager based on available space and stretch factors. The actual size given by a layout manager may be retrieved by making the widget "layout size aware", using setLayoutSizeAware(). If you have defined a "wtResize()" JavaScript method for the widget, then this method will also be called.

      The default width and height of a widget is WLength.Auto.

      Overrides:
      resize in class WWebWidget
      See Also:
      WWidget.getWidth(), WWidget.getHeight()
    • addArea

      public void addArea​(WAbstractArea area)
      Adds an interactive area.

      Adds the area which listens to events in a specific region of the widget. Areas are organized in a list, to which the given area is appended. When areas overlap, the area with the lowest index receives the event.

      Ownership of the area is transferred to the widget.

      Note: When defining at least one area, no more events will propagate to the widget itself. As a work-around, you can emulate this by listening for events on a WRectArea that corresponds to the whole widget, and which is added as the last area (catching all events that were not caught by preceding areas).

      See Also:
      insertArea(int index, WAbstractArea area)
    • insertArea

      public void insertArea​(int index, WAbstractArea area)
      Inserts an interactive area.

      Inserts the area which listens to events in the coresponding area of the widget. Areas are organized in a list, and the area is inserted at index index . When areas overlap, the area with the lowest index receives the event.

      Ownership of the Area is transferred to the widget.

      Note: When defining at least one area, no more events will propagate to the widget itself. As a work-around, you can emulate this by listening for events on a WRectArea that corresponds to the whole widget, and which is added as the last area (catching all events that were not caught by preceding areas).

      See Also:
      addArea(WAbstractArea area)
    • removeArea

      public void removeArea​(WAbstractArea area)
      Removes an interactive area.

      Removes the area from this widget, returning the ownership.

      See Also:
      addArea(WAbstractArea area)
    • getArea

      public WAbstractArea getArea​(int index)
      Returns the interactive area at the given index.

      Returns null if index was invalid.

      See Also:
      insertArea(int index, WAbstractArea area)
    • getAreas

      public java.util.List<WAbstractArea> getAreas()
      Returns the interactive areas set for this widget.

      See Also:
      addArea(WAbstractArea area)
    • getRepaintSlot

      public JSlot getRepaintSlot()
      A JavaScript slot that repaints the widget when triggered.

      This is useful for client-side initiated repaints. You may want to use this if you want to add interaction or animation to your WPaintedWidget.

      Note: This feature is currently only supported if the method is HtmlCanvas. This will not cause a server roundtrip. Instead, the resulting JavaScript of paintEvent() will be re-executed on the client side.

      See Also:
      getObjJsRef()
    • createJSTransform

      protected WJavaScriptHandle<WTransform> createJSTransform()
      Create a WTransform that is accessible from JavaScript, associated with this WPaintedWidget.
    • createJSBrush

      protected WJavaScriptHandle<WBrush> createJSBrush()
      Create a WBrush that is accessible from JavaScript, associated with this WPaintedWidget.
    • createJSPen

      protected WJavaScriptHandle<WPen> createJSPen()
      Create a WPen that is accessible from JavaScript, associated with this WPaintedWidget.
    • createJSPainterPath

      protected WJavaScriptHandle<WPainterPath> createJSPainterPath()
      Create a WPainterPath that is accessible from JavaScript, associated with this WPaintedWidget.
    • createJSRect

      protected WJavaScriptHandle<WRectF> createJSRect()
      Create a WRectF that is accessible from JavaScript, associated with this WPaintedWidget.
    • createJSPoint

      protected WJavaScriptHandle<WPointF> createJSPoint()
      Create a WPointF that is accessible from JavaScript, associated with this WPaintedWidget.
    • layoutSizeChanged

      protected void layoutSizeChanged​(int width, int height)
      Description copied from class: WWidget
      Virtual method that indicates a size change.

      This method propagates the client-side width and height of the widget when the widget is contained by a layout manager and setLayoutSizeAware(true) was called.

      Overrides:
      layoutSizeChanged in class WWidget
      See Also:
      WWidget.setLayoutSizeAware(boolean aware)
    • getMethod

      protected WPaintedWidget.Method getMethod()
      Returns the actual method used for rendering.

      The default method considers browser capabilites and the preferred method to make an actual choice for the implementation.

      You may want to reimplement this method to override this choice.

    • paintEvent

      protected abstract void paintEvent​(WPaintDevice paintDevice)
      Paints the widget.

      You should reimplement this method to paint the contents of the widget, using the given paintDevice.

    • getCreatePaintDevice

      protected WPaintDevice getCreatePaintDevice()
      Creates a paint device.

      Although it's usually not necessary to call this function, you may want to reimplement this function to customize or specialize the device used for painting the widget.

    • createDomElement

      protected DomElement createDomElement​(WApplication app)
      Description copied from class: WWebWidget
      Create DOM element for widget.

      This is an internal function, and should not be called directly, or be overridden!

      Overrides:
      createDomElement in class WWebWidget
    • getDomChanges

      protected void getDomChanges​(java.util.List<DomElement> result, WApplication app)
      Description copied from class: WWebWidget
      Get DOM changes for this widget.

      This is an internal function, and should not be called directly, or be overridden!

      Overrides:
      getDomChanges in class WWebWidget
    • enableAjax

      protected void enableAjax()
      Description copied from class: WWidget
      Progresses to an Ajax-enabled widget.

      This method is called when the progressive bootstrap method is used, and support for AJAX has been detected. The default behavior will upgrade the widget's event handling to use AJAX instead of full page reloads, and propagate the call to its children.

      You may want to reimplement this method if you want to make changes to widget when AJAX is enabled. You should always call the base implementation.

      Overrides:
      enableAjax in class WWebWidget
      See Also:
      WApplication.enableAjax()
    • render

      protected void render​(java.util.EnumSet<RenderFlag> flags)
      Description copied from class: WWidget
      Renders the widget.

      This function renders the widget (or an update for the widget), after this has been scheduled using scheduleRender().

      The default implementation will render the widget by serializing changes to JavaScript and HTML. You may want to reimplement this widget if you have been postponing some of the layout / rendering implementation until the latest moment possible. In that case you should make sure you call the base implementation however.

      Overrides:
      render in class WWebWidget
    • setFormData

      protected void setFormData​(WObject.FormData formData)
      Overrides:
      setFormData in class WObject
    • getObjJsRef

      protected java.lang.String getObjJsRef()
      Returns a JavaScript reference to the client side representation of the WPaintedWidget.

      The client side representation exposes the following interface:

      
       {
       canvas: exposes the underlying HTML canvas element
       repaint: a function that, when called, will repaint the widget without a server roundtrip
       }
      
       

      Note: The method should be HtmlCanvas and there has to be at least one WJavaScriptHandle associated with this WPaintedWidget in order for this reference to be valid.