use of com.codename1.ui.Graphics in project CodenameOne by codenameone.
the class CodenameOneImplementation method setClipRect.
/**
* Installs a new clipping rectangle
*
* @param graphics the graphics context
* @param rect rectangle representing the new clipping area
*/
public void setClipRect(Object graphics, Rectangle rect) {
Dimension d = rect.getSize();
setClip(graphics, rect.getX(), rect.getY(), d.getWidth(), d.getHeight());
}
use of com.codename1.ui.Graphics in project CodenameOne by codenameone.
the class BlackBerryCanvas method paint.
public void paint(Graphics g) {
int f = getFieldCount();
if (f > 0) {
g.drawBitmap(0, 0, getWidth(), getHeight(), screen, 0, 0);
Form currentForm = Display.getInstance().getCurrent();
for (int iter = 0; iter < f; iter++) {
Field fld = getField(iter);
int pops = 0;
if (currentForm != null) {
PeerComponent p = findPeer(currentForm.getContentPane(), fld);
if (p != null) {
pops = clipOnLWUITBounds(p, g);
} else {
Component cmp = currentForm.getFocused();
// we are now editing an edit field
if (cmp != null && cmp instanceof TextArea && cmp.hasFocus() && fld instanceof EditField) {
pops = clipOnLWUITBounds(cmp, g);
int x = fld.getLeft();
int y = fld.getTop();
g.clear(x, y, Math.max(cmp.getWidth(), fld.getWidth()), Math.max(cmp.getHeight(), fld.getHeight()));
}
}
}
paintChild(g, fld);
while (pops > 0) {
g.popContext();
pops--;
}
}
} else {
g.drawBitmap(0, 0, getWidth(), getHeight(), screen, 0, 0);
}
g.setColor(0);
g.drawText(debug, 0, 0);
painted = true;
}
use of com.codename1.ui.Graphics in project CodenameOne by codenameone.
the class Capture method capturePhoto.
/**
* <p>Invokes the camera and takes a photo synchronously while blocking the EDT, the sample below
* demonstrates a simple usage and applying a mask to the result</p>
* <script src="https://gist.github.com/codenameone/b18c37dfcc7de752e0e6.js"></script>
* <img src="https://www.codenameone.com/img/developer-guide/graphics-image-masking.png" alt="Picture after the capture was complete and the resulted image was rounded. The background was set to red so the rounding effect will be more noticeable" />
*
* @param width the target width for the image if possible, some platforms don't support scaling. To maintain aspect ratio set to -1
* @param height the target height for the image if possible, some platforms don't support scaling. To maintain aspect ratio set to -1
* @return the photo file location or null if the user canceled
*/
public static String capturePhoto(int width, int height) {
CallBack c = new CallBack();
if ("ios".equals(Display.getInstance().getPlatformName()) && (width != -1 || height != -1)) {
// workaround for threading issues in iOS https://github.com/codenameone/CodenameOne/issues/2246
capturePhoto(c);
Display.getInstance().invokeAndBlock(c);
if (c.url == null) {
return null;
}
ImageIO scale = Display.getInstance().getImageIO();
if (scale != null) {
try {
String path = c.url.substring(0, c.url.indexOf(".")) + "s" + c.url.substring(c.url.indexOf("."));
OutputStream os = FileSystemStorage.getInstance().openOutputStream(path);
scale.save(c.url, os, ImageIO.FORMAT_JPEG, width, height, 1);
Util.cleanup(os);
FileSystemStorage.getInstance().delete(c.url);
return path;
} catch (IOException ex) {
Log.e(ex);
}
}
} else {
c.targetWidth = width;
c.targetHeight = height;
capturePhoto(c);
Display.getInstance().invokeAndBlock(c);
}
return c.url;
}
use of com.codename1.ui.Graphics in project CodenameOne by codenameone.
the class IOSImplementation method createAlphaMask.
// -------------------------------------------------------------------------
// METHODS FOR DRAWING SHAPES AND TRANSFORMATIONS
// -------------------------------------------------------------------------
/**
* Creates a platform-specific alpha mask for a shape. This is used to cache
* masks in the {@link com.codename1.ui.GeneralPath} class. On iOS the alpha
* mask is an OpenGL texture ID (not a raster of alpha pixels), but other platforms
* may use different representations if they like.
*
* <p>The {@link com.codename1.ui.Graphics#drawAlphaMask} method
* is used to draw a mask on the graphics context and this will ultimately call {@link #drawAlphaMask}
* which can be platform specific also.
* </p>
* @param shape The shape that will have an alpha mask created.
* @param stroke The stroke settings for stroking the outline of the mask. Leave null to produce a fill
* mask.
* @return The platform specific alpha mask object or null if it is not supported or failed.
* @see #deleteAlphaMask
* @see #drawAlphaMask
* @see #isAlphaMaskSupported
* @see com.codename1.ui.Graphics#drawAlphaMask
* @see com.codename1.ui.GeneralPath#getAlphaMask
*/
public TextureAlphaMask createAlphaMask(Shape shape, Stroke stroke) {
int[] bounds = new int[] { 0, 0, 0, 0 };
long tex = nativeCreateAlphaMaskForShape(shape, stroke, bounds);
Rectangle shapeBounds = shape.getBounds();
int[] padding = new int[] { // top
shapeBounds.getY() - bounds[1], // right
bounds[2] - (shapeBounds.getX() + shapeBounds.getWidth()), // bottom
bounds[3] - (shapeBounds.getY() + shapeBounds.getHeight()), // left
shapeBounds.getX() - bounds[0] };
if (tex == 0) {
return null;
}
return new TextureAlphaMask(tex, new Rectangle(bounds[0], bounds[1], bounds[2] - bounds[0], bounds[3] - bounds[1]), padding);
}
use of com.codename1.ui.Graphics in project CodenameOne by codenameone.
the class ImageViewer method paint.
/**
* {@inheritDoc}
*/
@Override
public void paint(Graphics g) {
if (panPositionX < 0) {
Style s = getStyle();
int width = getWidth() - s.getHorizontalPadding();
float ratio = ((float) width) * (panPositionX * -1);
g.drawImage(image, ((int) ratio) + getX() + imageX, getY() + imageY, imageDrawWidth, imageDrawHeight);
if (cycleLeft || swipeableImages.getSelectedIndex() > getImageLeftPos()) {
Image left = getImageLeft();
if (swipePlaceholder != null) {
left.asyncLock(swipePlaceholder);
} else {
left.lock();
}
ratio = ratio - width;
imageAspectCalc(left);
g.drawImage(left, ((int) ratio) + getX() + prefX, getY() + prefY, prefW, prefH);
}
return;
}
if (panPositionX > 1) {
Style s = getStyle();
int width = getWidth() - s.getHorizontalPadding();
float ratio = ((float) width) * (1 - panPositionX);
g.drawImage(image, ((int) ratio) + getX() + imageX, getY() + imageY, imageDrawWidth, imageDrawHeight);
if (cycleRight || swipeableImages.getSelectedIndex() < getImageRightPos()) {
Image right = getImageRight();
if (swipePlaceholder != null) {
right.asyncLock(swipePlaceholder);
} else {
right.lock();
}
ratio = ratio + width;
imageAspectCalc(right);
g.drawImage(right, ((int) ratio) + getX() + prefX, getY() + prefY, prefW, prefH);
}
return;
}
// can happen in the GUI builder
if (image != null) {
g.drawImage(image, getX() + imageX, getY() + imageY, imageDrawWidth, imageDrawHeight);
}
}
Aggregations