Class ZoomContainer

All Implemented Interfaces:
Animation, Editable, StyleListener, Iterable<Component>

public class ZoomContainer extends Container

Shows one component that the user can zoom with a pinch or a double tap and pan with a drag while it is zoomed: a chart, a map drawn as a component, a page of a document.

Label picture = new Label(Image.createImage(400, 300, 0xff336699));
ZoomContainer zoom = new ZoomContainer(picture);
zoom.setMaxScale(4f);
Form f = new Form("Zoom", new BorderLayout());
f.add(BorderLayout.CENTER, zoom);
f.show();

The content is laid out at the container's size and drawn scaled, so it zooms as a picture of itself: text grows instead of reflowing. While zoomed, the content cannot leave the viewport; there is always content to the edge. At scale 1 with no pan, pointer events reach the content's own components as usual. While the content is zoomed or panned the container takes every pointer event over its area, so the content shows but does not respond to taps. A port without affine transforms never zooms: the content stays unscaled and keeps its pointer events.

  • Constructor Details

    • ZoomContainer

      public ZoomContainer(Component content)

      Wraps a component.

      Parameters
      • content: the component to zoom
  • Method Details

    • getContent

      public Component getContent()

      The component being zoomed.

      Returns

      the content

    • getScale

      public float getScale()

      The current zoom.

      Returns

      1 for the content at its laid out size, 2 for twice that

    • setScale

      public void setScale(float scale)

      Zooms around the center of the container.

      Parameters
      • scale: the zoom, clamped to the minimum and maximum
    • zoomAround

      public void zoomAround(float newScale, float fx, float fy)

      Zooms keeping one point of the container still.

      Parameters
      • newScale: the zoom, clamped to the minimum and maximum

      • fx: the x of the still point, relative to the container

      • fy: the y of the still point, relative to the container

    • getMinScale

      public float getMinScale()

      The smallest zoom.

      Returns

      the minimum scale

    • setMinScale

      public void setMinScale(float minScale)

      Sets the smallest zoom.

      Parameters
      • minScale: a positive scale, 1 by default
    • getMaxScale

      public float getMaxScale()

      The largest zoom.

      Returns

      the maximum scale

    • setMaxScale

      public void setMaxScale(float maxScale)

      Sets the largest zoom.

      Parameters
      • maxScale: the maximum scale, 3 by default
    • setDoubleTapScale

      public void setDoubleTapScale(float doubleTapScale)

      Sets the zoom a double tap goes to from the minimum. A double tap while zoomed goes back to the minimum.

      Parameters
      • doubleTapScale: the zoom, 2 by default
    • getPanX

      public float getPanX()

      How far the content is moved from the left edge.

      Returns

      the horizontal offset in pixels, zero or negative while zoomed in

    • getPanY

      public float getPanY()

      How far the content is moved from the top edge.

      Returns

      the vertical offset in pixels, zero or negative while zoomed in

    • getComponentAt

      public Component getComponentAt(int x, int y)
      Description copied from class: Container

      Returns a Component at coordinate (x, y).

      WARNING: This method may return components that are disabled, or invisible, or that do not respond to pointer events. If you are looking for the top-most component that responds to pointer events, you should use int) as it is guaranteed to return a component with Component#respondsToPointerEvents() true; or null if none is found at the coordinate.

      Parameters
      • x: absolute screen location

      • y: absolute screen location

      Returns

      a Component if found, null otherwise

      See also
      • Component#contains

      • #getResponderAt(int, int)

      Overrides:
      getComponentAt in class Container
    • isStickyDrag

      protected boolean isStickyDrag()
      Description copied from class: Component

      Returns true if the component is interested in receiving drag/pointer release events even after the gesture exceeded its boundaries. This is useful for spinners etc. where the motion might continue beyond the size of the component

      Returns

      false by default

      Overrides:
      isStickyDrag in class Component
    • pointerPressed

      public void pointerPressed(int x, int y)
      Description copied from class: Container

      If this Component is focused, the pointer pressed event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerPressed in class Container
    • pointerDragged

      public void pointerDragged(int x, int y)
      Description copied from class: Component

      If this Component is focused, the pointer dragged event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerDragged in class Component
    • pointerReleased

      public void pointerReleased(int x, int y)
      Description copied from class: Component

      If this Component is focused, the pointer released event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerReleased in class Component
    • pinch

      protected boolean pinch(int[] x, int[] y)
      Description copied from class: Component

      Invoked by subclasses interested in handling pinch to do their own actions based on the position of the two fingers, if true is returned other drag events will not be broadcast

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Returns

      false by default, true if pinch is handled

      Overrides:
      pinch in class Component
    • pinch

      protected boolean pinch(float s)
      Description copied from class: Component

      Invoked by subclasses interested in handling pinch to zoom events, if true is returned other drag events will not be broadcast

      Parameters
      • scale: @param scale the scaling of the pinch operation a number larger than 1 means scaling up and smaller than 1 means scaling down. It is recommended that code would threshold the number (so a change between 1.0 and 1.02 shouldn't necessarily trigger zoom). Notice that this number is relevant to current zoom levels and unaware of them so you should also enforce limits of maximum/minimum zoom levels.
      Returns

      false by default

      Overrides:
      pinch in class Component
    • pinchReleased

      protected void pinchReleased(int x, int y)
      Description copied from class: Component

      To be implemented by subclasses interested in being notified when a pinch zoom has ended (i.e the user has removed one of their fingers, but is still dragging).

      Parameters
      • x: The x-coordinate of the remaining finger in the drag. (Absolute)

      • y: The y-coordinate of the remaining finger in the drag. (Absolute)

      Overrides:
      pinchReleased in class Component
    • layoutContainer

      public void layoutContainer()
      Description copied from class: Container
      Performs the layout of the container if a layout is necessary
      Overrides:
      layoutContainer in class Container
    • paint

      public void paint(Graphics g)
      Description copied from class: Container

      This method paints the Component on the screen, it should be overriden by subclasses to perform custom drawing or invoke the UI API's to let the PLAF perform the rendering.

      Parameters
      • g: the component graphics
      Specified by:
      paint in interface Animation
      Overrides:
      paint in class Container