Search…
Image

Overview

An image is a rectangular image that you can draw or copy onto a surface.
Image objects can not be added directly to the user interface because they are not controllers (that is, they do not extend the Control class).
To display an image in the user interface, you need to use a control that best suits your needs, such as ImageControl, Button, or Animation. If you do not want to use any controls, you can draw the image on the screen using the Graphics object.
It is important to note that some transformation methods return a new instance of the image, while others apply to the current instance. To preserve an image with a single frame, use getFrameInstance (0);
To better understand TotalCross's recommended way to use images in your projects, go to the Colors, Fonts and Images page in the Guideline To Create Apps section.

Source Code

ImagImageAnimationSample.java
1
import totalcross.game.Animation;
2
import totalcross.sys.Settings;
3
import totalcross.ui.Button;
4
import totalcross.ui.ComboBox;
5
import totalcross.ui.Container;
6
import totalcross.ui.Label;
7
import totalcross.ui.dialog.MessageBox;
8
import totalcross.ui.event.ControlEvent;
9
import totalcross.ui.event.Event;
10
import totalcross.ui.gfx.Color;
11
import totalcross.ui.image.Image;
12
13
public class ImageAnimationSample extends Container {
14
final int gap = 5;
15
private Button btnStartStop;
16
private Animation anim;
17
private ComboBox cbEffect;
18
private int effect;
19
20
@Override
21
public void initUI() {
22
super.initUI();
23
add(btnStartStop = new Button(" Start/Stop "), CENTER, TOP + Settings.screenHeight / 7, SCREENSIZE + 50,
24
SCREENSIZE + 10);
25
btnStartStop.setBackForeColors(0x4583d4, 0xFFFFFF);
26
27
add(new Label("Effect: ", LEFT, Color.BLACK, false), LEFT + Settings.screenWidth / 5,
28
BOTTOM - Settings.screenHeight / 4);
29
30
String[] items = { "normal", "scaledBy", "smoothScaledBy", "getRotatedScaledInstance", "getTouchedUpInstance",
31
"changeColors", "fadedInstance", "applyColor2/dither" };
32
ComboBox.usePopupMenu = false;
33
add(cbEffect = new ComboBox(items), AFTER, BOTTOM - Settings.screenHeight / 4,
34
FILL - Settings.screenHeight / 10, PREFERRED);
35
cbEffect.setSelectedIndex(0);
36
cbEffect.setBackForeColors(0x3861af, 0xFFFFFF);
37
cbEffect.fillColor = 0x4583d4;
38
next(false);
39
}
40
41
@Override
42
public void onAddAgain() {
43
next(false);
44
}
45
46
@Override
47
public void onEvent(Event event) {
48
switch (event.type) {
49
case ControlEvent.PRESSED: {
50
if (event.target == cbEffect && effect != cbEffect.getSelectedIndex()) {
51
next(true);
52
} else if (event.target == btnStartStop) {
53
if (anim.isPaused) {
54
anim.resume();
55
} else {
56
anim.pause();
57
}
58
}
59
break;
60
}
61
}
62
}
63
64
/**
65
* shows next frame
66
*/
67
private void next(boolean changeEffect) {
68
try {
69
onRemove();
70
Image img = new Image("images/alligator.gif");
71
effect = cbEffect.getSelectedIndex();
72
double scale = Settings.isIOS() ? 1.5 : 2; // ios has less opengl
73
// memory
74
int scaledcount = Settings.screenWidth / 500;
75
System.out.println(scaledcount + " ," + Settings.screenWidth);
76
for (int i = 0; i < scaledcount; i++) {
77
img = img.scaledBy(scale, scale);
78
}
79
switch (effect) {
80
case 1:
81
img = img.scaledBy(scale, scale);
82
break;
83
case 2:
84
img = img.smoothScaledBy(scale, scale);
85
break;
86
case 3:
87
img = img.getRotatedScaledInstance(50, 90, -1);
88
break;
89
case 4:
90
img = img.getTouchedUpInstance((byte) 50, (byte) 100);
91
break;
92
case 5:
93
img.changeColors(0xFF31CE31, 0xFFFF00FF);
94
break;
95
case 6:
96
img = img.getFadedInstance();
97
break;
98
case 7:
99
img.applyColor2(Color.RED);
100
img.getGraphics().dither(0, 0, img.getWidth(), img.getHeight());
101
break;
102
}
103
if (Settings.isOpenGL) {
104
img.applyChanges();
105
}
106
anim = new Animation(img, 120);
107
anim.pauseIfNotVisible = true;
108
add(anim, CENTER, CENTER, PREFERRED, PREFERRED);
109
anim.start(Animation.LOOPS_UNLIMITED);
110
} catch (Throwable e) {
111
MessageBox.showException(e, true);
112
}
113
}
114
115
@Override
116
public void onRemove() {
117
if (anim != null) {
118
anim.stop();
119
remove(anim);
120
anim = null;
121
}
122
}
123
}
Copied!
Do not forget to create a folder called "images" inside /src/main/resources and save the alligator.gif image inside it [images].

