Class Graphics

java.lang.Object
com.codename1.ui.Graphics

public final class Graphics extends Object

Abstracts the underlying platform graphics context thus allowing us to achieve portability between MIDP devices and CDC devices. This abstaction simplifies and unifies the Graphics implementations of various platforms.

A graphics instance should never be created by the developer and is always accessed using either a paint callback or a mutable image. There is no supported way to create this object directly.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Rendering hint to indicate that the context should prefer to render primitives in a quick way, at the cost of quality, if there is an expensive operation.
  • Method Summary

    Modifier and Type
    Method
    Description
    Starts accessing the native graphics in the underlying OS, when accessing the native graphics Codename One shouldn't be used! The native graphics is unclipped and untranslated by default and its the responsibility of the caller to clip/translate appropriately.
    void
    clearRect(int x, int y, int width, int height)
    Clears rectangular area of the graphics context.
    void
    clipRect(int x, int y, int width, int height)
    Clips the given rectangle by intersecting with the current clipping region, this method can thus only shrink the clipping region and never increase it.
    int
    Concatenates the given alpha value to the current alpha setting, and returns the previous alpha setting.
    void
    darkerColor(int factor)
    Makes the current color slightly darker, this is useful for many visual effects
    void
    drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
    Draws a circular or elliptical arc based on the given angles and bounding box
    void
    drawChar(char character, int x, int y)
    Draw the given char using the current font and color in the x,y coordinates.
    void
    drawChars(char[] data, int offset, int length, int x, int y)
    Draw the given char array using the current font and color in the x,y coordinates.
    void
    drawImage(Image img, int x, int y)
    Draws the image so its top left coordinate corresponds to x/y
    void
    drawImage(Image img, int x, int y, int w, int h)
    Draws the image so its top left coordinate corresponds to x/y and scales it to width/height
    void
    drawLine(int x1, int y1, int x2, int y2)
    Draws a line between the 2 X/Y coordinates
    void
    drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
    Draws a closed polygon defined by arrays of x and y coordinates.
    void
    drawRect(int x, int y, int width, int height)
    Draws a rectangle in the given coordinates
    void
    drawRect(int x, int y, int width, int height, int thickness)
    Draws a rectangle in the given coordinates with the given thickness
    void
    drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
    Draws a rounded corner rectangle in the given coordinates with the arcWidth/height matching the last two arguments respectively.
    void
    drawShadow(Image img, int x, int y, int offsetX, int offsetY, int blurRadius, int spreadRadius, int color, float opacity)
    Deprecated
    void
    drawShape(Shape shape, Stroke stroke)
    Draws a outline shape inside the specified bounding box.
    void
    drawString(String str, int x, int y)
    Draw a string using the current font and color in the x,y coordinates.
    void
    drawString(String str, int x, int y, int textDecoration)
    Draw a string using the current font and color in the x,y coordinates.
    void
    drawStringBaseline(String str, int x, int y)
    Draws a string using baseline coordinates.
    void
    drawStringBaseline(String str, int x, int y, int textDecoration)
    Draws a string using baseline coordinates.
    void
    Invoke this to restore Codename One's graphics settings into the native graphics
    void
    fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
    Fills a circular or elliptical arc based on the given angles and bounding box.
    void
    fillLinearGradient(int startColor, int endColor, int x, int y, int width, int height, boolean horizontal)
    Draws a linear gradient in the given coordinates with the given colors, doesn't take alpha into consideration when drawing the gradient
    void
    fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
    Fills a closed polygon defined by arrays of x and y coordinates.
    void
    fillRadialGradient(int startColor, int endColor, int x, int y, int width, int height)
    Draws a radial gradient in the given coordinates with the given colors, doesn't take alpha into consideration when drawing the gradient.
    void
    fillRadialGradient(int startColor, int endColor, int x, int y, int width, int height, int startAngle, int arcAngle)
    Draws a radial gradient in the given coordinates with the given colors, doesn't take alpha into consideration when drawing the gradient.
    void
    fillRect(int x, int y, int width, int height)
    Fills the rectangle from the given position according to the width/height minus 1 pixel according to the convention in Java.
    void
    fillRect(int x, int y, int w, int h, byte alpha)
    Fills a rectangle with an optionally translucent fill color
    void
    fillRectRadialGradient(int startColor, int endColor, int x, int y, int width, int height, float relativeX, float relativeY, float relativeSize)
    Draws a radial gradient in the given coordinates with the given colors, doesn't take alpha into consideration when drawing the gradient.
    void
    fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
    Fills a rounded rectangle in the same way as drawRoundRect
    void
    Fills the given shape using the current alpha and color settings.
    void
    fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3)
    Draws a filled triangle with the given coordinates
    protected void
     
    int
    Returns the alpha as a value between 0-255 (0 - 0xff) where 255 is completely opaque and 0 is completely transparent
    int[]
    Returns the clip as an x,y,w,h array
    int
    Returns the clip height
    int
    Returns the clip width
    int
    Returns the x clipping position
    int
    Returns the y clipping position
    int
    Returns the current color
    Returns the font used with the drawString method calls
    Gets the current Paint that is set to be used for filling shapes.
    int
    Gets the current rendering hints for this context.
    float
    Returns the affine X scale
    float
    Returns the affine Y scale
    Gets the transformation matrix that is currently applied to this graphics context.
    void
    Loads the provided transform with the current transform applied to this graphics context.
    int
    Returns the current x translate value
    int
    Returns the current y translate value
    boolean
    Indicates whether the underlying implementation can draw using an affine transform hence methods such as rotate, scale and shear would work
    boolean
    Indicates whether invoking set/getAlpha would have an effect on all further rendering from this graphics object.
    boolean
    Returns true if antialiasing for standard rendering operations is turned on.
    boolean
    Indicates whether antialiasing for text is active, notice that text antialiasing is a separate attribute from standard anti-alisaing.
    boolean
    Returns true if antialiasing for text is supported, notice that text antialiasing is a separate attribute from standard anti-alisaing.
    boolean
    Returns true if antialiasing for standard rendering operations is supported, notice that text antialiasing is a separate attribute.
    boolean
    Checks to see if perspective (3D) com.codename1.ui.geom.Matrix transforms are supported by this graphics context.
    boolean
    Checks to see if this graphics context supports clip Shape.
    boolean
    Checks to see if this graphics context supports drawing shapes (i.e.
    boolean
    Checks to see if com.codename1.ui.geom.Matrix transforms are supported by this graphics context.
    void
    lighterColor(int factor)
    Makes the current color slightly lighter, this is useful for many visual effects
    void
    Pops the top clip from the clip stack and sets it as the current clip.
    void
    Pushes the current clip onto the clip stack.
    void
    Resets the affine transform to the default value
    void
    rotate(float angle)
    Rotates the coordinate system around a radian angle using the affine transform
    void
    rotate(float angle, int pivotX, int pivotY)
    Rotates the coordinate system around a radian angle using the affine transform
    void
    rotateRadians(float angle)
    RRotates the coordinate system around a radian angle using the affine transform
    void
    rotateRadians(float angle, int pivotX, int pivotY)
    Rotates the coordinate system around a radian angle using the affine transform
    void
    scale(float x, float y)
    Scales the coordinate system using the affine transform
    void
    setAlpha(int a)
    Sets alpha as a value between 0-255 (0 - 0xff) where 255 is completely opaque and 0 is completely transparent
    int
    Sets alpha as a value between 0-255 (0 - 0xff) where 255 is completely opaque and 0 is completely transparent
    int
    setAndGetColor(int rgb)
    Sets the current rgb color while ignoring any potential alpha component within said color value.
    void
    setAntiAliased(boolean a)
    Set whether antialiasing for standard rendering operations is turned on.
    void
    setAntiAliasedText(boolean a)
    Set whether antialiasing for text is active, notice that text antialiasing is a separate attribute from standard anti-alisaing.
    void
    setClip(int[] clip)
    Sets the clip from an array containing x, y, width, height value
    void
    setClip(int x, int y, int width, int height)
    Updates the clipping region to match the given region exactly
    void
    setClip(Shape shape)
    Clips the Graphics context to the Shape.
    void
    setColor(int rgb)
    Sets the current rgb color while ignoring any potential alpha component within said color value.
    void
    setColor(Paint paint)
    Sets paint to be used for filling shapes.
    void
    setFont(Font font)
    Sets the font to use with the drawString method calls
    void
    setRenderingHints(int hints)
    Sets rendering hints for this context.
    void
    Sets the transformation com.codename1.ui.geom.Matrix to apply to drawing in this graphics context.
    void
    shear(float x, float y)
    Shear the graphics coordinate system using the affine transform
    void
    tileImage(Image img, int x, int y, int w, int h)
    Allows an implementation to optimize image tiling rendering logic
    void
    transform(Transform transform)
    Concatenates the given transform to the context's transform.
    void
    translate(int x, int y)
    Translates the X/Y location for drawing on the underlying surface.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • RENDERING_HINT_FAST

      public static final int RENDERING_HINT_FAST

      Rendering hint to indicate that the context should prefer to render primitives in a quick way, at the cost of quality, if there is an expensive operation.

      Since

      7.0

      See also
      • #setRenderingHints(int)

      • #getRenderingHints()

      See Also:
  • Method Details

    • finalize

      protected void finalize()
      Overrides:
      finalize in class Object
    • translate

      public void translate(int x, int y)

      Translates the X/Y location for drawing on the underlying surface. Translation is incremental so the new value will be added to the current translation and in order to reset translation we have to invoke translate(-getTranslateX(), -getTranslateY())

      Parameters
      • x: the x coordinate

      • y: the y coordinate

    • getTranslateX

      public int getTranslateX()

      Returns the current x translate value

      Returns

      the current x translate value

    • getTranslateY

      public int getTranslateY()

      Returns the current y translate value

      Returns

      the current y translate value

    • getColor

      public int getColor()

      Returns the current color

      Returns

      the RGB graphics color

    • setColor

      public void setColor(int rgb)

      Sets the current rgb color while ignoring any potential alpha component within said color value.

      Parameters
      • rgb: the RGB value for the color.
    • setColor

      public void setColor(Paint paint)

      Sets paint to be used for filling shapes. This is only used for the #fillShape(com.codename1.ui.geom.Shape) method.

      Parameters
      • paint
      Since

      7.0

      See also
      • LinearGradientPaint
    • getPaint

      public Paint getPaint()

      Gets the current Paint that is set to be used for filling shapes.

      Returns

      The paint that is to be used for filling shapes.

      Since

      7.0

      See also
      • LinearGradientPaint
    • setAndGetColor

      public int setAndGetColor(int rgb)

      Sets the current rgb color while ignoring any potential alpha component within said color value.

      Parameters
      • rgb: the RGB value for the color.
      Returns

      The previous color value.

      Since

      8.0

    • getFont

      public Font getFont()

      Returns the font used with the drawString method calls

      Returns

      the font used with the drawString method calls

    • setFont

      public void setFont(Font font)

      Sets the font to use with the drawString method calls

      Parameters
      • font: the font used with the drawString method calls
    • getClipX

      public int getClipX()

      Returns the x clipping position

      Returns

      the x clipping position

    • getClip

      public int[] getClip()

      Returns the clip as an x,y,w,h array

      Returns

      clip array copy

    • setClip

      public void setClip(int[] clip)

      Sets the clip from an array containing x, y, width, height value

      Parameters
      • clip: 4 element array
    • setClip

      public void setClip(Shape shape)

      Clips the Graphics context to the Shape.

      This is not supported on all platforms and contexts currently. Use #isShapeClipSupported to check if the current context supports clipping shapes.

      Image duke = null;
      try {
          // duke.png is just the default Codename One icon copied into place
          duke = Image.createImage("/duke.png");
      } catch(IOException err) {
          Log.e(err);
      }
      final Image finalDuke = duke;
      
      Form hi = new Form("Shape Clip");
      
      // We create a 50 x 100 shape, this is arbitrary since we can scale it easily
      GeneralPath path = new GeneralPath();
      path.moveTo(20,0);
      path.lineTo(30, 0);
      path.lineTo(30, 100);
      path.lineTo(20, 100);
      path.lineTo(20, 15);
      path.lineTo(5, 40);
      path.lineTo(5, 25);
      path.lineTo(20,0);
      
      Stroke stroke = new Stroke(0.5f, Stroke.CAP_ROUND, Stroke.JOIN_ROUND, 4);
      hi.getContentPane().getUnselectedStyle().setBgPainter((Graphics g, Rectangle rect) -> {
          g.setColor(0xff);
          float widthRatio = ((float)rect.getWidth()) / 50f;
          float heightRatio = ((float)rect.getHeight()) / 100f;
          g.scale(widthRatio, heightRatio);
          g.translate((int)(((float)rect.getX()) / widthRatio), (int)(((float)rect.getY()) / heightRatio));
          g.setClip(path);
          g.setAntiAliased(true);
          g.drawImage(finalDuke, 0, 0, 50, 100);
          g.setClip(path.getBounds());
          g.drawShape(path, stroke);
          g.translate(-(int)(((float)rect.getX()) / widthRatio), -(int)(((float)rect.getY()) / heightRatio));
          g.resetAffine();
      });
      
      hi.show();
      
      Parameters
      • shape: The shape to clip.
      See also
      • #isShapeClipSupported
    • getClipY

      public int getClipY()

      Returns the y clipping position

      Returns

      the y clipping position

    • getClipWidth

      public int getClipWidth()

      Returns the clip width

      Returns

      the clip width

    • getClipHeight

      public int getClipHeight()

      Returns the clip height

      Returns

      the clip height

    • clipRect

      public void clipRect(int x, int y, int width, int height)

      Clips the given rectangle by intersecting with the current clipping region, this method can thus only shrink the clipping region and never increase it.

      Parameters
      • x: the x coordinate of the rectangle to intersect the clip with

      • y: the y coordinate of the rectangle to intersect the clip with

      • width: the width of the rectangle to intersect the clip with

      • height: the height of the rectangle to intersect the clip with

    • setClip

      public void setClip(int x, int y, int width, int height)

      Updates the clipping region to match the given region exactly

      Parameters
      • x: the x coordinate of the new clip rectangle.

      • y: the y coordinate of the new clip rectangle.

      • width: the width of the new clip rectangle.

      • height: the height of the new clip rectangle.

    • pushClip

      public void pushClip()
      Pushes the current clip onto the clip stack. It can later be restored using #popClip.
    • popClip

      public void popClip()
      Pops the top clip from the clip stack and sets it as the current clip.
    • drawLine

      public void drawLine(int x1, int y1, int x2, int y2)

      Draws a line between the 2 X/Y coordinates

      Parameters
      • x1: first x position

      • y1: first y position

      • x2: second x position

      • y2: second y position

    • fillRect

      public void fillRect(int x, int y, int width, int height)

      Fills the rectangle from the given position according to the width/height minus 1 pixel according to the convention in Java.

      Parameters
      • x: the x coordinate of the rectangle to be filled.

      • y: the y coordinate of the rectangle to be filled.

      • width: the width of the rectangle to be filled.

      • height: the height of the rectangle to be filled.

    • drawShadow

      public void drawShadow(Image img, int x, int y, int offsetX, int offsetY, int blurRadius, int spreadRadius, int color, float opacity)
      Deprecated

      this method should have been internals

    • clearRect

      public void clearRect(int x, int y, int width, int height)

      Clears rectangular area of the graphics context. This will remove any color information that has already been drawn to the graphics context making it transparent.

      The difference between this method and say int, int, int) with alpha=0 is that fillRect() will just blend with the colors underneath (and thus int, int, int) with an alpha of 0 actually does nothing.

      NOTE: In contrast to other drawing methods, coordinates input here are absolute and will not be adjusted by the xTranslate and yTranslate values

      This method is designed to be used by #drawPeerComponent(com.codename1.ui.PeerComponent) only.

      Parameters
      • x: The x-coordinate of the box to clear. In screen coordinates.

      • y: The y-coordinate of the box to clear. In screen coordinates.

      • width: The width of the box to clear.

      • height: The height of the box to clear.

    • drawRect

      public void drawRect(int x, int y, int width, int height)

      Draws a rectangle in the given coordinates

      Parameters
      • x: the x coordinate of the rectangle to be drawn.

      • y: the y coordinate of the rectangle to be drawn.

      • width: the width of the rectangle to be drawn.

      • height: the height of the rectangle to be drawn.

    • drawRect

      public void drawRect(int x, int y, int width, int height, int thickness)

      Draws a rectangle in the given coordinates with the given thickness

      Parameters
      • x: the x coordinate of the rectangle to be drawn.

      • y: the y coordinate of the rectangle to be drawn.

      • width: the width of the rectangle to be drawn.

      • height: the height of the rectangle to be drawn.

      • thickness: the thickness in pixels

    • drawRoundRect

      public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)

      Draws a rounded corner rectangle in the given coordinates with the arcWidth/height matching the last two arguments respectively.

      Parameters
      • x: the x coordinate of the rectangle to be drawn.

      • y: the y coordinate of the rectangle to be drawn.

      • width: the width of the rectangle to be drawn.

      • height: the height of the rectangle to be drawn.

      • arcWidth: the horizontal diameter of the arc at the four corners.

      • arcHeight: the vertical diameter of the arc at the four corners.

    • lighterColor

      public void lighterColor(int factor)

      Makes the current color slightly lighter, this is useful for many visual effects

      Parameters
      • factor: the degree of lightening a color per channel a number from 1 to 255
    • darkerColor

      public void darkerColor(int factor)

      Makes the current color slightly darker, this is useful for many visual effects

      Parameters
      • factor: the degree of lightening a color per channel a number from 1 to 255
    • fillRoundRect

      public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)

      Fills a rounded rectangle in the same way as drawRoundRect

      Parameters
      • x: the x coordinate of the rectangle to be filled.

      • y: the y coordinate of the rectangle to be filled.

      • width: the width of the rectangle to be filled.

      • height: the height of the rectangle to be filled.

      • arcWidth: the horizontal diameter of the arc at the four corners.

      • arcHeight: the vertical diameter of the arc at the four corners.

      See also
      • #drawRoundRect
    • fillArc

      public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)

      Fills a circular or elliptical arc based on the given angles and bounding box. The resulting arc begins at startAngle and extends for arcAngle degrees. Usage:

      Painter p = new Painter(cmp) {
          public void paint(Graphics g, Rectangle rect) {
              boolean antiAliased = g.isAntiAliased();
              g.setAntiAliased(true);
              int r = Math.min(rect.getWidth(), rect.getHeight())/2;
              int x = rect.getX() + rect.getWidth()/2 - r;
              int y = rect.getY() + rect.getHeight()/2 - r;
              switch (style) {
                  case CircleButtonStrokedDark:
                  case CircleButtonStrokedLight: {
                      if (cmp.getStyle().getBgTransparency() != 0) {
                          int alpha = cmp.getStyle().getBgTransparency();
                          if (alpha <0) {
                              alpha = 0xff;
                          }
                          g.setColor(cmp.getStyle().getBgColor());
                          g.setAlpha(alpha);
                          g.fillArc(x, y, 2*r-1, 2*r-1, 0, 360);
                          g.setAlpha(0xff);
                      }
                      g.setColor(cmp.getStyle().getFgColor());
                      g.drawArc(x, y, 2*r-1, 2*r-1, 0, 360);
                      break;
                  }
                  case CircleButtonFilledDark:
                  case CircleButtonFilledLight:
                  case CircleButtonTransparentDark:
                  case CircleButtonTransparentLight: {
                      int alpha = cmp.getStyle().getBgTransparency();
                      if (alpha < 0) {
                          alpha = 0xff;
                      }
                      g.setAlpha(alpha);
                      g.setColor(cmp.getStyle().getBgColor());
                      g.fillArc(x, y, 2*r, 2*r, 0, 360);
                      g.setAlpha(0xff);
                      break;
                  }
              }
      
              g.setAntiAliased(antiAliased);
          }
      };
      cmp.getAllStyles().setBgPainter(p);
      
      Parameters
      • x: the x coordinate of the upper-left corner of the arc to be filled.

      • y: the y coordinate of the upper-left corner of the arc to be filled.

      • width: the width of the arc to be filled, must be 1 or more.

      • height: the height of the arc to be filled, must be 1 or more.

      • startAngle: the beginning angle.

      • arcAngle: the angular extent of the arc, relative to the start angle.

    • drawArc

      public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)

      Draws a circular or elliptical arc based on the given angles and bounding box

      Parameters
      • x: the x coordinate of the upper-left corner of the arc to be drawn.

      • y: the y coordinate of the upper-left corner of the arc to be drawn.

      • width: the width of the arc to be drawn.

      • height: the height of the arc to be drawn.

      • startAngle: the beginning angle.

      • arcAngle: the angular extent of the arc, relative to the start angle.

    • drawString

      public void drawString(String str, int x, int y, int textDecoration)

      Draw a string using the current font and color in the x,y coordinates. The font is drawn from the top position and not the baseline.

      Parameters
      • str: the string to be drawn.

      • x: the x coordinate.

      • y: the y coordinate.

      • textDecoration: Text decoration bitmask (See Style's TEXT_DECORATION_* constants)

    • drawStringBaseline

      public void drawStringBaseline(String str, int x, int y)

      Draws a string using baseline coordinates.

      Parameters
      • str: The string to be drawn.

      • x: The x-coordinate of the start of left edge of the text block.

      • y: The y-coordinate of the baseline of the text.

      See also
      • #drawString(java.lang.String, int, int)
    • drawStringBaseline

      public void drawStringBaseline(String str, int x, int y, int textDecoration)

      Draws a string using baseline coordinates.

      Parameters
      • str: The string to be drawn.

      • x: The x-coordinate of the start of left edge of the text block.

      • y: The y-coordinate of the baseline of the text.

      • textDecoration: Text decoration bitmask (See Style's TEXT_DECORATION_* constants)

      See also
      • #drawString(java.lang.String, int, int, int)
    • drawString

      public void drawString(String str, int x, int y)

      Draw a string using the current font and color in the x,y coordinates. The font is drawn from the top position and not the baseline.

      Parameters
      • str: the string to be drawn.

      • x: the x coordinate.

      • y: the y coordinate.

    • drawChar

      public void drawChar(char character, int x, int y)

      Draw the given char using the current font and color in the x,y coordinates. The font is drawn from the top position and not the baseline.

      Parameters
      • character: - the character to be drawn

      • x: the x coordinate of the baseline of the text

      • y: the y coordinate of the baseline of the text

      Deprecated

      use drawString instead, this method is inefficient

    • drawChars

      public void drawChars(char[] data, int offset, int length, int x, int y)

      Draw the given char array using the current font and color in the x,y coordinates. The font is drawn from the top position and not the baseline.

      Parameters
      • data: the array of characters to be drawn

      • offset: the start offset in the data

      • length: the number of characters to be drawn

      • x: the x coordinate of the baseline of the text

      • y: the y coordinate of the baseline of the text

      Deprecated

      use drawString instead, this method is inefficient

    • drawImage

      public void drawImage(Image img, int x, int y)

      Draws the image so its top left coordinate corresponds to x/y

      Parameters
      • img: @param img the specified image to be drawn. This method does nothing if img is null.

      • x: the x coordinate.

      • y: the y coordinate.

    • drawImage

      public void drawImage(Image img, int x, int y, int w, int h)

      Draws the image so its top left coordinate corresponds to x/y and scales it to width/height

      Parameters
      • img: @param img the specified image to be drawn. This method does nothing if img is null.

      • x: the x coordinate.

      • y: the y coordinate.

      • w: the width to occupy

      • h: the height to occupy

    • drawShape

      public void drawShape(Shape shape, Stroke stroke)

      Draws a outline shape inside the specified bounding box. The bounding box will resize the shape to fit in its dimensions.

      This is not supported on all platforms and contexts currently. Use #isShapeSupported to check if the current context supports drawing shapes.

      Form hi = new Form("Shape");
      
      // We create a 50 x 100 shape, this is arbitrary since we can scale it easily
      GeneralPath path = new GeneralPath();
      path.moveTo(20,0);
      path.lineTo(30, 0);
      path.lineTo(30, 100);
      path.lineTo(20, 100);
      path.lineTo(20, 15);
      path.lineTo(5, 40);
      path.lineTo(5, 25);
      path.lineTo(20,0);
      
      hi.getContentPane().getUnselectedStyle().setBgPainter((Graphics g, Rectangle rect) -> {
          g.setColor(0xff);
          float widthRatio = ((float)rect.getWidth()) / 50f;
          float heightRatio = ((float)rect.getHeight()) / 100f;
          g.scale(widthRatio, heightRatio);
          g.translate((int)(((float)rect.getX()) / widthRatio), (int)(((float)rect.getY()) / heightRatio));
          g.fillShape(path);
          g.resetAffine();
      });
      
      hi.show();
      
      Parameters
      • shape: The shape to be drawn.

      • stroke: the stroke to use

      See also
      • #setStroke

      • #isShapeSupported

    • fillShape

      public void fillShape(Shape shape)

      Fills the given shape using the current alpha and color settings.

      This is not supported on all platforms and contexts currently. Use #isShapeSupported to check if the current context supports drawing shapes.

      Form hi = new Form("Shape");
      
      // We create a 50 x 100 shape, this is arbitrary since we can scale it easily
      GeneralPath path = new GeneralPath();
      path.moveTo(20,0);
      path.lineTo(30, 0);
      path.lineTo(30, 100);
      path.lineTo(20, 100);
      path.lineTo(20, 15);
      path.lineTo(5, 40);
      path.lineTo(5, 25);
      path.lineTo(20,0);
      
      hi.getContentPane().getUnselectedStyle().setBgPainter((Graphics g, Rectangle rect) -> {
          g.setColor(0xff);
          float widthRatio = ((float)rect.getWidth()) / 50f;
          float heightRatio = ((float)rect.getHeight()) / 100f;
          g.scale(widthRatio, heightRatio);
          g.translate((int)(((float)rect.getX()) / widthRatio), (int)(((float)rect.getY()) / heightRatio));
          g.fillShape(path);
          g.resetAffine();
      });
      
      hi.show();
      

      Note: You can specify a custom Paint to use for filling the shape using the #setColor(com.codename1.ui.Paint) method. This is useful for filling the shape with a LinearGradientPaint, for example.

      Parameters
      • shape: The shape to be filled.
      See also
      • #isShapeSupported
    • isTransformSupported

      public boolean isTransformSupported()

      Checks to see if com.codename1.ui.geom.Matrix transforms are supported by this graphics context.

      Returns
      Returns:

      true if this graphics context supports com.codename1.ui.geom.Matrix transforms.

      Note that this method only confirms that 2D transforms are supported. If you need to perform 3D transformations, you should use the #isPerspectiveTransformSupported method.

      See also
      • #setTransform

      • #getTransform

      • #isPerspectiveTransformSupported

    • isPerspectiveTransformSupported

      public boolean isPerspectiveTransformSupported()

      Checks to see if perspective (3D) com.codename1.ui.geom.Matrix transforms are supported by this graphics context. If 3D transforms are supported, you can use a 4x4 transformation com.codename1.ui.geom.Matrix via #setTransform to perform 3D transforms.

      Note: It is possible for 3D transforms to not be supported but Affine (2D) transforms to be supported. In this case you would be limited to a 3x3 transformation matrix in #setTransform. You can check for 2D transformation support using the #isTransformSupported method.

      Returns

      true if Perspective (3D) transforms are supported. false otherwise.

      See also
      • #isTransformSupported

      • #setTransform

      • #getTransform

    • isShapeSupported

      public boolean isShapeSupported()

      Checks to see if this graphics context supports drawing shapes (i.e. #drawShape and #fillShape methods. If this returns false, and you call #drawShape or #fillShape, then nothing will be drawn.

      Returns

      true If #drawShape and #fillShape are supported.

      See also
      • #drawShape

      • #fillShape

    • isShapeClipSupported

      public boolean isShapeClipSupported()

      Checks to see if this graphics context supports clip Shape. If this returns false, calling setClip(Shape) will have no effect on the Graphics clipping area

      Returns

      true If setClip(Shape) is supported.

    • transform

      public void transform(Transform transform)

      Concatenates the given transform to the context's transform.

      Parameters
      • transform: The transform to concatenate.
      Since

      7.0

    • getTransform

      public Transform getTransform()

      Gets the transformation matrix that is currently applied to this graphics context.

      Returns

      The current transformation matrix.

      Deprecated

      Use #getTransform(com.codename1.ui.Transform) instead.

      See also
      • #setTransform
    • setTransform

      public void setTransform(Transform transform)

      Sets the transformation com.codename1.ui.geom.Matrix to apply to drawing in this graphics context. In order to use this for 2D/Affine transformations you should first check to make sure that transforms are supported by calling the #isTransformSupported method. For 3D/Perspective transformations, you should first check to make sure that 3D/Perspective transformations are supported by calling the #isPerspectiveTransformSupported.

      Transformations are applied with (0,0) as the origin. So rotations and scales are anchored at this point on the screen. You can use a different anchor point by either embedding it in the transformation matrix (i.e. pre-transform the com.codename1.ui.geom.Matrix to anchor at a different point) or use the int, int) variation that allows you to explicitly set the anchor point.

      Parameters
      • transform: @param transform The transformation com.codename1.ui.geom.Matrix to use for drawing. 2D/Affine transformations can be achieved using a 3x3 transformation com.codename1.ui.geom.Matrix. 3D/Perspective transformations can be achieved using a 4x3 transformation com.codename1.ui.geom.Matrix.
      See also
      • #isTransformSupported

      • #isPerspectiveTransformSupported

      • #setTransform(com.codename1.ui.geom.Matrix, int, int)

    • getTransform

      public void getTransform(Transform t)

      Loads the provided transform with the current transform applied to this graphics context.

      Parameters
      • t: An "out" parameter to be filled with the current transform.
    • fillTriangle

      public void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3)

      Draws a filled triangle with the given coordinates

      Parameters
      • x1: the x coordinate of the first vertex of the triangle

      • y1: the y coordinate of the first vertex of the triangle

      • x2: the x coordinate of the second vertex of the triangle

      • y2: the y coordinate of the second vertex of the triangle

      • x3: the x coordinate of the third vertex of the triangle

      • y3: the y coordinate of the third vertex of the triangle

    • fillRadialGradient

      public void fillRadialGradient(int startColor, int endColor, int x, int y, int width, int height)

      Draws a radial gradient in the given coordinates with the given colors, doesn't take alpha into consideration when drawing the gradient. Notice that a radial gradient will result in a circular shape, to create a square use fillRect or draw a larger shape and clip to the appropriate size.

      Parameters
      • startColor: the starting RGB color

      • endColor: the ending RGB color

      • x: the x coordinate

      • y: the y coordinate

      • width: the width of the region to be filled

      • height: the height of the region to be filled

    • fillRadialGradient

      public void fillRadialGradient(int startColor, int endColor, int x, int y, int width, int height, int startAngle, int arcAngle)

      Draws a radial gradient in the given coordinates with the given colors, doesn't take alpha into consideration when drawing the gradient. Notice that a radial gradient will result in a circular shape, to create a square use fillRect or draw a larger shape and clip to the appropriate size.

      Parameters
      • startColor: the starting RGB color

      • endColor: the ending RGB color

      • x: the x coordinate

      • y: the y coordinate

      • width: the width of the region to be filled

      • height: the height of the region to be filled

      • startAngle: the beginning angle. Zero is at 3 o'clock. Positive angles are counter-clockwise.

      • arcAngle: the angular extent of the arc, relative to the start angle. Positive angles are counter-clockwise.

    • fillRectRadialGradient

      public void fillRectRadialGradient(int startColor, int endColor, int x, int y, int width, int height, float relativeX, float relativeY, float relativeSize)

      Draws a radial gradient in the given coordinates with the given colors, doesn't take alpha into consideration when drawing the gradient. Notice that this method differs from fillRadialGradient since it draws a square gradient at all times and can thus be cached Notice that a radial gradient will result in a circular shape, to create a square use fillRect or draw a larger shape and clip to the appropriate size.

      Parameters
      • startColor: the starting RGB color

      • endColor: the ending RGB color

      • x: the x coordinate

      • y: the y coordinate

      • width: the width of the region to be filled

      • height: the height of the region to be filled

      • relativeX: indicates the relative position of the gradient within the drawing region

      • relativeY: indicates the relative position of the gradient within the drawing region

      • relativeSize: indicates the relative size of the gradient within the drawing region

    • fillLinearGradient

      public void fillLinearGradient(int startColor, int endColor, int x, int y, int width, int height, boolean horizontal)

      Draws a linear gradient in the given coordinates with the given colors, doesn't take alpha into consideration when drawing the gradient

      Parameters
      • startColor: the starting RGB color

      • endColor: the ending RGB color

      • x: the x coordinate

      • y: the y coordinate

      • width: the width of the region to be filled

      • height: the height of the region to be filled

      • horizontal: indicating wheter it is a horizontal fill or vertical

    • fillRect

      public void fillRect(int x, int y, int w, int h, byte alpha)

      Fills a rectangle with an optionally translucent fill color

      Parameters
      • x: the x coordinate of the rectangle to be filled

      • y: the y coordinate of the rectangle to be filled

      • w: the width of the rectangle to be filled

      • h: the height of the rectangle to be filled

      • alpha: the alpha values specify semitransparency

    • fillPolygon

      public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)

      Fills a closed polygon defined by arrays of x and y coordinates. Each pair of (x, y) coordinates defines a point.

      Parameters
      • xPoints: - a an array of x coordinates.

      • yPoints: - a an array of y coordinates.

      • nPoints: - a the total number of points.

    • drawPolygon

      public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)

      Draws a closed polygon defined by arrays of x and y coordinates. Each pair of (x, y) coordinates defines a point.

      Parameters
      • xPoints: - an array of x coordinates.

      • yPoints: - an array of y coordinates.

      • nPoints: - the total number of points.

    • isAlphaSupported

      public boolean isAlphaSupported()

      Indicates whether invoking set/getAlpha would have an effect on all further rendering from this graphics object.

      Returns

      false if setAlpha has no effect true if it applies to everything some effect

    • setAndGetAlpha

      public int setAndGetAlpha(int a)

      Sets alpha as a value between 0-255 (0 - 0xff) where 255 is completely opaque and 0 is completely transparent

      Parameters
      • a: the alpha value
      Returns

      The previous alpha value.

    • concatenateAlpha

      public int concatenateAlpha(int a)

      Concatenates the given alpha value to the current alpha setting, and returns the previous alpha setting.

      Parameters
      • a: Alpha value to concatenate (0-255).
      Returns

      The previous alpha setting (0-255).

      Since

      7.0

    • getAlpha

      public int getAlpha()

      Returns the alpha as a value between 0-255 (0 - 0xff) where 255 is completely opaque and 0 is completely transparent

      Returns

      the alpha value

    • setAlpha

      public void setAlpha(int a)

      Sets alpha as a value between 0-255 (0 - 0xff) where 255 is completely opaque and 0 is completely transparent

      Parameters
      • a: the alpha value
    • isAntiAliasingSupported

      public boolean isAntiAliasingSupported()

      Returns true if antialiasing for standard rendering operations is supported, notice that text antialiasing is a separate attribute.

      Returns

      true if antialiasing is supported

    • isAntiAliasedTextSupported

      public boolean isAntiAliasedTextSupported()

      Returns true if antialiasing for text is supported, notice that text antialiasing is a separate attribute from standard anti-alisaing.

      Returns

      true if text antialiasing is supported

    • isAntiAliased

      public boolean isAntiAliased()

      Returns true if antialiasing for standard rendering operations is turned on.

      Returns

      true if antialiasing is active

    • setAntiAliased

      public void setAntiAliased(boolean a)

      Set whether antialiasing for standard rendering operations is turned on.

      Parameters
      • a: true if antialiasing is active
    • isAntiAliasedText

      public boolean isAntiAliasedText()

      Indicates whether antialiasing for text is active, notice that text antialiasing is a separate attribute from standard anti-alisaing.

      Returns

      true if text antialiasing is supported

    • setAntiAliasedText

      public void setAntiAliasedText(boolean a)

      Set whether antialiasing for text is active, notice that text antialiasing is a separate attribute from standard anti-alisaing.

      Parameters
      • a: true if text antialiasing is supported
    • isAffineSupported

      public boolean isAffineSupported()

      Indicates whether the underlying implementation can draw using an affine transform hence methods such as rotate, scale and shear would work

      Returns

      true if an affine transformation matrix is present

    • resetAffine

      public void resetAffine()
      Resets the affine transform to the default value
    • scale

      public void scale(float x, float y)

      Scales the coordinate system using the affine transform

      Parameters
      • x: scale factor for x

      • y: scale factor for y

    • rotate

      public void rotate(float angle)

      Rotates the coordinate system around a radian angle using the affine transform

      Parameters
      • angle: the rotation angle in radians about the screen origin.
      Deprecated
    • rotateRadians

      public void rotateRadians(float angle)

      RRotates the coordinate system around a radian angle using the affine transform

      Parameters
      • angle: the rotation angle in radians about graphics context's translated origin.
      Since

      6.0

    • rotate

      public void rotate(float angle, int pivotX, int pivotY)

      Rotates the coordinate system around a radian angle using the affine transform

      Parameters
      • angle: the rotation angle in radians

      • pivotX: the pivot point In absolute coordinates.

      • pivotY: the pivot point In absolute coordinates.

      Deprecated
    • rotateRadians

      public void rotateRadians(float angle, int pivotX, int pivotY)

      Rotates the coordinate system around a radian angle using the affine transform

      Parameters
      • angle: the rotation angle in radians

      • pivotX: the pivot point relative to the current graphics context's translation.

      • pivotY: the pivot point relative to the current graphics context's translation.

      Since

      6.0

    • shear

      public void shear(float x, float y)

      Shear the graphics coordinate system using the affine transform

      Parameters
      • x: shear factor for x

      • y: shear factor for y

    • beginNativeGraphicsAccess

      public Object beginNativeGraphicsAccess()

      Starts accessing the native graphics in the underlying OS, when accessing the native graphics Codename One shouldn't be used! The native graphics is unclipped and untranslated by default and its the responsibility of the caller to clip/translate appropriately.

      When finished with the native graphics it is essential to invoke endNativeGraphicsAccess

      Returns

      an instance of the underlying native graphics object

    • endNativeGraphicsAccess

      public void endNativeGraphicsAccess()
      Invoke this to restore Codename One's graphics settings into the native graphics
    • tileImage

      public void tileImage(Image img, int x, int y, int w, int h)

      Allows an implementation to optimize image tiling rendering logic

      Parameters
      • img: the image

      • x: coordinate to tile the image along

      • y: coordinate to tile the image along

      • w: coordinate to tile the image along

      • h: coordinate to tile the image along

    • getScaleX

      public float getScaleX()

      Returns the affine X scale

      Returns

      the current scale

    • getScaleY

      public float getScaleY()

      Returns the affine Y scale

      Returns

      the current scale

    • getRenderingHints

      public int getRenderingHints()

      Gets the current rendering hints for this context.

      Returns

      The rendering hints.

      See also
      • #RENDERING_HINT_FAST
    • setRenderingHints

      public void setRenderingHints(int hints)

      Sets rendering hints for this context.

      Parameters
      • hints: int of rendering hints produced by logical AND on all applicable hints.
      Since

      7.0

      See also
      • #RENDERING_HINT_FAST

      • #getRenderingHints()