public final class Graphics
- Object
- Graphics
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.
Fields
public static final int RENDERING_HINT_FAST = 1 | 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. |
Methods
protected void finalize() | |
public void translate(int x, int y) | Translates the X/Y location for drawing on the underlying surface. |
public int getTranslateX() | Returns the current x translate value |
public int getTranslateY() | Returns the current y translate value |
public int getColor() | Returns the current color |
public void setColor(int rgb) | Sets the current rgb color while ignoring any potential alpha component within said color value. |
public void setColor(Paint paint) | Sets paint to be used for filling shapes. |
public Paint getPaint() | Gets the current Paint that is set to be used for filling shapes. |
public int setAndGetColor(int rgb) | Sets the current rgb color while ignoring any potential alpha component within said color value. |
public Font getFont() | Returns the font used with the drawString method calls |
public void setFont(Font font) | Sets the font to use with the drawString method calls |
public int getClipX() | Returns the x clipping position |
public int[] getClip() | Returns the clip as an x,y,w,h array |
public void setClip(int[] clip) | Sets the clip from an array containing x, y, width, height value |
public void setClip(Shape shape) | Clips the Graphics context to the Shape. |
public int getClipY() | Returns the y clipping position |
public int getClipWidth() | Returns the clip width |
public int getClipHeight() | Returns the clip height |
public boolean isVisible(int x, int y, int w, int h) | Returns true if the given rectangle (in the current Graphics coordinate space, with the active translation applied) intersects the current clip rectangle, i.e. anything drawn into it would be at least partially visible. |
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. |
public void setClip(int x, int y, int width, int height) | Updates the clipping region to match the given region exactly |
public void pushClip() | Pushes the current clip onto the clip stack. |
public void popClip() | Pops the top clip from the clip stack and sets it as the current clip. |
public void drawLine(int x1, int y1, int x2, int y2) | Draws a line between the 2 X/Y coordinates |
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. |
public void drawShadow(Image img, int x, int y, int offsetX, int offsetY, int blurRadius, int spreadRadius, int color, float opacity) | Deprecated |
public void clearRect(int x, int y, int width, int height) | Clears rectangular area of the graphics context. |
public void drawRect(int x, int y, int width, int height) | Draws a rectangle in the given coordinates |
public void drawRect(int x, int y, int width, int height, int thickness) | Draws a rectangle in the given coordinates with the given thickness |
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. |
public void lighterColor(int factor) | Makes the current color slightly lighter, this is useful for many visual effects |
public void darkerColor(int factor) | Makes the current color slightly darker, this is useful for many visual effects |
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 |
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. |
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 |
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. |
public void drawStringBaseline(String str, int x, int y) | Draws a string using baseline coordinates. |
public void drawStringBaseline(String str, int x, int y, int textDecoration) | Draws a string using baseline coordinates. |
public void drawString(String str, int x, int y) | Draw a string using the current font and color in the x,y coordinates. |
public void drawChar(char character, int x, int y) | Deprecated Draw the given char using the current font and color in the x,y coordinates. |
public void drawChars(char[] data, int offset, int length, int x, int y) | Deprecated Draw the given char array using the current font and color in the x,y coordinates. |
public void drawImage(Image img, int x, int y) | Draws the image so its top left coordinate corresponds to x/y |
public boolean isRoundedImageSupported() | Draws the image so its top left coordinate corresponds to x/y and scales it to width/height |
public boolean isRoundedImageSupported(Image img) | Whether drawImageRounded(Image, int, int, int, int, float) will round THIS image, which is the question a caller actually needs answered. |
public void drawImageRounded(Image img, int x, int y, int w, int h, float cornerRadius) | Draws an image with rounded corners, without building a rounded copy of it, on platforms that support it; elsewhere the image is drawn square. |
public void drawImage(Image img, int x, int y, int w, int h) | |
public void drawShape(Shape shape, Stroke stroke) | Draws a outline shape inside the specified bounding box. |
public void fillShape(Shape shape) | Fills the given shape using the current alpha and color settings. |
public void fillShapeShadow(Shape shape, int fillColor, int fillAlpha, int shadowColor, float shadowOpacity, int blurRadius, int offsetX, int offsetY) | Fills the given shape and casts a blurred drop shadow behind it in a single GPU-accelerated draw where the platform supports it (e.g. Android Paint.setShadowLayer, iOS CGContextSetShadow), with no retained bitmap. |
public boolean isShapeShadowSupported() | Checks whether #fillShapeShadow renders a GPU shadow on this platform. |
public boolean isTransformSupported() | Checks to see if com.codename1.ui.geom.Matrix transforms are supported by this graphics context. |
public boolean isPerspectiveTransformSupported() | Checks to see if perspective (3D) com.codename1.ui.geom.Matrix transforms are supported by this graphics context. |
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. |
public boolean isShapeClipSupported() | Checks to see if this graphics context supports clip Shape. |
public void transform(Transform transform) | Concatenates the given transform to the context’s transform. |
public Transform getTransform() | Deprecated Gets the transformation matrix that is currently applied to this graphics context. |
public void setTransform(Transform transform) | Sets the transformation com.codename1.ui.geom.Matrix to apply to drawing in this graphics context. |
public void getTransform(Transform t) | Loads the provided transform with the current transform applied to this graphics context. |
public void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3) | Draws a filled triangle with the given coordinates |
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. |
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. |
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. |
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 |
public void fillGradient(Gradient gradient, int x, int y, int width, int height) | Fills the rectangle (x, y, width, height) with the given multi-stop gradient. |
public Image gaussianBlur(Image source, float radius) | Returns a copy of the given image with a Gaussian blur of the given radius applied. |
public boolean blurRegion(int x, int y, int width, int height, float radius) | Applies a Gaussian blur to the contents already painted into the rectangular region. |
public boolean glassRegion(int x, int y, int width, int height, float radius, float cornerRadius, float sat, float scale, float offset, float refract, float specular) | Applies the iOS “Liquid Glass” material to the contents already painted into the rectangular region. |
public boolean lensRegion(int x, int y, int width, int height, float cornerRadius, float magnify, float aberration, int tintColor, float tintStrength) | Applies the iOS 26 selection “drop” LENS to the contents already painted into the region (the bar + glyphs UNDER it): radial magnification, edge chromatic aberration, and a luminance-keyed dark->accent tint so dark glyphs read in the accent colour only where the lens covers them. |
public void fillRect(int x, int y, int w, int h, byte alpha) | Fills a rectangle with an optionally translucent fill color |
public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) | Fills a closed polygon defined by arrays of x and y coordinates. |
public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) | Draws a closed polygon defined by arrays of x and y coordinates. |
public boolean isAlphaSupported() | Indicates whether invoking set/getAlpha would have an effect on all further rendering from this graphics object. |
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 |
public int concatenateAlpha(int a) | Concatenates the given alpha value to the current alpha setting, and returns the previous alpha setting. |
public int getAlpha() | Returns the alpha as a value between 0-255 (0 - 0xff) where 255 is completely opaque and 0 is completely transparent |
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 |
public boolean isAntiAliasingSupported() | Returns true if antialiasing for standard rendering operations is supported, notice that text antialiasing is a separate attribute. |
public boolean isAntiAliasedTextSupported() | Returns true if antialiasing for text is supported, notice that text antialiasing is a separate attribute from standard anti-alisaing. |
public boolean isAntiAliased() | Returns true if antialiasing for standard rendering operations is turned on. |
public void setAntiAliased(boolean a) | Set whether antialiasing for standard rendering operations is turned on. |
public boolean isAntiAliasedText() | Indicates whether antialiasing for text is active, notice that text antialiasing is a separate attribute from standard anti-alisaing. |
public void setAntiAliasedText(boolean a) | Set whether antialiasing for text is active, notice that text antialiasing is a separate attribute from standard anti-alisaing. |
public boolean isAffineSupported() | Indicates whether the underlying implementation can draw using an affine transform hence methods such as rotate, scale and shear would work |
public void resetAffine() | Resets the affine transform to the default value |
public void scale(float x, float y) | Scales the coordinate system using the affine transform |
public void translateMatrix(float x, float y) | Translates the coordinate system using the affine transform matrix (as opposed to #translate(int, int) which uses a per-Graphics integer accumulator). |
public boolean isTranslateMatrixSupported() | Checks whether #translateMatrix(float, float) composes through the impl matrix on this port (the matrix-correct mode) versus falling back to the integer #translate(int, int) accumulator. |
public void rotate(float angle) | Deprecated Rotates the coordinate system around a radian angle using the affine transform |
public void rotateRadians(float angle) | RRotates the coordinate system around a radian angle using the affine transform |
public void rotate(float angle, int pivotX, int pivotY) | Deprecated Rotates the coordinate system around a radian angle using the affine transform |
public void rotateRadians(float angle, int pivotX, int pivotY) | Rotates the coordinate system around a radian angle using the affine transform |
public void shear(float x, float y) | Shear the graphics coordinate system using the affine transform |
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. |
public void endNativeGraphicsAccess() | Invoke this to restore Codename One’s graphics settings into the native graphics |
public void tileImage(Image img, int x, int y, int w, int h) | Allows an implementation to optimize image tiling rendering logic |
public float getScaleX() | Returns the affine X scale |
public float getScaleY() | Returns the affine Y scale |
public int getRenderingHints() | Gets the current rendering hints for this context. |
public void setRenderingHints(int hints) | Sets rendering hints for this context. |
Inherited methods
Field details
RENDERING_HINT_FAST
public static final int RENDERING_HINT_FAST = 1Method details
finalize
protected void finalize()translate
public void translate(int x, int y)translate(-getTranslateX(), -getTranslateY())Parameters
xint- the x coordinate
yint- the y coordinate
getTranslateX
public int getTranslateX()Returns
getTranslateY
public int getTranslateY()Returns
getColor
public int getColor()Returns
setColor
public void setColor(int rgb)Parameters
rgbint- the RGB value for the color.
setColor
public void setColor(Paint paint)#fillShape(com.codename1.ui.geom.Shape) method.See also
getPaint
public Paint getPaint()Paint that is set to be used for filling shapes.Returns
See also
setAndGetColor
public int setAndGetColor(int rgb)Parameters
rgbint- the RGB value for the color.
Returns
getFont
public Font getFont()Returns
setFont
public void setFont(Font font)Parameters
fontFont- the font used with the drawString method calls
getClipX
public int getClipX()Returns
getClip
public int[] getClip()Returns
setClip
public void setClip(int[] clip)Parameters
clipint[]- 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(new Painter() {
public void paint(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
shapeShape- The shape to clip.
See also
getClipY
public int getClipY()Returns
getClipWidth
public int getClipWidth()Returns
getClipHeight
public int getClipHeight()Returns
isVisible
public boolean isVisible(int x, int y, int w, int h)Returns true if the given rectangle (in the current Graphics coordinate space, with the active translation applied) intersects the current clip rectangle, i.e. anything drawn into it would be at least partially visible.
Use this to skip work for off-screen draws - typical case is a zoomed canvas where most images fall outside the visible window and should not be decoded or scaled.
if (g.isVisible(x, y, w, h)) {
g.drawImage(image, x, y, w, h);
}
When a non-identity affine transform is in effect (see setTransform),
the four corners of the rectangle are mapped through the current
transform and the axis-aligned bounding box of the result is tested
against the clip. This means a rotated/scaled rectangle is judged by
where it actually lands on screen rather than by its untransformed
coordinates. The integer translate (translate) is intentionally not
added here: getClipX/getClipY are already reported in the same
untranslated coordinate space you pass to draw calls, so the translation
cancels out.
Note: shape-clipped graphics (setClip(Shape)) fall back to the
bounding rectangle of the clip, and perspective (3D) transforms are
approximated by their 2D corner projection; this matches the precision
actually used by the platform draw calls.
Parameters
xint- left edge of the rectangle
yint- top edge of the rectangle
wint- width of the rectangle
hint- height of the rectangle
Returns
clipRect
public void clipRect(int x, int y, int width, int height)Parameters
xint- the x coordinate of the rectangle to intersect the clip with
yint- the y coordinate of the rectangle to intersect the clip with
widthint- the width of the rectangle to intersect the clip with
heightint- the height of the rectangle to intersect the clip with
setClip
public void setClip(int x, int y, int width, int height)Parameters
xint- the x coordinate of the new clip rectangle.
yint- the y coordinate of the new clip rectangle.
widthint- the width of the new clip rectangle.
heightint- the height of the new clip rectangle.
pushClip
public void pushClip()#popClip.popClip
public void popClip()drawLine
public void drawLine(int x1, int y1, int x2, int y2)Parameters
x1int- first x position
y1int- first y position
x2int- second x position
y2int- second y position
fillRect
public void fillRect(int x, int y, int width, int height)Parameters
xint- the x coordinate of the rectangle to be filled.
yint- the y coordinate of the rectangle to be filled.
widthint- the width of the rectangle to be filled.
heightint- 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)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
xint- The x-coordinate of the box to clear. In screen coordinates.
yint- The y-coordinate of the box to clear. In screen coordinates.
widthint- The width of the box to clear.
heightint- The height of the box to clear.
drawRect
public void drawRect(int x, int y, int width, int height)Parameters
xint- the x coordinate of the rectangle to be drawn.
yint- the y coordinate of the rectangle to be drawn.
widthint- the width of the rectangle to be drawn.
heightint- the height of the rectangle to be drawn.
drawRect
public void drawRect(int x, int y, int width, int height, int thickness)Parameters
xint- the x coordinate of the rectangle to be drawn.
yint- the y coordinate of the rectangle to be drawn.
widthint- the width of the rectangle to be drawn.
heightint- the height of the rectangle to be drawn.
thicknessint- the thickness in pixels
drawRoundRect
public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)Parameters
xint- the x coordinate of the rectangle to be drawn.
yint- the y coordinate of the rectangle to be drawn.
widthint- the width of the rectangle to be drawn.
heightint- the height of the rectangle to be drawn.
arcWidthint- the horizontal diameter of the arc at the four corners.
arcHeightint- the vertical diameter of the arc at the four corners.
lighterColor
public void lighterColor(int factor)Parameters
factorint- the degree of lightening a color per channel a number from 1 to 255
darkerColor
public void darkerColor(int factor)Parameters
factorint- 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)Parameters
xint- the x coordinate of the rectangle to be filled.
yint- the y coordinate of the rectangle to be filled.
widthint- the width of the rectangle to be filled.
heightint- the height of the rectangle to be filled.
arcWidthint- the horizontal diameter of the arc at the four corners.
arcHeightint- the vertical diameter of the arc at the four corners.
See also
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:
Form hi = new Form("fillArc / drawArc", new BorderLayout());
Container cmp = new Container();
cmp.setPreferredSize(new Dimension(300, 300));
Painter p = new Painter() {
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;
g.setColor(0x4488ff);
g.fillArc(x, y, 2 * r, 2 * r, 0, 360);
g.setColor(0xffffff);
g.drawArc(x, y, 2 * r - 1, 2 * r - 1, 0, 360);
g.setAntiAliased(antiAliased);
}
};
cmp.getAllStyles().setBgPainter(p);
hi.add(BorderLayout.CENTER, cmp);
hi.show();
Parameters
xint- the x coordinate of the upper-left corner of the arc to be filled.
yint- the y coordinate of the upper-left corner of the arc to be filled.
widthint- the width of the arc to be filled, must be 1 or more.
heightint- the height of the arc to be filled, must be 1 or more.
startAngleint- the beginning angle.
arcAngleint- 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)Parameters
xint- the x coordinate of the upper-left corner of the arc to be drawn.
yint- the y coordinate of the upper-left corner of the arc to be drawn.
widthint- the width of the arc to be drawn.
heightint- the height of the arc to be drawn.
startAngleint- the beginning angle.
arcAngleint- the angular extent of the arc, relative to the start angle.
drawString
public void drawString(String str, int x, int y, int textDecoration)Parameters
strString- the string to be drawn.
xint- the x coordinate.
yint- the y coordinate.
textDecorationint- Text decoration bitmask (See Style’s TEXT_DECORATION_* constants)
drawStringBaseline
public void drawStringBaseline(String str, int x, int y)Parameters
strString- The string to be drawn.
xint- The x-coordinate of the start of left edge of the text block.
yint- The y-coordinate of the baseline of the text.
drawStringBaseline
public void drawStringBaseline(String str, int x, int y, int textDecoration)Parameters
strString- The string to be drawn.
xint- The x-coordinate of the start of left edge of the text block.
yint- The y-coordinate of the baseline of the text.
textDecorationint- Text decoration bitmask (See Style’s TEXT_DECORATION_* constants)
drawString
public void drawString(String str, int x, int y)Parameters
strString- the string to be drawn.
xint- the x coordinate.
yint- the y coordinate.
drawChar
public void drawChar(char character, int x, int y)Parameters
characterchar- the character to be drawn
xint- the x coordinate of the baseline of the text
yint- the y coordinate of the baseline of the text
drawChars
public void drawChars(char[] data, int offset, int length, int x, int y)Parameters
datachar[]- the array of characters to be drawn
offsetint- the start offset in the data
lengthint- the number of characters to be drawn
xint- the x coordinate of the baseline of the text
yint- the y coordinate of the baseline of the text
drawImage
public void drawImage(Image img, int x, int y)Parameters
imgImage- the specified image to be drawn. This method does nothing if img is null.
xint- the x coordinate.
yint- the y coordinate.
isRoundedImageSupported
public boolean isRoundedImageSupported()Returns
drawImageRounded(Image, int, int, int, int, float) roundsisRoundedImageSupported
public boolean isRoundedImageSupported(Image img)Whether drawImageRounded(Image, int, int, int, int, float) will round
THIS image, which is the question a caller actually needs answered.
The platform may round and still not be able to round a given picture. Drawing an Image is a virtual call, and ComponentImage, DynamicImage, FontImage, RGBImage and SVGScaledView paint procedurally rather than handing over a bitmap; a rotated image keeps its angle beside a shared peer. None of those can be handed to a rounded draw without losing the drawing or the rotation, so they come out square.
A caller that consults the no-argument method alone therefore skips its own rounded-copy fallback on a platform that advertises support and gets square artwork for exactly those images. Ask this one per picture.
Parameters
imgImage- the image about to be drawn
Returns
drawImageRounded
public void drawImageRounded(Image img, int x, int y, int w, int h, float cornerRadius)Draws an image with rounded corners, without building a rounded copy of it, on platforms that support it; elsewhere the image is drawn square.
The corners are anti-aliased where the platform draws them analytically, which a shaped clip of the same outline is not.
Parameters
imgImage- the image to draw
xint- destination x
yint- destination y
wint- destination width
hint- destination height
cornerRadiusfloat- radius in pixels, clamped to half the smaller side
drawImage
public void drawImage(Image img, int x, int y, int w, int h)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(new Painter() {
public void paint(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
shapeShape- The shape to be drawn.
strokeStroke- 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(new Painter() {
public void paint(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
shapeShape- The shape to be filled.
See also
fillShapeShadow
public void fillShapeShadow(Shape shape, int fillColor, int fillAlpha, int shadowColor, float shadowOpacity, int blurRadius, int offsetX, int offsetY)Paint.setShadowLayer, iOS
CGContextSetShadow), with no retained bitmap. This lets borders/decorations render soft
shadows every frame without caching a per-component image. Guard with
#isShapeShadowSupported() and fall back to your own path otherwise.Parameters
shapeShape- the shape to fill (and whose fill casts the shadow)
fillColorint- the fill color as 0xRRGGBB
fillAlphaint- the fill alpha 0..255
shadowColorint- the shadow color as 0xRRGGBB
shadowOpacityfloat- the shadow opacity in the range 0..1
blurRadiusint- the gaussian blur radius in pixels
offsetXint- the shadow x offset in pixels
offsetYint- the shadow y offset in pixels
isShapeShadowSupported
public boolean isShapeShadowSupported()#fillShapeShadow renders a GPU shadow on this platform. When false the caller
should draw its shadow another way (e.g. the cached-image fallback in RoundRectBorder).Returns
isTransformSupported
public boolean isTransformSupported()com.codename1.ui.geom.Matrix transforms are supported by this graphics context.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.
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
isShapeSupported
public boolean isShapeSupported()#drawShape
and #fillShape methods. If this returns false, and you call #drawShape or #fillShape, then
nothing will be drawn.Returns
#drawShape and #fillShape are supported.See also
isShapeClipSupported
public boolean isShapeClipSupported()Returns
transform
public void transform(Transform transform)Parameters
transformTransform- The transform to concatenate.
getTransform
public Transform getTransform()#getTransform(com.codename1.ui.Transform) instead.Gets the transformation matrix that is currently applied to this graphics context.
Unlike java.awt.Graphics2D.getTransform(), the matrix returned here does
not include the integer translation set via #translate(int, int).
Codename One keeps that translation as a separate accumulator
(#getTranslateX() / #getTranslateY()) that is applied independently of
the affine matrix, so a freshly-translated context still reports the
identity transform. This split is deliberate: #setTransform /
getTransform / #transform(com.codename1.ui.Transform) round-trip
correctly only because the translate is excluded here – folding it in
would cause it to be counted twice (once in the matrix, once in the
drawing pipeline).
Consequently, to map a point from the current drawing coordinate space to the context’s device coordinates you must add the translation yourself:
float[] pt = { x + g.getTranslateX(), y + g.getTranslateY() };
g.getTransform().transformPoint(pt, pt);
// pt now holds the transformed device coordinates
If you only need to know whether a rectangle would be drawn on screen,
prefer #isVisible(int, int, int, int), which performs this mapping
(translate + transform) internally and tests it against the clip.
Returns
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
transformTransform- The transformation
com.codename1.ui.geom.Matrixto use for drawing. 2D/Affine transformations can be achieved using a 3x3 transformationcom.codename1.ui.geom.Matrix. 3D/Perspective transformations can be achieved using a 4x3 transformationcom.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.
As with #getTransform(), the loaded matrix does not include the integer
translation set via #translate(int, int); add #getTranslateX() /
#getTranslateY() to a point before transforming it if you need device
coordinates.
Parameters
tTransform- 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)Parameters
x1int- the x coordinate of the first vertex of the triangle
y1int- the y coordinate of the first vertex of the triangle
x2int- the x coordinate of the second vertex of the triangle
y2int- the y coordinate of the second vertex of the triangle
x3int- the x coordinate of the third vertex of the triangle
y3int- 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)Parameters
startColorint- the starting RGB color
endColorint- the ending RGB color
xint- the x coordinate
yint- the y coordinate
widthint- the width of the region to be filled
heightint- 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)Parameters
startColorint- the starting RGB color
endColorint- the ending RGB color
xint- the x coordinate
yint- the y coordinate
widthint- the width of the region to be filled
heightint- the height of the region to be filled
startAngleint- the beginning angle. Zero is at 3 o’clock. Positive angles are counter-clockwise.
arcAngleint- 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)Parameters
startColorint- the starting RGB color
endColorint- the ending RGB color
xint- the x coordinate
yint- the y coordinate
widthint- the width of the region to be filled
heightint- the height of the region to be filled
relativeXfloat- indicates the relative position of the gradient within the drawing region
relativeYfloat- indicates the relative position of the gradient within the drawing region
relativeSizefloat- 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)Parameters
startColorint- the starting RGB color
endColorint- the ending RGB color
xint- the x coordinate
yint- the y coordinate
widthint- the width of the region to be filled
heightint- the height of the region to be filled
horizontalboolean- indicating wheter it is a horizontal fill or vertical
fillGradient
public void fillGradient(Gradient gradient, int x, int y, int width, int height)LinearGradient, RadialGradient, or
ConicGradient - the port picks the right native shader path
(Java2D LinearGradientPaint/RadialGradientPaint on JavaSE; Android
LinearGradient/RadialGradient/SweepGradient shaders; software
rasterizer fallback elsewhere). Pass null or width/height <= 0 for a no-op.gaussianBlur
public Image gaussianBlur(Image source, float radius)blurRegion
public boolean blurRegion(int x, int y, int width, int height, float radius)glassRegion
public boolean glassRegion(int x, int y, int width, int height, float radius, float cornerRadius, float sat, float scale, float offset, float refract, float specular)lensRegion
public boolean lensRegion(int x, int y, int width, int height, float cornerRadius, float magnify, float aberration, int tintColor, float tintStrength)fillRect
public void fillRect(int x, int y, int w, int h, byte alpha)Parameters
xint- the x coordinate of the rectangle to be filled
yint- the y coordinate of the rectangle to be filled
wint- the width of the rectangle to be filled
hint- the height of the rectangle to be filled
alphabyte- the alpha values specify semitransparency
fillPolygon
public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)Parameters
xPointsint[]- a an array of x coordinates.
yPointsint[]- a an array of y coordinates.
nPointsint- a the total number of points.
drawPolygon
public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)Parameters
xPointsint[]- an array of x coordinates.
yPointsint[]- an array of y coordinates.
nPointsint- the total number of points.
isAlphaSupported
public boolean isAlphaSupported()Returns
setAndGetAlpha
public int setAndGetAlpha(int a)Parameters
aint- the alpha value
Returns
concatenateAlpha
public int concatenateAlpha(int a)Parameters
aint- Alpha value to concatenate (0-255).
Returns
getAlpha
public int getAlpha()Returns
setAlpha
public void setAlpha(int a)Parameters
aint- the alpha value
isAntiAliasingSupported
public boolean isAntiAliasingSupported()Returns
isAntiAliasedTextSupported
public boolean isAntiAliasedTextSupported()Returns
isAntiAliased
public boolean isAntiAliased()Returns
setAntiAliased
public void setAntiAliased(boolean a)Parameters
aboolean- true if antialiasing is active
isAntiAliasedText
public boolean isAntiAliasedText()Returns
setAntiAliasedText
public void setAntiAliasedText(boolean a)Parameters
aboolean- true if text antialiasing is supported
isAffineSupported
public boolean isAffineSupported()Returns
resetAffine
public void resetAffine()scale
public void scale(float x, float y)Parameters
xfloat- scale factor for x
yfloat- scale factor for y
translateMatrix
public void translateMatrix(float x, float y)Translates the coordinate system using the affine transform matrix
(as opposed to #translate(int, int) which uses a per-Graphics
integer accumulator). On every port today
isTranslationSupported() == false, which means g.translate(int, int)
is added to draw coordinates before the impl matrix is applied;
a subsequent g.scale() or g.rotate() therefore multiplies the
integer translate too. That’s surprising when porting code that came
from Java2D / AWT where translate composes into the matrix the same
way as scale and rotate.
translateMatrix composes the translation directly onto the impl
matrix, exactly like #scale(float, float) and #rotate(float) do.
The result is uniform “post-multiply translate onto the current
transform” semantics across iOS / JavaSE / Android / JavaScript –
the same code produces the same on-screen position regardless of
which port you target or whether you’re drawing into a Form’s
Graphics or a mutable Image’s Graphics.
On ports where #isTranslateMatrixSupported() returns false (e.g.
the legacy JavaScript port) the call falls back to the integer
#translate(int, int) so apps don’t silently render at the wrong
position – the visual result on those ports matches whatever
translate(int, int) does there.
Parameters
xfloat- x-axis translation
yfloat- y-axis translation
isTranslateMatrixSupported
public boolean isTranslateMatrixSupported()#translateMatrix(float, float) composes through the
impl matrix on this port (the matrix-correct mode) versus falling
back to the integer #translate(int, int) accumulator. Use this to
gate code that needs matrix-correct translation semantics.Returns
translateMatrix reaches the impl matrix; false on ports
where it falls back to the integer accumulator.See also
rotate
public void rotate(float angle)#rotateRadians(float)
which pivots around the context’s translated origin.Parameters
anglefloat- the rotation angle in radians about the screen origin.
See also
rotateRadians
public void rotateRadians(float angle)Parameters
anglefloat- the rotation angle in radians about graphics context’s translated origin.
rotate
public void rotate(float angle, int pivotX, int pivotY)int, int) whose pivot coordinates are relative to the current translation.Parameters
anglefloat- the rotation angle in radians
pivotXint- the pivot point In absolute coordinates.
pivotYint- the pivot point In absolute coordinates.
See also
rotateRadians
public void rotateRadians(float angle, int pivotX, int pivotY)Parameters
anglefloat- the rotation angle in radians
pivotXint- the pivot point relative to the current graphics context’s translation.
pivotYint- the pivot point relative to the current graphics context’s translation.
shear
public void shear(float x, float y)Parameters
xfloat- shear factor for x
yfloat- 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
endNativeGraphicsAccess
public void endNativeGraphicsAccess()tileImage
public void tileImage(Image img, int x, int y, int w, int h)Parameters
imgImage- the image
xint- coordinate to tile the image along
yint- coordinate to tile the image along
wint- coordinate to tile the image along
hint- coordinate to tile the image along
getScaleX
public float getScaleX()Returns
getScaleY
public float getScaleY()Returns
getRenderingHints
public int getRenderingHints()Returns
See also
setRenderingHints
public void setRenderingHints(int hints)Parameters
hintsint- int of rendering hints produced by logical AND on all applicable hints.