Methods

Type
Name
Description
Constructor
Image(int width, int height)
Creates an Image object with the given width and height. The new image has the same color depth and color map of the default drawing surface.
Constructor
Image(byte[] fullDescription)
Creates an Image object from the given byte array, which must specify the whole image, including its headers. Use only JPEG or PNG images on the devices (GIF and BMP are supported on the desktop only).
Constructor
Image(String path)
Attempts to read the contents of the file specified by the given path, creating an Image object from the bytes read. The path given is the path to the image file. The file must be in 2, 16, 256, 24 bpp color compressed (RLE) or uncompressed BMP bitmap format, a PNG file, a GIF file, or a JPEG file. If the image cannot be loaded, an ImageException will be thrown.
Constructor
Image(Stream s)
Attempts to read the contents of the given stream, and create an Image object from the bytes read. Loads a BMP, JPEG, GIF, or PNG image from a stream. Note that GIF and BMP are supported only on the desktop. Note that all the bytes of the given stream will be fetched, even those bytes that may follow the image.
Boolean
isSupported(String filename)
Check if a specific file is supported by the platform at runtime; PNG or JPEG are always supported. GIF and BMP are supported on JavaSE only.
int
getWidth( )
Returns the image width
int
getHeight( )
Returns the image height
Graphics
getGraphics( )
Returns the Graphics object used by this image
void
changeColors(int from, int to)
change all pixels of the same color by another color
void
applyColor(int color)
Applies the given color RGB values to all pixels of this image, preserving the transparent color and alpha channel, if set.
Image
scaledBy(double scaleX, double scaleY)
Returns a scaled instance of this image. The new dimensions are calculated based on this image’s dimensions and the given proportions. The algorithm used is the replicate scale: not good quality, but fast. The given values must be > 0
Image
smoothScaledBy(double scaleX, double scaleY, int backColor)
Returns a scaled instance of this image. The new dimensions are calculated based on this image’s dimensions and the given proportions. The given values must be > 0. The transparent pixels are replaced by backColor, which produces a smooth border.
Image
getRotatedScaledInstance(int scale, int angle, int fillColor)
Returns a rotated and/or scaled version of this image, where the scale parameter indicates the percentage of scaling to be performe, the angle indicates the rotation angle, expressed in trigonometric degrees and FillColor is the fill color. Do not use this method for scaling only, because the scaling methods are faster. If you need a smooth scale and rotate, scale it first with smoothScaledBy() or getSmoothScaledInstance() and rotate it without scaling (or vice-versa).
Image
getTouchedUpInstance(byte brightness, byte contrast)
Retorna uma instância retocada da imagem, onde o parâmetro brightness indica o brilho(que deve ser entre -128 e 127) e o constrast indica o nível de contraste, que também deve ser entre -128(onde não há contraste) e 127(o nível máximo de contraste)
Image
getFadedInstance(int backColor)
Returns a touched-up instance of this image with the specified brightness and contrast.

References

Last modified 2yr ago