public class ImageRotation extends AbstractTransform
To rotate an image on itself, use the following lines:
ImageRotation rotation = new ImageRotation();
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
int rx = x + imageWidth / 2;
int ry = y + imageHeight / 2;
rotation.setRotationCenter(rx, ry);
rotation.setAngle(78);
rotation.drawNearestNeighbor(gc, image, rx, ry, GraphicsContext.HCENTER | GraphicsContext.VCENTER);
To rotate an image around a circle, use the following lines:
ImageRotation rotation = new ImageRotation();
rotation.setRotationCenter(rx, ry);
for (int i = 0; i < 360; i += 45) {
rotation.setAngle(i);
rotation.drawBilinear(gc, image, rx - diameter / 2, ry - diameter / 2,
GraphicsContext.TOP | GraphicsContext.LEFT);
}
An image rotation instance holds a global state for all drawings. Several instances can be created at the same time.
However a default instance is created on MicroUI framework startup and is always available.
Modifier and Type | Field and Description |
---|---|
static ImageRotation |
Singleton
Default instance created on MicroUI framework startup.
|
Constructor and Description |
---|
ImageRotation() |
Modifier and Type | Method and Description |
---|---|
void |
drawBilinear(GraphicsContext gc,
Image image,
int x,
int y,
int anchor)
Draws the given
Image applying the current rotation. |
void |
drawNearestNeighbor(GraphicsContext gc,
Image image,
int x,
int y,
int anchor)
Draws the given
Image applying the current rotation. |
int |
getAngle()
Returns the current rotation angle.
|
int |
getRotationX()
Returns the current X coordinate.
|
int |
getRotationY()
Returns the current Y coordinate.
|
void |
setAngle(int angle)
Set the new rotation angle.
|
void |
setRotationCenter(int x,
int y)
Set the new rotation center coordinates.
|
getAlpha, getTranslateX, getTranslateY, isMirrored, resetAlpha, resetTranslate, setAlpha, setMirror, translate
public static final ImageRotation Singleton
public int getAngle()
public void setAngle(int angle)
angle
- the new rotation anglepublic void setRotationCenter(int x, int y)
x
- the x coordinatey
- the y coordinatepublic int getRotationX()
public int getRotationY()
public void drawBilinear(GraphicsContext gc, Image image, int x, int y, int anchor)
Image
applying the current rotation. This method uses the bilinear
algorithm
to render the image. This algorithm performs better rendering than nearest neighbor
algorithm but it
is slower to apply.gc
- the GraphicsContext
where to render the drawing.image
- the Image
to drawx
- the x coordinate of the image reference anchor pointy
- the y coordinate of the image reference anchor pointanchor
- position of the image reference point around the anchor pointjava.lang.NullPointerException
- if @{code image} or @{code gc} is @{code null}java.lang.IllegalArgumentException
- if anchor
is not a valid value (BASELINE
is illegal).drawNearestNeighbor(GraphicsContext, Image, int, int, int)
public void drawNearestNeighbor(GraphicsContext gc, Image image, int x, int y, int anchor)
Image
applying the current rotation. This method uses the nearest neighbor
algorithm to render the image. This algorithm is faster than bilinear
algorithm but its rendering is
more simple.gc
- the GraphicsContext
where render the drawing.image
- the Image
to drawx
- the x coordinate of the image reference anchor pointy
- the y coordinate of the image reference anchor pointanchor
- position of the image reference point around the anchor pointjava.lang.NullPointerException
- if @{code image} or @{code gc} is @{code null}java.lang.IllegalArgumentException
- if anchor
is not a valid value (BASELINE
is illegal).drawBilinear(GraphicsContext, Image, int, int, int)