-
Notifications
You must be signed in to change notification settings - Fork 13
/
ScaledImages.java
80 lines (63 loc) · 2.41 KB
/
ScaledImages.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Java
*
* Copyright 2016-2022 MicroEJ Corp. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be found with this software.
*/
package com.microej.howto.microui.image;
import ej.drawing.TransformPainter;
import ej.microui.MicroUI;
import ej.microui.display.Colors;
import ej.microui.display.Display;
import ej.microui.display.Displayable;
import ej.microui.display.GraphicsContext;
import ej.microui.display.Image;
import ej.microui.display.Painter;
/**
* This class shows how to use the TransformPainter utility class with scale draw method. The example draws an image
* scaled up to the display smallest axis
*/
public class ScaledImages extends Displayable {
@Override
public void render(GraphicsContext g) {
final Display display = Display.getDisplay();
final int displayWidth = display.getWidth();
final int displayHeight = display.getHeight();
// fill up background with black
g.setColor(Colors.BLACK);
Painter.fillRectangle(g, 0, 0, displayWidth, displayHeight);
Image image = Image.getImage("/images/mascot.png"); //$NON-NLS-1$
final int imageWidth = image.getWidth();
final int imageHeight = image.getHeight();
// compute scaling factors on both dimensions (ensure float division is used)
final float horizontalScalingFactor = (float) displayWidth / imageWidth;
final float verticalScalingFactor = (float) displayHeight / imageHeight;
// keep only the smallest one
final float actualScalingFactor = Math.min(horizontalScalingFactor, verticalScalingFactor);
// compute image top left anchor
final int imageWidthScaled = (int) (imageWidth * actualScalingFactor);
final int imageHeightScaled = (int) (imageHeight * actualScalingFactor);
final int anchorX = (displayWidth - imageWidthScaled) / 2;
final int anchorY = (displayHeight - imageHeightScaled) / 2;
// draw scaled image
TransformPainter.drawScaledImageBilinear(g, image, anchorX, anchorY, actualScalingFactor, actualScalingFactor);
}
/**
* Entry Point for the example.
*
* @param args
* Not used.
*/
public static void main(String[] args) {
// A call to MicroUI.start is required to initialize the graphics
// runtime environment
MicroUI.start();
ScaledImages sample = new ScaledImages();
Display.getDisplay().requestShow(sample);
}
@Override
public boolean handleEvent(int event) {
// No event handling is required for this sample.
return false;
}
}