Revision: 2475
http://sourceforge.net/p/swingme/code/2475
Author: yuranet
Date: 2021-06-25 10:28:33 +0000 (Fri, 25 Jun 2021)
Log Message:
-----------
drawRegion works
Modified Paths:
--------------
iOSME/src/javax/microedition/lcdui/Graphics.java
Modified: iOSME/src/javax/microedition/lcdui/Graphics.java
===================================================================
--- iOSME/src/javax/microedition/lcdui/Graphics.java 2021-06-24 23:21:17 UTC (rev 2474)
+++ iOSME/src/javax/microedition/lcdui/Graphics.java 2021-06-25 10:28:33 UTC (rev 2475)
@@ -6,6 +6,7 @@
import apple.coregraphics.opaque.CGColorRef;
import apple.coregraphics.opaque.CGContextRef;
import apple.coregraphics.opaque.CGFontRef;
+import apple.coregraphics.opaque.CGImageRef;
import apple.coregraphics.struct.CGPoint;
import apple.coregraphics.struct.CGRect;
import apple.coregraphics.struct.CGSize;
@@ -15,7 +16,9 @@
import apple.foundation.NSMutableDictionary;
import apple.uikit.UIBezierPath;
import apple.uikit.UIColor;
+import apple.uikit.UIImage;
import apple.uikit.c.UIKit;
+import apple.uikit.enums.UIImageOrientation;
import apple.uikit.enums.UIRectCorner;
import javax.microedition.lcdui.game.Sprite;
import android.graphics.ColorMatrix;
@@ -84,12 +87,12 @@
public void setClip(int x, int y, int w, int h) {
CoreGraphics.CGContextResetClip(context);
- CoreGraphics.CGContextClipToRect(context, getRect(x, y, w, h));
+ CoreGraphics.CGContextClipToRect(context, getDestRect(x, y, w, h));
dirtyClip = true;
}
public void clipRect(int x, int y, int w, int h) {
- CoreGraphics.CGContextClipToRect(context, getRect(x, y, w, h));
+ CoreGraphics.CGContextClipToRect(context, getDestRect(x, y, w, h));
dirtyClip = true;
}
@@ -177,46 +180,6 @@
dirtyClip = true;
}
- public void setColorMatrix(ColorMatrix cm) {
- throw new UnsupportedOperationException();
- //paint.setColorFilter( cm==null?null:new ColorMatrixColorFilter(cm) );
- }
-
- public void drawImage(javax.microedition.lcdui.Image image, int x, int y, int anchor) {
-
- if (anchor == 0) {
- anchor = TOP | LEFT;
- }
-
- int ax;
- if ((anchor & LEFT) != 0) {
- ax = x;
- } else if ((anchor & HCENTER) != 0) {
- ax = x - image.getWidth() / 2;
- } else {
- ax = x - image.getWidth();
- }
-
- int ay;
- if ((anchor & TOP) != 0) {
- ay = y;
- } else if ((anchor & VCENTER) != 0) {
- ay = y - image.getHeight() / 2;
- } else {
- ay = y - image.getHeight();
- }
-
- CGRect imageRect = new CGRect(new CGPoint(tx+ax, ty+ay), new CGSize(image.getWidth(), image.getHeight()));
-
- UIKit.UIGraphicsBeginImageContext(imageRect.size());
- UIKit.UIGraphicsPushContext(context);
-
- image.getUIImage().drawInRect(imageRect);
-
- UIKit.UIGraphicsPopContext();
- UIKit.UIGraphicsEndImageContext();
- }
-
public void drawLine(int x1, int y1, int x2, int y2) {
CoreGraphics.CGContextMoveToPoint(context, tx+x1, ty+y1); //start at this point
CoreGraphics.CGContextAddLineToPoint(context, tx+x2, ty+y2); //draw to this point
@@ -223,18 +186,18 @@
CoreGraphics.CGContextStrokePath(context); // and now draw the Path!
}
- private CGRect getRect(int x, int y, int width, int height) {
+ private CGRect getDestRect(int x, int y, int width, int height) {
return new CGRect(new CGPoint(tx + x, ty + y), new CGSize(width, height));
}
public void drawRect(int x, int y, int width, int height) {
- CGRect rectangle = getRect(x, y, width, height);
+ CGRect rectangle = getDestRect(x, y, width, height);
CoreGraphics.CGContextAddRect(context, rectangle);
CoreGraphics.CGContextStrokePath(context);
}
public void drawRoundRect(int x, int y, int width, int height, int rx, int ry) {
- CGRect rectangle = getRect(x, y, width, height);
+ CGRect rectangle = getDestRect(x, y, width, height);
UIBezierPath bezierPath = UIBezierPath.bezierPathWithRoundedRectByRoundingCornersCornerRadii(rectangle, UIRectCorner.AllCorners, new CGSize(rx, ry));
CoreGraphics.CGContextAddPath(context, bezierPath.CGPath());
CoreGraphics.CGContextStrokePath(context);
@@ -241,18 +204,62 @@
}
public void fillRect(int x, int y, int width, int height) {
- CGRect rectangle = getRect(x, y, width, height);
+ CGRect rectangle = getDestRect(x, y, width, height);
CoreGraphics.CGContextAddRect(context, rectangle);
CoreGraphics.CGContextFillPath(context);
}
public void fillRoundRect(int x, int y, int width, int height, int rx, int ry) {
- CGRect rectangle = getRect(x, y, width, height);
+ CGRect rectangle = getDestRect(x, y, width, height);
UIBezierPath bezierPath = UIBezierPath.bezierPathWithRoundedRectByRoundingCornersCornerRadii(rectangle, UIRectCorner.AllCorners, new CGSize(rx, ry));
CoreGraphics.CGContextAddPath(context, bezierPath.CGPath());
CoreGraphics.CGContextFillPath(context);
}
+ public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
+ CGPoint center = new CGPoint(tx + x + width / 2, ty + y + height / 2);
+
+ CoreGraphics.CGContextSaveGState(context);
+
+ UIBezierPath clip = UIBezierPath.bezierPathWithArcCenterRadiusStartAngleEndAngleClockwise(center, Math.max(width / 2, height/ 2), startAngle, arcAngle, true);
+ clip.addLineToPoint(center);
+ clip.closePath();
+ clip.addClip();
+
+ UIBezierPath bezierPath = UIBezierPath.bezierPathWithOvalInRect(getDestRect(x, y, width, height));
+ CoreGraphics.CGContextAddPath(context, bezierPath.CGPath());
+ CoreGraphics.CGContextStrokePath(context);
+
+ CoreGraphics.CGContextRestoreGState(context);
+ }
+
+ public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
+ CGPoint center = new CGPoint(tx + x + width / 2, ty + y + height / 2);
+
+ CoreGraphics.CGContextSaveGState(context);
+
+ UIBezierPath clip = UIBezierPath.bezierPathWithArcCenterRadiusStartAngleEndAngleClockwise(center, Math.max(width / 2, height/ 2), startAngle, arcAngle, true);
+ clip.addLineToPoint(center);
+ clip.closePath();
+ clip.addClip();
+
+ UIBezierPath bezierPath = UIBezierPath.bezierPathWithOvalInRect(getDestRect(x, y, width, height));
+ CoreGraphics.CGContextAddPath(context, bezierPath.CGPath());
+ CoreGraphics.CGContextFillPath(context);
+
+ CoreGraphics.CGContextRestoreGState(context);
+ }
+
+ public void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3) {
+ CoreGraphics.CGContextSetLineWidth(context, 1.0);
+ CoreGraphics.CGContextMoveToPoint(context, tx+x1, ty+y1);
+ CoreGraphics.CGContextAddLineToPoint(context, tx+x2, ty+y2);
+ CoreGraphics.CGContextAddLineToPoint(context, tx+x3, ty+y3);
+ CoreGraphics.CGContextAddLineToPoint(context, tx+x1, ty+y1);
+ CoreGraphics.CGContextClosePath(context);
+ CoreGraphics.CGContextFillPath(context);
+ }
+
public Font getFont() {
return font;
}
@@ -333,49 +340,58 @@
//}
}
- public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
- CGPoint center = new CGPoint(tx + x + width / 2, ty + y + height / 2);
+ public void drawChar(char character, int x, int y, int anchor) {
+ char[] carr = new char[1];
+ carr[0] = character;
+ drawString(new String(carr), x, y, anchor); // as we send to a public method we do not need to add tx and ty
+ }
- CoreGraphics.CGContextSaveGState(context);
+ public void setColorMatrix(ColorMatrix cm) {
+ throw new UnsupportedOperationException();
+ //paint.setColorFilter( cm==null?null:new ColorMatrixColorFilter(cm) );
+ }
- UIBezierPath clip = UIBezierPath.bezierPathWithArcCenterRadiusStartAngleEndAngleClockwise(center, Math.max(width / 2, height/ 2), startAngle, arcAngle, true);
- clip.addLineToPoint(center);
- clip.closePath();
- clip.addClip();
+ public void drawImage(Image image, int x, int y, int anchor) {
- UIBezierPath bezierPath = UIBezierPath.bezierPathWithOvalInRect(getRect(x, y, width, height));
- CoreGraphics.CGContextAddPath(context, bezierPath.CGPath());
- CoreGraphics.CGContextStrokePath(context);
+ if (anchor == 0) {
+ anchor = TOP | LEFT;
+ }
- CoreGraphics.CGContextRestoreGState(context);
- }
+ int ax;
+ if ((anchor & LEFT) != 0) {
+ ax = x;
+ }
+ else if ((anchor & HCENTER) != 0) {
+ ax = x - image.getWidth() / 2;
+ }
+ else {
+ ax = x - image.getWidth();
+ }
- public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
- CGPoint center = new CGPoint(tx + x + width / 2, ty + y + height / 2);
+ int ay;
+ if ((anchor & TOP) != 0) {
+ ay = y;
+ }
+ else if ((anchor & VCENTER) != 0) {
+ ay = y - image.getHeight() / 2;
+ }
+ else {
+ ay = y - image.getHeight();
+ }
- CoreGraphics.CGContextSaveGState(context);
+ CGRect imageRect = getDestRect(ax, ay, image.getWidth(), image.getHeight());
- UIBezierPath clip = UIBezierPath.bezierPathWithArcCenterRadiusStartAngleEndAngleClockwise(center, Math.max(width / 2, height/ 2), startAngle, arcAngle, true);
- clip.addLineToPoint(center);
- clip.closePath();
- clip.addClip();
+ //UIKit.UIGraphicsBeginImageContext(imageRect.size()); // for drawing into a new image
- UIBezierPath bezierPath = UIBezierPath.bezierPathWithOvalInRect(getRect(x, y, width, height));
- CoreGraphics.CGContextAddPath(context, bezierPath.CGPath());
- CoreGraphics.CGContextFillPath(context);
+ UIKit.UIGraphicsPushContext(context);
+ image.getUIImage().drawInRect(imageRect);
+ UIKit.UIGraphicsPopContext();
- CoreGraphics.CGContextRestoreGState(context);
+ //UIKit.UIGraphicsEndImageContext();
}
- public void drawChar(char character, int x, int y, int anchor) {
- char[] carr = new char[1];
- carr[0] = character;
- drawString(new String(carr), x, y, anchor); // as we send to a public method we do not need to add tx and ty
- }
+ public void drawRegion(Image src, int xSrc, int ySrc, int width, int height, int transform, int x_dst, int y_dst, int anchor) {
- public void drawRegion(Image src, int xSrc, int ySrc, int width,
- int height, int transform, int x_dst, int y_dst, int anchor) {
-
// Speed up: Handle full image painting first
if (src != null && transform == Sprite.TRANS_NONE && xSrc == 0 && ySrc == 0 && width == src.getWidth() && height == src.getHeight()) {
drawImage(src, x_dst, y_dst, anchor);
@@ -382,78 +398,8 @@
return;
}
- //drawRegion(src, xSrc, ySrc, width, height, getMatrix(transform), x_dst, y_dst, anchor);
- throw new UnsupportedOperationException();
- }
-/*
- // Cache Matrix. Avoid object creation.
- private static Matrix matrix = new Matrix();
-
- public static Matrix getMatrix(int transform) {
-
- int rotate;
- boolean mirror;
-
- switch (transform) {
- case Sprite.TRANS_NONE: {
- rotate = 0;
- mirror = false;
- break;
- }
- case Sprite.TRANS_ROT90: {
- rotate = 90;
- mirror = false;
- break;
- }
- case Sprite.TRANS_ROT180: {
- rotate = 180;
- mirror = false;
- break;
- }
- case Sprite.TRANS_ROT270: {
- rotate = 270;
- mirror = false;
- break;
- }
- case Sprite.TRANS_MIRROR: {
- rotate = 0;
- mirror = true;
- break;
- }
- case Sprite.TRANS_MIRROR_ROT90: {
- rotate = 90;
- mirror = true;
- break;
- }
- case Sprite.TRANS_MIRROR_ROT180: {
- rotate = 180;
- mirror = true;
- break;
- }
- case Sprite.TRANS_MIRROR_ROT270: {
- rotate = 270;
- mirror = true;
- break;
- }
- default: {
- throw new IllegalArgumentException("Bad transform");
- }
- }
-
- // Create a matrix and apply the rotation and mirroring (scale == -1)
- matrix.reset();
- matrix.preRotate(rotate);
- matrix.preScale(mirror ? -1.0f : 1.0f, 1.0f);
- return matrix;
-
- }
-
- public void drawRegion(Image src, int xSrc, int ySrc, int width,
- int height, Matrix matrix, int xDst, int yDst, int anchor) {
-
// may throw NullPointerException, this is ok
- if (xSrc + width > src.getWidth() || ySrc + height > src.getHeight() ||
- width < 0 || height < 0 || xSrc < 0 || ySrc < 0) {
+ if (xSrc + width > src.getWidth() || ySrc + height > src.getHeight() || width < 0 || height < 0 || xSrc < 0 || ySrc < 0) {
throw new IllegalArgumentException("Area out of Image: "+src.getWidth()+"x"+src.getHeight()+" area: "+xSrc+","+ySrc+" "+width+"x"+height);
}
@@ -461,92 +407,98 @@
anchor = TOP | LEFT;
}
+ int destWidth = width;
+ int destHeight = height;
+ if (transform == Sprite.TRANS_ROT90 || transform == Sprite.TRANS_ROT270 || transform == Sprite.TRANS_MIRROR_ROT90 || transform == Sprite.TRANS_MIRROR_ROT270) {
+ destWidth = height;
+ destHeight = width;
+ }
-
- // Get the destination rectangle after rotation and mirroring...
- RectF r = new RectF(0, 0, width, height);
- matrix.mapRect(r);
-
-
- float anchorX = (r.right - r.left);
+ int anchorX = x_dst;
int nRefs = 0;
// Process horizontal anchor (LEFT, RIGHT and HCENTER)
if ((anchor & LEFT) > 0) {
nRefs++;
- anchorX = 0.0f;
}
if ((anchor & HCENTER) > 0) {
nRefs++;
- anchorX = anchorX / 2.0f;
+ anchorX = anchorX - destWidth / 2;
}
if ((anchor & RIGHT) > 0) {
nRefs++;
+ anchorX = anchorX - destWidth;
}
// Can only have one horizontal anchor
if (nRefs > 1) {
- throw new IllegalArgumentException("Bad Anchor");
+ throw new IllegalArgumentException("Bad Anchor " + anchor);
}
- float anchorY = (r.bottom - r.top);
+ int anchorY = y_dst;
nRefs = 0;
// Process vertical anchor (BASELINE, BOTTOM, TOP and VCENTER)
+ if ((anchor & TOP) > 0) {
+ nRefs++;
+ }
if ((anchor & BASELINE) > 0) {
nRefs++;
+ anchorY = anchorY - destHeight;
}
if ((anchor & BOTTOM) > 0) {
nRefs++;
+ anchorY = anchorY - destHeight;
}
- if ((anchor & TOP) > 0) {
- nRefs++;
- anchorY = 0.0f;
- }
if ((anchor & VCENTER) > 0) {
nRefs++;
- anchorY = anchorY / 2.0f;
+ anchorY = anchorY - destHeight / 2;
}
// Can only have one horizontal anchor
if (nRefs > 1) {
- throw new IllegalArgumentException("Bad Anchor");
+ throw new IllegalArgumentException("Bad Anchor " + anchor);
}
- canvas.save();
+ CGRect srcRect = new CGRect(new CGPoint(xSrc, ySrc), new CGSize(width, height));
+ CGRect destRect = getDestRect(anchorX, anchorY, destWidth, destHeight);
- // Translate for new destination, destination rectangle and anchor
- canvas.translate(tx+(xDst - r.left - anchorX), ty+(yDst - r.top - anchorY));
+ CGImageRef cropped = CoreGraphics.CGImageCreateWithImageInRect(src.getUIImage().CGImage(), srcRect);
+ UIImage transformed = src.getUIImage().imageWithCGImageScaleOrientation(cropped, src.getUIImage().scale(), getUIImageOrientation(transform));
- // Apply the transformation matrix
- canvas.concat(matrix);
-
- // Draw the image
- Rect srcRect = new Rect(xSrc, ySrc, xSrc + width, ySrc + height);
- Rect dstRect = new Rect(0, 0, width, height);
-
-
- int a = paint.getAlpha();
- paint.setAlpha(0xFF); // we do not support alpha when drawing images
- canvas.drawBitmap(src.getUIImage(), srcRect, dstRect, paint);
- paint.setAlpha(a);
-
- canvas.restore();
+ UIKit.UIGraphicsPushContext(context);
+ transformed.drawInRect(destRect);
+ UIKit.UIGraphicsPopContext();
}
-*/
- public void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3) {
- CoreGraphics.CGContextSetLineWidth(context, 1.0);
- CoreGraphics.CGContextMoveToPoint(context, tx+x1, ty+y1);
- CoreGraphics.CGContextAddLineToPoint(context, tx+x2, ty+y2);
- CoreGraphics.CGContextAddLineToPoint(context, tx+x3, ty+y3);
- CoreGraphics.CGContextAddLineToPoint(context, tx+x1, ty+y1);
- CoreGraphics.CGContextClosePath(context);
- CoreGraphics.CGContextFillPath(context);
+ /**
+ * @see apple.uikit.enums.UIImageOrientation
+ * @see apple.imageio.enums.CGImagePropertyOrientation
+ */
+ public static long getUIImageOrientation(int transform) {
+ switch (transform) {
+ case Sprite.TRANS_NONE:
+ return UIImageOrientation.Up;
+ case Sprite.TRANS_ROT90:
+ return UIImageOrientation.Right;
+ case Sprite.TRANS_ROT180:
+ return UIImageOrientation.Down;
+ case Sprite.TRANS_ROT270:
+ return UIImageOrientation.Left;
+ case Sprite.TRANS_MIRROR:
+ return UIImageOrientation.UpMirrored;
+ case Sprite.TRANS_MIRROR_ROT90:
+ return UIImageOrientation.RightMirrored;
+ case Sprite.TRANS_MIRROR_ROT180:
+ return UIImageOrientation.DownMirrored;
+ case Sprite.TRANS_MIRROR_ROT270:
+ return UIImageOrientation.LeftMirrored;
+ default:
+ throw new IllegalArgumentException("Bad transform " + transform);
+ }
}
- public void drawRGB(int[] rgbData, int offset, int scanlength, int x,
- int y, int width, int height, boolean processAlpha) {
+ public void drawRGB(int[] rgbData, int offset, int scanlength, int x, int y, int width, int height, boolean processAlpha) {
if (rgbData == null)
throw new NullPointerException();
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|